【问题标题】:Find similar list value inside dictionary在字典中查找相似的列表值
【发布时间】:2019-02-14 10:07:22
【问题描述】:

我正在尝试从我的列表中找到与字典值中的列表相匹配的匹配项

例如字典包含

dict = {"test1": [1, 2, 3, 4], 
        "test2": [2, 2, 3, 4], 
        "test3": [1, 2, 4, 5], 
        "test4": [6, 2, 3, 4], 
        "test5": [7, 2, 3, 4]}

以及我需要为 if 找到匹配项的数据

answer = [6,2,3,4]

我正在尝试提取答案输入的第一个值必须不同而其余相同的任何测试,例如

[(this is different), 2,3,4]

那么最后我想记录test1test2test5

【问题讨论】:

  • 你能分享你写的没有产生你想要的输出的代码吗?
  • "所以最后我想记录 test1、test2 和 test 5。" - 你所说的“成为”是什么意思录”?我不清楚你想要达到什么目的。

标签: python python-3.x dictionary


【解决方案1】:

假设您有一个包含字符串键的有效字典,使用列表推导可以过滤掉不需要的键,如下所示:

>>> d = {
...     "test1": [1, 2, 3, 4],
...     "test2": [2, 2, 3, 4],
...     "test3": [1, 2, 4, 5],
...     "test4": [6, 2, 3, 4],
...     "test5": [7, 2, 3, 4]
... }
>>> answer = [6, 2, 3, 4]
>>> [k for k, v in d.items() if v[0] != answer[0] and v[1:] == answer[1:]]
['test1', 'test2', 'test5']

请注意,dict = ... 会覆盖内置函数 dict,这不是一个好主意。

【讨论】:

    【解决方案2】:

    请注意,这是您应该考虑将其抽象为函数的类型。

    def match_records(a, b):
        return a[-3:] == b[-3:] and a[0] != b[0]
    
    results = {k:v for k, v in d.items() if match_records(answers, v)}
    

    【讨论】:

    • 这甚至会返回测试 4,但事实并非如此。你的条件不够。你应该包含一个条件来检查第一个数字不应该是相同的
    • @Onyambu 哦,我错过了那个条件。
    【解决方案3】:

    解决方案

    dict_a = { 
        'test 1': [1, 2, 3, 4], 'test 2': [2, 2, 3, 4], 'test 3': [1, 2, 4, 5], 
        'test 4': [6, 2, 3, 4], 'test 5': [7, 2, 3, 4]
    }
    
    answer = [6, 2, 3, 4]
    
    for i in dict_a:
        if answer[0] != dict_a[i][0]:
            if answer[1:4] == dict_a[i][1:4]:
                print(dict_a[i])
    

    存储输出

    answer = [6, 2, 3, 4]
    store = {}
    
    for i in dict_a:
        if answer[0] != dict_a[i][0]:
            if answer[1:4] == dict_a[i][1:4]:
                store[i] = dict_a[i]
    
    for k in store:
        print(f"{k}: {store[k]}")
    

    输出

    (xenial)vash@localhost:~/python/AtBS$ python3.7 list_dict.py 
    [1, 2, 3, 4]
    [2, 2, 3, 4]
    [7, 2, 3, 4]
    

    【讨论】:

      【解决方案4】:

      我认为您的代码存在几个问题。 首先,由于 dict 是关键字,因此应避免将其作为变量名。 其次,未定义变量 test1、test2 等。在这种情况下,最好使用'test1','test2'等

      无论如何,请看下面的答案。

                  dic = {'test1':[1, 2, 3, 4], 'test2':[2, 2, 3, 4], 'test3':[1,2,4,5], 
                      'test4':[6,2,3,4], 'test5':[7, 2, 3,4]}
      
      
                  toMatch = [6,2,3,4]
      
                  matching = []
                  for (key, value) in dic.items():
                      if((value[1:]==toMatch[1:])&(value[0]!=toMatch[0])):
                          print(key,value)
                          matching.append(key)
      
                  print('Matching keys are:', matching)
      

      答案是:

      test1 [1, 2, 3, 4]
      test2 [2, 2, 3, 4]
      test5 [7, 2, 3, 4]
      Matching keys are: ['test1', 'test2', 'test5']
      

      【讨论】:

        【解决方案5】:

        您可以遍历 dict 的项目并将子列表的切片与您要检查的列表的切片进行比较:

        d = {'test1':[1, 2, 3, 4], 'test2':[2, 2, 3, 4], 'test3':[1,2,4,5], 'test4':[6,2,3,4], 'test5':[7, 2, 3,4]}
        def check(d, l):
            for k, v in d.items():
                if v != l and v[1:] == l[1:]:
                    yield k
        print(list(check(d, [6, 2, 3, 4])))
        

        这个输出:

        ['test1', 'test2', 'test5']
        

        【讨论】:

          猜你喜欢
          • 2021-08-24
          • 2019-01-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-01-22
          • 1970-01-01
          • 2014-10-06
          相关资源
          最近更新 更多