是它更有效的檢查,如果一個項目已經在列表中添加它之前:列表成員測試或設置
for word in open('book.txt','r').read().split():
if word in list:
pass
else:
list.append(item)
或在其上添加的一切,然後運行set()?像這樣:
for word in open('book.txt','r').read().split():
list.append(word)
list = set(list)
你想只有獨特的元素?如果否,請在此停止,否則,訂單是否重要? – thefourtheye
''set'應該像這樣構造:'listobject = set(listobject)' – thefourtheye
不要使用'list'作爲變量名,它會影響內建列表函數 – thefourtheye