繁体   English   中英

如何检查第三个列表中是否存在两个列表的元素组合?

[英]How do I check if combination of elements of two lists exists in a third list?

我有一个看起来像这样的元组列表

data = [("hyd", "ab", 10.99), ("del", "ab", 12.99), ("del", "cc", 10.19), ("cal", "dd", 4.99), ("hyd", "ee", 13.11), ("noi", "dd", 10.49), ("noi", "bb", 10.99),]

我在下面还有另外两个列表

loc = ["noi", "del", "cal", "hyd"]

dealer = ["ee", "dd", "ab", "cc", "bb"]

现在,对于dealer每个元素,我想要一个loc每个对应元素的值列表

因此,由于dealer有5个元素,因此我将为loc每个对应元素提供5个带有值的列表。

就像是

对于ee ,它将再次检查loc列表的每个元素,并从数据中找出loc每个元素包含的值

对于ee [None, None, None, 13.11]

因此我们可以看到ee上面检查了noi ,在分配给None的values什么都没有找到,然后它检查了del ,没有找到分配了None的东西,然后它对cal进行了检查,发现了什么,分配了None,但对于hyd却找到了13.11并因此分配了价值。

同样的,
对于dd [10.49, None, 4.99, None] ,依此类推...

如何获得针对经销商五个要素的五个清单?

我试图做这样的事情

temp_list = []
for i in dealer:
   print("i", i)
   for j in loc:
      print("j", j)
      for k in data:
         #print("k", k)
         if i in k and j in k:
            temp_list.append(k[2])
         else:
            temp_list.append(None)

但是我没有得到预期的输出。 我如何获得清单?

完成预期的输出

ee [None, None, None, 13.11]
dd [10.49, None, 4.99, None]
ab [None, 12.99, None, 10.99]
cc [None, 10.99, None, None]
bb [10.99, None, None, None]

您可以以更高效的方式进行操作。 您的解决方案是O(len(data)* len(dealers)* len(locations))。 我们可以在O(len(data))中通过仅对数据进行一次迭代来完成:

data = [("hyd", "ab", 10.99), ("del", "ab", 12.99), ("del", "cc", 10.19), ("cal", "dd", 4.99), ("hyd", "ee", 13.11), ("noi", "dd", 10.49), ("noi", "bb", 10.99),]
locations = ["noi", "del", "cal", "hyd"]
dealers = ["ee", "dd", "ab", "cc", "bb"]


out = {dealer: [None] * len(loc) for dealer in dealers} 
loc_index = {val: index for index, val in enumerate(locations)}

for location, dealer, amount in data:
    try:
        out[dealer][loc_index[location]] = amount
    except (IndexError, KeyError):
        pass

print(out)
# {'ee': [None, None, None, 13.11], 'cc': [None, 10.19, None, None], 
# 'dd': [10.49, None, 4.99, None], 'ab': [None, 12.99, None, 10.99], 
# 'bb': [10.99, None, None, None]}

使用更好的数据结构!

假设data两个元素在前两个元素中不能相等,那么可以使用以下字典来简化生活:

>>> from collections import defaultdict
>>> 
>>> data = [("hyd", "ab", 10.99), ("del", "ab", 12.99), ("del", "cc", 10.19), ("cal", "dd", 4.99), ("hyd", "ee", 13.11), ("noi", "dd", 10.49), ("noi", "bb", 10.99),]
>>> d = defaultdict(dict)
>>> 
>>> for key, subkey, val in data:
...:    d[key][subkey] = val
...:    
>>> d
>>> 
defaultdict(dict,
            {'cal': {'dd': 4.99},
             'del': {'ab': 12.99, 'cc': 10.19},
             'hyd': {'ab': 10.99, 'ee': 13.11},
             'noi': {'bb': 10.99, 'dd': 10.49}})

...因为现在您可以执行以下操作:

>>> loc = ["noi", "del", "cal", "hyd"]
>>> dealer = ["ee", "dd", "ab", "cc", "bb"]
>>> 
>>> [[d[lo].get(deal) for lo in loc] for deal in dealer]
>>> 
[[None, None, None, 13.11],
 [10.49, None, 4.99, None],
 [None, 12.99, None, 10.99],
 [None, 10.19, None, None],
 [10.99, None, None, None]]

...或者如果您想要一个dict

>>> {deal:[d[lo].get(deal) for lo in loc] for deal in dealer}
>>> 
{'ab': [None, 12.99, None, 10.99],
 'bb': [10.99, None, None, None],
 'cc': [None, 10.19, None, None],
 'dd': [10.49, None, 4.99, None],
 'ee': [None, None, None, 13.11]}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM