擅长:python、mysql、java
<p>Python可以很好地比较两个列表:</p>
<pre><code>a = ["Shelby", "Dodge HellCat", "Nissian GT-R R34", "Ford Focus RS", "Corvette ZR1"]
b = ["Shelby", "Dodge HellCat", "Nissian GT-R R34", "Ford Focus RS", "Corvette ZR1"]
if a == b:
print("They are the same")
else:
print("They are different")
# Outputs: They are the same
</code></pre>
<p>但是,如果它们的顺序不一致,这将不起作用:</p>
<pre><code>a = ["Shelby", "Dodge HellCat", "Nissian GT-R R34", "Ford Focus RS", "Corvette ZR1"]
b = ["Dodge HellCat", "Shelby", "Nissian GT-R R34", "Ford Focus RS", "Corvette ZR1"]
if a == b:
print("They are the same")
else:
print("They are different")
# Outputs: They are different
</code></pre>
<p>根据需求,似乎需要检查两个列表中是否都存在所有元素,但不一定按顺序。最简单的方法可能是使用两个<code>set</code>,但是似乎需要使用循环。你知道吗</p>
<p>相反,可以尝试使用<code>not in</code>关键字来检查汽车是否存在。例如:</p>
<pre><code>for car in their_cars:
if car not in my_cars:
print("Oh no, a car that's not in my list")
</code></pre>
<p>警告,这不会是完整的。假设有人进入<code>"Shelby"</code>5次,它就会通过。所以你得倒过来检查一下。你知道吗</p>