列表相當於項目我有兩個列表符號在Python中
circles_plotted = [1]
circles_next_plotted = [1]
數字被附加到每個圓圈繪製時間列出circles_plotted
。但是,每個列表中的正確標記和項目是否相等? (這將是一個if語句):
(item in circles_next_plotted)==(item in circles_plotted)
列表相當於項目我有兩個列表符號在Python中
circles_plotted = [1]
circles_next_plotted = [1]
數字被附加到每個圓圈繪製時間列出circles_plotted
。但是,每個列表中的正確標記和項目是否相等? (這將是一個if語句):
(item in circles_next_plotted)==(item in circles_plotted)
的符號不正確。
(item in circles_next_plotted)==(item in circles_plotted)
是Python中NameError
:name 'item' is not defined
。
你可能的意思是
>>> (item for item in circles_next_plotted)==(item for item in circles_plotted)
False
這是因爲(item for item in circles_next_plotted)
是發電機的表達和兩臺發電機是兩個不同的對象,因此不相等。
>>> (item for item in circles_next_plotted)
<generator object <genexpr> at 0x000000D50AF46BF8>
>>> (item for item in circles_plotted)
<generator object <genexpr> at 0x000000D50AF46CA8>
比較列表,你應該使用與==
運營商已經實施了名單做了每個項目的比較,而不是一個對象身份檢查一個簡單的比較:
>>> circles_next_plotted==circles_plotted
True
要檢查是否名單是相等的,你可以使用==
。如果要素的位置不同,你可以檢查是否分類列表是平等的:
li1 = [1, 2, 3]
li2 = [1, 2, 3]
print(li1 == li2) # True
li3 = [3, 2, 1]
li4 = [1, 2, 3]
print(li3 == li4) # False
print(sorted(li3) == sorted(li4)) # True
你也可以檢查與LI3和LI4元素的多集是否相等:
import collections
li3 = [3, 2, 1]
li4 = [1, 2, 3]
print(collections.Counter(li3) == collections.Counter(li4)) # True
只是做'circles_plotted == circles_next_plotted',Python將在內部處理淺層比較。 – thefourtheye
@thefourtheye當'circles_plotted'中有多個值時,這仍然有效,因此列表中的所有值都不相等嗎? – lyche