【问题标题】:Find element in list that contains unicodes在列表中查找包含 unicode 的元素
【发布时间】:2017-10-07 18:15:01
【问题描述】:

我有以下列表,我想知道元素“caston”是否在列表中。

a = [{u'us': u'Running', u'HalfTima': u'1234', u'Boks': u'caston', u'KickoffTime': 1507401900000L, u'AwayName': u'jose'}]

我尝试过以下 foudcodes 但它不起作用,因为我总是得到 False 而不是 True:

print type(a)

print ("'caston'" in a)

print ( "u'caston'" in a)

print (caston in a)

print ("caston" in a)

你能告诉我正确的方法是什么吗?

谢谢。

【问题讨论】:

  • 您在列表中有一个字典,所以您实际上是在寻找字典中的值。

标签: python list unicode find


【解决方案1】:

你可以试试这个:

a = [{u'us': u'Running', u'HalfTima': u'1234', u'Boks': u'caston', u'KickoffTime': 1507401900000L, u'AwayName': u'jose'}]
print("caston" in a[0].values())

输出:

True

【讨论】:

    【解决方案2】:

    使用str函数list可以转换成字符串,然后就可以搜索到需要的字符串了。

    例如

    >>> a = [{u'us': u'Running', u'HalfTima': u'1234', u'Boks': u'caston', u'KickoffTime': 1507401900000L, u'AwayName': u'jose'}]
    >>> 'caston' in str(a)
    True
    >>>
    

    【讨论】:

      【解决方案3】:

      如果您only 想检查元素“caston”是否在列表中,那么您可以使用正则表达式进行检查:

      import re
      a=r"[{u'us': u'Running', u'HalfTima': u'1234', u'Boks': u'caston', u'KickoffTime': 1507401900000L, u'AwayName': u'jose'}]"
      
      pattern=r'\bcaston\b'
      
      match=re.search(pattern,a)
      if match:
          print(True)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-04-09
        • 2012-03-20
        • 1970-01-01
        • 2019-11-06
        • 2016-09-16
        • 2014-07-31
        • 2013-04-17
        • 1970-01-01
        相关资源
        最近更新 更多