if len(set([rc1, rc2, rc3, rc4, rc5])) > 1:
print("Error")
更新:如果您需要检查它们中的n是否完全相同,则类似的操作将起作用:
items = [rc1, rc2, rc3, rc4, rc5]
n = 4
if any(items.count(item) == n for item in items):
print("{} of them are the same, {} is different".format(n, len(items) - n))
或者您可以实际计算重复次数最多的元素:
max_repeat = max(items.count(item) for item in items)
print("{} of them are the same".format(max_repeat))
您的问题与标题不一致(“正好4”或“至少4”?)但如果不是所有的都相同,这将打印一个错误:
更新:如果您需要检查它们中的n是否完全相同,则类似的操作将起作用:
或者您可以实际计算重复次数最多的元素:
这是字典的经典用例:
由于列表的大小为5,这相当于检查列表中的第一项或第二项是否正好出现4次。您可以使用
list.count
两次:相关问题 更多 >
编程相关推荐