【问题标题】:Is there any way to get x in "x for y in x for z"有什么方法可以在“x for y in x for z”中获得 x
【发布时间】:2021-08-07 15:43:03
【问题描述】:

例如下面的代码:

dict = {
"foo": "a",
"bar": "b",
"see": "c",
}

string = "foo in the city"

if any(x in string for x in dict):
    print(dict[x])

有没有办法得到x? (或者通过其他方法获取dict[x]?)

【问题讨论】:

标签: python


【解决方案1】:

在给出答案之前有几点需要纠正:

  1. any() 只能用于可迭代对象,如列表、元组或字典 - 因此在您定义的条件内返回的对象将不会被传递,并且对于 any() 将无法识别

  2. List Comprehensions 是一种处理可迭代对象的值并以列表形式返回所需值的惊人方法,可以根据您的需要进一步修改或打印该列表

继续原来的解决方案:

dict = {
"foo": "a",
"bar": "b",
"see": "c",
}

string = "foo in the city"
list_final = [x for x in dict if x in string]

一个好的方法是以列表元素的形式获取输出(如果有多个元素:

print(list_final)

如果您希望将其作为单个元素值(作为字符串) - 极少数情况,例如这里的情况:

print(list_final[0])

【讨论】:

    【解决方案2】:

    只需将“x in string”放在括号内即可。

    所以你会得到

    dict = {
      "foo": "a",
      "bar": "b",
      "see": "c",
    }
    
    string = "foo in the city"
    
    if any((x in string) for x in dict):
        print(dict[x])
    

    【讨论】:

    • 请注意,这只会输出满足条件的最后一个值,而不是全部。
    【解决方案3】:
    dict = {
    "foo": "a",
    "bar": "b",
    "see": "c",
    }
    
    string = "foo in the city"
    
    print([dict[x] for x in string.split() if x in dict])
    
    # Output:
    # ['a']
    

    【讨论】:

    • 不要使用保留关键字作为dict
    • 保留函数
    • 感谢您提供答案。您能否编辑您的答案以包括对您的代码的解释?这将帮助未来的读者更好地理解正在发生的事情,尤其是那些不熟悉该语言并努力理解这些概念的社区成员。解释这与此处的其他六个答案有何不同以及在什么情况下首选您的方法也是有用的。
    【解决方案4】:

    尚不清楚您要达到的目标。 您正在寻找的解决方案可能是:

    res = [x  for x in dict if x in string]
    

    [print(x) for x in dict if x in string]
    

    但最后一个它不是使用列表理解的好方法。

    附言您不应该使用 dict 作为字典的名称

    【讨论】:

      【解决方案5】:

      假设您想要获取所有键值在字符串中的值,您可以执行以下操作:

      mydict = {
      "foo": "a",
      "bar": "b",
      "see": "c",
      }
      
      string = "foo in the city"
      
      matches = [x for x in mydict if x in string]
      
      for match in matches:
          print(mydict[match])
      

      Try it online!

      【讨论】:

        【解决方案6】:

        根据您的需要。我想这就是你想要的。

        dict = {
        "foo": "a",
        "bar": "b",
        "see": "c",
        }
        
        s = "foo in the city"
        
        result = [dict[x] for x in s.split() if dict.get(x,0) != 0]
        print(result)
        

        如果您只想打印

        [print(dict[x]) for x in s.split() if dict.get(x,0) != 0]
        

        输出:

          a
        

        【讨论】:

          【解决方案7】:

          我想你想检查string 中的任何单词(请不要将变量命名为它们的类型)是否在字典中?如果是这样,也许这会有所帮助:

          # make it a list
          words = string.split(' ')
          
          x = 'foo'
          
          if any(x in words for x in dict):
              print(dict[x])
          
          # output:
          # a
          

          【讨论】:

            猜你喜欢
            • 2013-11-29
            • 1970-01-01
            • 2021-03-20
            • 1970-01-01
            • 2011-03-21
            • 1970-01-01
            • 2013-02-26
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多