!我是不是最后一个知道这种用法的-_-|| ?
字典:In [1]: arr = [1,2,3,4,5] In [2]: {i: i*2 for i in arr} Out[2]: {1: 2, 2: 4, 3: 6, 4: 8, 5: 10}列表:
In [3]: [2*i for i in arr] Out[3]: [2, 4, 6, 8, 10]还可以再增加if判定
In [1]: arr = [1,2,3,4,5] In [2]: {i: i*2 for i in arr if i != 4} Out[2]: {1: 2, 2: 4, 3: 6, 5: 10}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)