Skip to main content
 首页 » 编程设计

python之检查项目符号点是否在列表中

2025年05月04日17lovecherry

因此,我试图通过使用 for 循环遍历项目符号来检查项目符号点是否是列表中项目的一部分。我知道,至少在 Regex 中,项目符号点定义为 \u2022。但是不知道怎么用这个。我目前拥有但显然不起作用的是这样的。

list = ['changing. • 5.0 oz.', 'hello', 'dfd','df', 'changing. • 5.0 oz.'] 
for items in list: 
     if "\u2022" in items: 
        print('yay') 

提前致谢!

请您参考如下方法:

在Python 3 your code will work fine because UTF-8 is the default source code encoding .如果您要经常使用 Unicode,请考虑切换到 Python 3。

在Python 2, the default is to treat literal strings as sequences of bytes , 因此您必须通过在字符串前加上 u 来显式声明哪些字符串是 Unicode。

首先,将您的源代码编码设置为 UTF-8。

# -*- coding: utf-8 -*- 

然后告诉 Python 将这些字符串编码为 Unicode。否则它们将被视为单独的字节,这将导致奇怪的事情,例如 Python 认为第一个字符串的长度为 21 而不是 19。

print len(u'changing. • 5.0 oz.')    # 19 characters 
print len('changing. • 5.0 oz.')     # 21 bytes 

这是因为the Unicode code point U+02022 BULLET is UTF-8 encoded as three bytes e2 80 a2 .第一个将其视为单个字符,第二个将其视为三个字节。

最后,将您要搜索的字符编码为 Unicode。那是 u'\u2022' 或 u'•'。

#!/usr/bin/env python 
# -*- coding: utf-8 -*- 
 
list = [u'changing. • 5.0 oz.', u'hello', u'dfd', u'df', u'changing. • 5.0 oz.'] 
for item in list: 
    if u'•' in item: 
        print('yay') 

真正的代码可能不会使用常量字符串,所以你必须确保 list 中的任何内容都被编码为 UTF-8。