12 可迭代参数 any
# 全部为false,返回false def any(iterable): for ele in iterable: if ele: return False return True li = [0,False,''] print(any(li)) #False
13.enumerate 列出遍历数据和下标
li = ['a','b','c'] for index,item in enumerate(li,7): print(index,item) # 改下标 7 a 8 b 9 c
14.set集合 不支持索引和切片,无序不重复
1.创建集合1
set1 = {'1','2'} set2 = {'11','1'} # 添加 add set1.add('3') # 清空 clear() set1.clear() # 取差集 difference set1.difference(set2) #set1取set1中有的 # 取交集 set1.intersection(set2) # 取并集 set1.union(set2) set1 | set2 # 末尾移除 set1.pop() # 指定移除 set1.discard(3) # 更新 update 合并一起去重 set1.update(set2)
15 练习题1 三组数据求和
# 1-10,20-30,35-40 def threeSum(a1,a2,a3): return sum(a1+a2+a3) a1 = list(range(1,11)) a2 = list(range(20,31)) a3 = list(range(35,41)) print(threeSum(a1,a2,a3))
16 练习题2 大小和尚多少个
def computers(): for i in range(1,101): for j in range(1,34): if i+j==100 and 3*j+i/3 ==100: print('大和尚有{}个,小和尚有{}个'.format(j,i)) pass computers() # 大和尚有25个,小和尚有75个
17 练习题3 找出独一无二的数据
li = [1,1,1,2,2,2,2,3,2,2,3,4,2,1,1] def findUnionNumber(li): for item in li: if li.count(item)==1: return item pass print(findUnionNumber(li))
1.字典统计每个元素的次数
dict ={} for key in li: dict[key] = dict.get(key,0)+1 print(dict)
2.collection包下Counter类统计
from collections import Counter a = [1, 2, 3, 1, 1, 2] result = Counter(a) print(result)
3.pandas包下的value_counts方法统计
import pandas as pd a = pd.DataFrame([[1,2,3], [3,1,3], [1,2,1]]) result = a.apply(pd.value_counts) print(result)
18.利用set找出独一无二的数据
li = [1,2,3,3,2,3,4,4,5,1,2,1] def uniqueNum(li): set1 = set(li) for i in set1: li.remove(i) set2 = set(li) for j in set2: set1.remove(j) return set1 print(uniqueNum(li))
19 面向对象编程 oop
# 面向过程编程 根据业务从上到下开始编程 # 类的结构 # 类名 属性 方法 class People: name = 'zhan', age = 20, def eat(self): print('正在吃饭') # 创建对象 people = People() people.eat()