list的相关操作
http://www.runoob.com/python/python-lists.html
找list中重复出现的元素的index
labels = [1,2,1,2,0,0,2]
id = [i for i,x in enumerate(labels) if x==1]
id
结果为[4, 5]
https://blog.csdn.net/t8116189520/article/details/84395319
g =[-0.4310240649520269,
-0.3027811144738707,
-0.2735361649907948,
0.16473515620977836,
-0.19215072108337972,
0.36197685132974006,
0.6727800579605527]
l = [i for i in range(len(g))] #即l = [0,1,2,3,4,5,6]
l.sort(key = lambda x: g[x])
print(l)
#结果为[0,1,2,4,3,5,6]
按g的顺序,对l中的元素排序
上述用法总结为:key=lambda 元素: 元素[字段索引]
python3排序 sorted(key=lambda)
https://blog.csdn.net/liudsl/article/details/79980625
python list的两种排序方法(sort和sorted方法)
https://blog.csdn.net/weixin_37923128/article/details/80970466
Python几种创建list的方法的效率对比
https://www.cnblogs.com/PythonChan/p/6213675.html
首先这种方式复杂度为平方级
def test1(n):
lst = []
for i in range(n*10000):
lst = lst + [i]
return lst
接下来这几种,时间复杂度都是线性级的
def test2(n):
lst = []
for i in range(n*10000):
lst.append(i)
return lst
def test3(n):
return [i for i in range(n*10000)]
def test4(n):
return list(range(n*10000))
速度对比:
test4>test3>test2
直接用系统list方法是最快的,然后是列表推导,用list类的append方法排后。
一个list中去掉一些元素后剩下的元素:
ls = [i for i in range(10)]
lst_1 = random.sample(ls, len(ls)//2)
lst_2 = [i for i in ls if i not in lst_1]
print(ls)
print(lst_1)
print(lst_2)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[4, 5, 9, 3, 0]
[1, 2, 6, 7, 8]