Closed. This question needs details or clarity. It is not currently accepting answers. Learn more。
想改进这个问题吗?添加细节并通过editing this post澄清问题。
如果有人问过,请原谅,但我找不到答案。
我想把这两对分别列在两张单子上。每对都包括重复的(这就是为什么我认为集合不起作用)例如:
fun(A, B):
return count
if A = [1 1 0 0] and B = [1 2 2 1]
returns 2 and not 1
if A = [1 1 0 0] and B = [1 3 3 3]
returns 1
if A = [1 2 2 0] and B = [1 3 2 2]
returns 3
特别是我做了一个主谋类型的游戏,这是为了计算正确的颜色错误点总数(在正确的颜色正确点从列表中删除后)
这是我现在有的,但看起来不是很蟒蛇。
def count_func(A,B)
count = 0
for b in reversed(B):
used = False
for a in reversed(A):
if b == a and not used:
A.remove(b) #remove that from list
B.remove(b)
count += 1
used = True
return count
顺便说一下,我正在使用python 3。谢谢你的帮助,如果这已经回答之前让我知道。
编辑:
纠正了第二个例子并试图澄清
最佳答案
您可以删除布尔值“used”和字符串的反转:
def count_func(A,B):
count = 0
for b in B:
for a in A:
if b == a:
A.remove(a)
count += 1
break
return count
关于python - python从两个单独的列表中计数对,但列表中没有唯一值(我认为集合不起作用),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49061953/