【数据挖掘与机器学习】二、Python编程基础(2)

【数据挖掘与机器学习】二、Python编程基础(2)


注:本文适合有编程基础的同学快速入门

今天来更新python基础知识(2)啦,想要看(1)的小伙伴可以在下面链接可以看到哦~
python基础知识(1)
接下来就正式进入今天的正题了

列表

1.加入元素

list = ['a','b','c','d']
list.append('baidu')
print("追加后的列表:",list)
list.insert(1,'google')
print("插入元素后的列表:",list)

运行上面的代码,结果如下:

追加后的列表 :  ['a', 'b', 'c', 'd', 'Baidu']
插入元素后的列表 :  ['a', 'Google', 'b', 'c', 'd', 'Baidu']

2.列表的索引

list = ['a','b','c','d']
print("b的索引值为:",list.index('b'))

运行上面的代码,结果如下:

b索引值为 1

3.删除元素

list = ['a','b','c','d']
list.remove('d')
print("列表现在为:",list)
p=list.pop()
print("删除%r后的列表为%r"%(p,list))
print("删除元素为:",list.pop(1))

运行上面的代码,结果如下:

列表现在为 :  ['a', 'b', 'c']
删除 'c' 后的列表为 ['a', 'b'] : 
删除元素为 :  b

4.列表元素倒排和统计元素出现次数

list = ['a', 'b', 'c','a', 'd','a','c']
list.reverse()
print("倒排后的list是:",list)
print("c的出现次数是:",list.count('c'))
list.sort(reverse=True)#将列表元素从大到小排序
list

运行上面的代码,结果如下:

倒排后的list['c', 'a', 'd', 'a', 'c', 'b', 'a']
c的出现次数是: 2
['d', 'c', 'c', 'b', 'a', 'a', 'a']

5.列表推导式示例

vec = [2, 4, 6, 8, 10]
print([3*x for x in vec])
vec = [2, 4, 6,8,10]
print([3*x  for  x  in  vec  if x > 6])
vec1 = [2, 4, 6]
vec2 = [4, 3, -9]
print([x*y for x in vec1 for y in vec2 if x*y>0])

运行上面的代码,结果如下:

[6, 12, 18, 24, 30]
[24, 30]
[8, 6, 16, 12, 24, 18]

元组

元组和列表的不同之处在于,元组是圆括号’()‘表示的,列表是方括号’[]'表示的。

tup = tuple('bar')  #创建元组
print('输出元组tup:',tup)  #输出元组
nested_tup = (4,5,6),(7,8)
print('输出元组tup:',nested_tup)  #元素是元组的元组
print('元组的连接',tup+tuple('wwy'))
a,b,c = tup  #元组的拆分
print(a,b,c)
print(tup.count(a)) # 统计某个数值在元组中出现的次数

运行结果如下:

输出元组tup: ('b', 'a', 'r')
输出元组tup: ((4, 5, 6), (7, 8))
元组的连接 ('b', 'a', 'r', 'w', 'w', 'y')
b a r
1

字典
scientists = {'Newton' : 1642, 'Darwin' : 1809, 'Turing' : 1912}
print('keys:', scientists.keys())  #返回字典中的所有键
print('values:', scientists.values()) #返回字典中的所有值
print('items:', scientists.items()) #返回所有键值对
print('get:', scientists.get('Curie', 1867))  # get方法
temp = {'Curie' : 1867, 'Hopper' : 1906, 'Franklin' : 1920}
scientists.update(temp)  #用字典temp更新字典scientists
print('after update:', scientists)
scientists.clear() # 清空字典
print('after clear:', scientists)

运行结果如下:

keys: dict_keys(['Newton', 'Darwin', 'Turing'])
values: dict_values([1642, 1809, 1912])
items: dict_items([('Newton', 1642), ('Darwin', 1809), ('Turing', 1912)])
get: 1867
after update: {'Newton': 1642, 'Darwin': 1809, 'Turing': 1912, 'Curie': 1867, 'Hopper': 1906, 'Franklin': 1920}
after clear: {}

可以在自己的python编译器上尝试运行一下,更有利于提升熟练度哦


集合
set1 = set([0,1,2,3,4])
set2 = set([1,3,5,7,9])
print(set1.issubset(set2))#是否属于
print(set1.union(set2))#并集
print(set2.difference(set1))

结果如下:

False
{0, 1, 2, 3, 4, 5, 7, 9}
{9, 5, 7}

总结

目前为止python最最基础的语法内容已经过完了(当然不是很细节,只能说大致过了一下),接下来会更新python的numpy库(主要用于处理python数组),后续还会更新pandas~

  • 11
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值