在3个不同长度的列表中查找常见字符串值;If语句包含3个条件



我有三个长列表,例如:

dates_a = [20/07/2022, 21/07/2022, 22/07/2022, ... , 02/08/2022] -> 300 string objects in total
dates_b = [18/02/2021, 05/05/2021, 22/06/2022, ... , 21/07,2022] -> 200+ string objects in total
dates_c = [01/02/2022, 01/04/2022, 01/06/2022, ... , 01/08/2022] -> 100+ string objects in total

我试着用来解决这个问题

for i in range(len(dates_a), len(dates_b), len(dates_c)):
if dates_a[i] == dates_b[i] and (dates_b[i] == dates_c[i] and dates_a[i] == dates_c[i]):
list_of_dates.append(dates_a[i])
else:
print("not")
print(list_of_dates)

有什么更简单的方法吗?目前,由于list_of_dates返回一个空列表,此代码也失败了。

将它们转换为集合,这样您就可以获取交集,然后在需要时将结果返回到列表中。

list_of_dates = list(set(dates_a) & set(dates_b) & set(dates_c))

最新更新