[英]How do I check to see if all of the items in one list are in a second list in python?
[英]How a search one list and see if all those items are in second list and then print (if any) all the missing elements and how many there are?
我目前正在學習python。 我想知道如何比較兩個列表。 我有兩個清單:
days = ['sunday', 'monday', 'tuesday', 'wednesday', 'friday']
new_days = ['weekend', 'wednesday', 'saturday', 'sunday', 'monday','thursday']
我怎么寫,我假設有一個循環,一個代碼來查看天數列表中的所有元素是否都在 new_days 列表中,然后天數列表中是否有任何缺失的元素不在 new_days 列表中打印找出丟失的數量並打印出丟失的數量? 我會使用 all() 函數來比較兩者嗎?
如果您只是想找出兩個列表之間的差異,可以使用以下代碼:
days = ['sunday', 'monday', 'tuesday', 'wednesday', 'friday']
new_days = ['weekend', 'wednesday', 'saturday', 'sunday', 'monday','thursday']
difference = list(set(days) - set(new_days))
這有什么幫助
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.