我有一本单子字典。每个列表都包含许多数字。列表可以有不同的长度。我希望找到所有列表中的第一个公共编号。答案必须是一个函数。你知道吗
例如,在这种情况下:
d = { 'case1' : [18, 17, 497, 298, 57, 82, 1], 'case2': [128, 184, 497,298,57, 1, 82], 'case3':[104,2828,3881, 497, 1, 38], 'case4': [392, 497, 573, 1]}
预期产量为:497。我不想抓一个。我只找497。你知道吗
我现在只有这个:
def find_first_common_number(dictionary_of_lists):
for list_name in dictionary_of_lists: #list name e.g. 'case1'
for num1 in dictionary_of_lists[list_name]:
#this is where I am going to have to find the first common
# number from all lists in the dictionary
# the order this number appears in each list DOES NOT matter
我很感激你在这方面的帮助。我浏览了列表方法,做不了什么,因为我对Python还不熟悉。如果你能解释/评论你的方法,那就太好了。你知道吗
您可以做的一件事就是与&;运算符进行一组比较
DOCS
如果你想在函数中使用它,你可以这样做。你知道吗
然而,我坚决支持没有“第一”项的观点。可能有很多用例存在重叠的“第一个”,这是毫无意义的
我可以这样试一下吗:
输出: 497个
相关问题 更多 >
编程相关推荐