Day16: python生产列表的使用方法
数据再运算
a = range(0,15) b = [_**2 for _ in a] c = [str(_) for _ in a] print(b) print(c) output: [0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196] ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14']
就是说用好循环,变量值怎么赋值 掌握这种小技巧
一串随机数
#生成 10 个 0 到 1 的随机浮点数,保留小数点后两位 from random import random a = [round(random(),2) for _ in range(10)] print(a) #生成 10 个 0 到 10 的满足均匀分布的浮点数,保留小数点后3位: from random import uniform b = [round(uniform(0,10),3) for _ in range(10)] print(b) output: [0.53, 0.14, 0.0, 0.42, 0.27, 0.87, 0.89, 0.28, 0.5, 0.65] [2.206, 5.056, 8.73, 8.816, 2.416, 7.271, 2.327, 8.125, 8.961, 2.768]
if 和嵌套 for
#对一个列表里面的数据筛选,只计算 [0,11) 中3的倍数 a = range(15) b = [x**2 for x in a if x%3==0] print(b) # for 嵌套 for .一行代码生成 99 乘法表的所有 45 个元素 c = [i*j for i in range(10) for j in range(1,i+1)] print(c) output: [0, 9, 36, 81, 144] [1, 2, 4, 3, 6, 9, 4, 8, 12, 16, 5, 10, 15, 20, 25, 6, 12, 18, 24, 30, 36, 7, 14, 21, 28, 35, 42, 49, 8, 16, 24, 32, 40, 48, 56, 64, 9, 18, 27, 36, 45, 54, 63, 72, 81]
zip 和列表
# zip 和列表 a= range(4) b = ['a','b','c','d','e'] c = [str(y) + str(x) for x, y in zip(a,b)] print(c) zip(a,b) output: ['a0', 'b1', 'c2', 'd3'] <zip at 0x6fffe0ab948>
zip(a, b)用法:元素对
打印键值对
#以列表形式 打印键值对 a = {'a':1,'b':2,'c':3} b = [k+ '=' + str(v) for k, v in a.items()] print(b) output: ['a=1', 'b=2', 'c=3']
文件列表
import os # 设置目标路径 dirs = [d for d in os.listdir('/home/sage/PYTHON60DAYS') if os.path.isdir(d)]#找文件夹 files = [d for d in os.listdir('/home/sage/PYTHON60DAYS') if os.path.isfile(d)]#找文件 print(dirs) print(files) output: ['.ipynb_checkpoints'] ['day121314.ipynb', 'day15.ipynb', 'day16.ipynb', 'day5-Copy1.ipynb', 'day5.ipynb']
转为小写
a = ['Hello', 'World', '2019Python'] b = [_.lower() for _ in a] print(b) output: ['hello', 'world', '2019python']
但是如果由于列表中元素并不是都是str,比如说有数值类型,就会报错,要么就是先转化为str,要么就是使用sinstance,判断元素是否为 str 类型,如果是,再调用 lower 做转化:
a = ['Hello', 'World', '2019Python', 2020, 'asdaASD'] c = [w.lower() for w in a if isinstance(w,str) ] print(c) output: ['hello', 'world', '2019python', 'asdaasd']
上面显然不是str类型就不在转化出来的[]中了
保留唯一值
# 保留唯一值,那些列表中只出现过一次的值 list.count(item) def filter_non_unique(lst): return [item for item in lst if lst.count(item) == 1] filter_non_unique([1, 2, 2, 3, 4, 4, 5]) output: [1, 3, 5]
筛选分组
#筛选分组 利用某种属性进行分组 def bifurcate(lst, filter): return [ [x for i,x in enumerate(lst) if filter[i] == True], [x for i,x in enumerate(lst) if filter[i] == False] ] bifurcate(['beep', 'boop', 'foo', 'bar'], [True, True, False, True]) output: [['beep', 'boop', 'bar'], ['foo']]
函数分组
#利用函数运行结果分组 fn是函数 lambda x: x[0] == 'u'就是一个函数,返回true or false def bifurcate_by(lst, fn): return [ [x for x in lst if fn(x)], [x for x in lst if not fn(x)] ] bifurcate_by(['Python3', 'up', 'users', 'people'], lambda x: x[0] == 'u') output: [['up', 'users'], ['Python3', 'people']]
差集
#差集 a b 为lst def difference(a, b): _a, _b =set(a),set(b) return [item for item in _a if item not in _b]#返回a中那些没有出现在b中的元素 difference([1,1,2,3,3,5,15], [1, 2, 4,15]) output: [3, 5]