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

Python 如何检查第三个列表中是否存在两个列表元素的组合?,python,arrays,tuples,Python,Arrays,Tuples,我有一个元组列表,看起来像这样 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),] temp_list = [] for i in dealer: print("i", i) for j in loc:

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

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),]
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)
下面还有两个列表

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

dealer = ["ee", "dd", "ab", "cc", "bb"]
现在,对于
dealer
的每个元素,我想要一个
loc

因此,由于
dealer
中有5个元素,我将列出五个列表,其中包含
loc
中每个对应元素的值

差不多

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

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

因此我们可以看到上面的
ee
检查
noi
,在
值中找不到任何值,因此分配了一个值。然后它检查
del
,找不到任何值,因此分配了一个值,然后它检查
cal
,找不到任何值,分配了一个值,但对于
hyd
它找到了13.11,因此分配了值

同样地,
对于
dd
[10.49,无,4.99,无]
等等

如何获得经销商五大要素的五个列表

我试过这样做

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),]
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(数据)*len(经销商)*len(位置))。我们可以在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]}

使用更好的数据结构

假设
数据的前两个元素中没有任何两个元素是相等的,您可以使用以下字典使您的生活更轻松:

>>> 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]]
。。。或者,如果您需要一个
命令

>>> {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]}

请给我们完整的预期输出。@timgeb添加了预期输出。
'ab'
的预期输出有错误,请看我的答案。我想第三行应该是
[None,12.99,None,10.99]
?@timgeb是的,这将是
10.99
我的错!嘿,
d
表示数据吗?@SouvikRay
d
data
中的数据表示为字典。