我認爲你的問題讓很多人感到困惑,因爲你在dict和list之間混合了你的語法。 如果:
L = [] # L is synonym for list and [] (braces) used to create list()
在這裏,你正在尋找一個列表中的值,不的關鍵也不在一個字典值:
if x in L:
然後使用X看似意一個鍵,但在列表中它是一個int()索引,並且if x in L:
不測試以查看索引是否在L中,但是如果值在L中:
L[x]=value
所以,如果你打算看一個值是L
列表做:
L = [] # that's a list and empty; and x will NEVER be in an empty list.
if x in L: # that looks for value in list; not index in list
# to test for an index in a list do if len(L)>=x
idx = L.index(x)
L[idx] = something # that makes L[index]=value not L[key]=value
else:
# x is not in L so you either append it or append something_else
L.append(x)
如果你使用: L[x] = something
與if x in L:
在一起,然後它將使意義有一個列表,只有這些值:L=[ 0, 1, 2, 3, 4, ...]
OR L=[ 1.0, 2.0, 3.0, ...]
但是我提供這樣的:
L = []
L.extend(my_iterable)
coder0 = 'farr'
coder1 = 'Mark Byers'
if coder0 not in L:
L.append(coder1)
奇怪的邏輯
您正在使用字典,它們沒有索引或像列表一樣排序。 – birryree
標題說明名單,我認爲字典是一個錯字 –
你的問題有一些字典的意義。現在它只是毫無意義... – JBernardo