我想從列表中打印前 10 個不同的元素:top=10test=[1,1,1,2,3,4,5,6,7,8,9,10,11,12,13]for i in range(0,top): if test[i]==1: top=top+1 else: print(test[i])它正在打?。?,3,4,5,6,7,8我期待:2,3,4,5,6,7,8,9,10,11我缺少什么?
3 回答

紅糖糍粑
TA貢獻1815條經驗 獲得超6個贊
使用 numpy
import numpy as np
top=10
test=[1,1,1,2,3,4,5,6,7,8,9,10,11,12,13]
test=np.unique(np.array(test))
test[test!=1][:top]
輸出
array([ 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])

大話西游666
TA貢獻1817條經驗 獲得超14個贊
我的解決方案
test = [1,1,1,2,3,4,5,6,7,8,9,10,11,12,13]
uniqueList = [num for num in set(test)] #creates a list of unique characters [1,2,3,4,5,6,7,8,9,10,11,12,13]
for num in range(0,11):
if uniqueList[num] != 1: #skips one, since you wanted to start with two
print(uniqueList[num])
添加回答
舉報
0/150
提交
取消