【问题标题】:How to compare several unordered list and get the list names if there are matching values如果有匹配的值,如何比较几个无序列表并获取列表名称
【发布时间】:2022-01-01 08:59:34
【问题描述】:

我有几个包含值的列表。就像下面一样。

Alc=['P-111127759','111157751','1123104714']
FItems=['1123104714','797917266','79791761','79791765','79791763']
kuVItem=['1110234713','161231437','756623557','1123104714','7630672177', '754955924','712969245','963176673','181104711']
Products=['7171523315','Pqw-805967111','1150596117','115596116']
WProducts=['PQ-4559678','7171523315','5505296117','755961146']

如果列表中有任何相似的值,我基本上想找出列表名称。 例如,Alc、FItems 和 kuVItem 列表具有相同的值“1123104714”。所以我基本上希望这些名字成对出现。我的意思是这样的

['Alc', 'FItems'],['Alc', 'kuVItem'],['FItems','kuVItem']

有没有办法在 python 中做到这一点?

提前致谢!任何帮助表示赞赏!

【问题讨论】:

  • 列表是否必须来自 python 变量?是什么在以这种方式创造它们?可以改成使用字典吗?
  • 名称每次都如何以及为什么会发生变化?如果您不知道列表的名称,那么您将如何引用它们来比较它们?
  • 我不知道列表是否是正确的结构。你会每次都重写代码吗?您可以使用字典,其中键是列表名称,值是值列表。从可用性的角度来看会更有意义,并且解决这个问题会更简单。
  • 是的,可以改成Dic。

标签: python pandas list


【解决方案1】:

复制的完整代码

import itertools
dict_of_lists = {
    'a' : [1,3],
    'b' : [3,4],
    'c' : [3,6]
    }

def common_data(list1, list2):
    result = False

    # traverse in the 1st list
    for x in list1:

    # traverse in the 2nd list
        for y in list2:

            # if one common
            if x == y:
                result = True
                return result

    return result

result_list = list(map(dict, itertools.combinations(list_of_lists.items(), 2)))
print(result_list)
pairs_commons_elements = []

for element in result_list:

    if(common_data(list_of_lists[list(element.keys())[0]],list_of_lists[list(element.keys())[1]])):
        pairs_common_elements.append(list(element.keys()))

print(pairs_common_elements)

说明

首先,我会将您的所有列表放在一个列表中,我这样做是为了获得更通用的答案,因为您的具体问题的答案已经给出。

dict_of_lists = {
'a' : [1,3],
'b' : [3,4],
'c' : [3,6]
}

我们定义“公共数据”来检查两个列表是否有任何共同的元素:

def common_data(list1, list2):
    result = False

    # traverse in the 1st list
    for x in list1:

    # traverse in the 2nd list
        for y in list2:

            # if one common
            if x == y:
                result = True
                return result

    return result

为了能够使用这个功能,我们需要首先列出每对可能的字典键(itertools.combinations() 管理这个):

result_list = list(map(dict, itertools.combinations(list_of_lists.items(), 2)))

最后,我们遍历每一个可能的对并检查它们是否有任何共同的元素。如果有,我们将它们添加到pairsCommonElements 列表中。

pairs_common_elements = []
    
    if(common_data(list_of_lists[list(element.keys())[0]],list_of_lists[list(element.keys())[1]])):
        pairs_common_elements.append(list(element.keys()))

print(pairs_common_elements)

这会输出[['a', 'b'], ['a', 'c'], ['b', 'c']]

【讨论】:

    【解决方案2】:

    我假设这些值将通过字典提供(对于名称经常更改的命名值,这是我能想到的唯一方法)。

    然后只需遍历键,并将每个键的值与其他条目进行比较。因为我们只想知道是否有重叠,set.intersection() 是一个很好的工具。

    我的代码最终是:

    my_dict = {
      'Alc':['P-111127759','111157751','1123104714'],
      'FItems':['1123104714','797917266','79791761','79791765','79791763'],
      'kuVItem':['1110234713','161231437','756623557','1123104714','7630672177', '754955924','712969245','963176673','181104711'],
      'Products':['7171523315','Pqw-805967111','1150596117','115596116'],
      'WProducts':['PQ-4559678','7171523315','5505296117','755961146']}
    
    
    results = []
    for key in my_dict:
      for key2 in my_dict:
        if key != key2:
          intersection = set.intersection(set(my_dict[key]), set(my_dict[key2]))
          if len(intersection) > 0:
            if [key2, key] not in results and [key, key2] not in results:
              results.append([key, key2])
    
    print(results)
    

    哪个输出[['Alc', 'FItems'], ['Alc', 'kuVItem'], ['FItems', 'kuVItem'], ['Products', 'WProducts']]

    【讨论】:

    • 感谢您的快速回复!欣赏!
    猜你喜欢
    • 2020-07-06
    • 1970-01-01
    • 1970-01-01
    • 2020-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多