Python中判断列表是否包含某个元素的方法

文章目录

  • 1、成员运算符 in 和 not in
  • 2、使用count()方法
  • 3、使用 any() 函数
  • 4、使用 set 转换
  • 5、使用 numpy 库
  • 6、使用 any() 和生成器表达式
  • 7、使用 index() 方法
  • 8、使用 itertools.chain() 函数
  • 9、使用 collections.Counter 类
  • 10、使用 pandas 库

以下整理几种判断列表中是否包含某个元素的方法。以下代码基于python2.7执行。

1、成员运算符 in 和 not in

最基本的方法是使用成员运算符 in 和 not in。这两个运算符能够快速判定一个元素是否存在于列表中。

#coding=utf-8

# 使用成员运算符
my_list = [1, 2, 3, 4, 5]

# 判定元素是否存在
element = 3
if element in my_list:
    print('{} 存在于列表中。'.format(element))
else:
    print("{} 不存在于列表中。".format(element))

# 或者使用 not in 判定不存在
element = 6
if element not in my_list:
    print("{} 不存在于列表中。".format(element))
else:
    print("{} 存在于列表中。".format(element))

输出:

3 存在于列表中。
6 不存在于列表中。

2、使用count()方法

count() 方法能够统计列表中特定元素的出现次数,通过判断次数是否大于零,能够得知元素是否存在。

#coding=utf-8

# 使用 count() 方法
my_list = [1, 2, 3, 4, 5]
element = 3
if my_list.count(element) > 0:
    print("{} 存在于列表中。".format(element))
else:
    print("{} 不存在于列表中。".format(element))

输出:

3 存在于列表中。

3、使用 any() 函数

any() 函数接受一个可迭代对象,只要其中任何一个元素为真(即非零、非空、非None等),就返回 True。这个特性可以用于判定列表是否包含某个元素。

#coding=utf-8

my_list = [1, 2, 3, 4, 5]

# 使用 any() 函数
element = 3
if any(x == element for x in my_list):
    print("{} 存在于列表中。".format(element))
else:
    print("{} 不存在于列表中。".format(element))

4、使用 set 转换

将列表转换为集合(set)能够大幅提高查找速度,因为集合是哈希表,查找操作的时间复杂度为 O(1)。

#coding=utf-8

my_list = [1, 2, 3, 4, 5]

# 使用 set 转换
element = 3
if element in set(my_list):
    print("{} 存在于列表中。".format(element))
else:
    print("{} 不存在于列表中。".format(element))

5、使用 numpy 库

对于数值型列表,numpy 提供了强大的数组操作,包括成员判定。

#coding=utf-8
import numpy as np

my_list = [1, 2, 3, 4, 5]

# 使用 numpy 库
element = 3
if np.isin(element, my_list):
    print("{} 存在于列表中。".format(element))
else:
    print("{} 不存在于列表中。".format(element))

6、使用 any() 和生成器表达式

结合 any() 和生成器表达式,可以在一行代码中进行简洁的判定。

#coding=utf-8

my_list = [1, 2, 3, 4, 5]

# 使用 any() 和生成器表达式
element = 3
if any(e == element for e in my_list):
    print("{} 存在于列表中。".format(element))
else:
    print("{} 不存在于列表中。".format(element))

7、使用 index() 方法

index() 方法能够返回指定元素的索引值,如果元素不存在,则抛出 ValueError。可以通过捕获异常的方式判断元素是否存在。

#coding=utf-8

my_list = [1, 2, 3, 4, 5]

# 使用 index() 方法
element = 3
try:
    index = my_list.index(element)
    print("{} 存在于列表中,索引值为 {}。".format(element,index))
except ValueError:
    print("{} 不存在于列表中。".format(element))

8、使用 itertools.chain() 函数

itertools.chain() 函数能够将多个可迭代对象连接在一起,结合 any() 函数,可以用于判定多个列表是否包含某个元素。

#coding=utf-8
from itertools import chain

my_list = [1, 2, 3, 4, 5]

# 使用 itertools.chain() 函数
element = 3
lists = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
if any(element in sublist for sublist in chain(*lists)):
    print("{} 存在于列表中。".format(element))
else:
    print("{} 不存在于列表中。".format(element))

9、使用 collections.Counter 类

如果需要判定某个元素在列表中出现的次数,可以使用 collections.Counter 类。

#coding=utf-8
from collections import Counter

my_list = [1, 2, 3, 4, 5]

# 使用 collections.Counter 类
element = 3
element_counts = Counter(my_list)
if element_counts[element] > 0:
    print("{} 存在于列表中。".format(element))
else:
    print("{} 不存在于列表中。".format(element))

10、使用 pandas 库

对于数据科学领域,pandas 库提供了强大的数据结构和数据分析工具,可以方便地进行元素判定。

#coding=utf-8
import pandas as pd

my_list = [1, 2, 3, 4, 5]

# 使用 pandas 库
element = 3
df = pd.DataFrame({'column_name': my_list})
if element in df['column_name'].values:
    print("{} 存在于列表中。".format(element))
else:
    print("{} 不存在于列表中。".format(element))


创作不易,欢迎打赏,你的鼓励将是我创作的最大动力。

Python中判断列表是否包含某个元素的方法_第1张图片

你可能感兴趣的:(python,判断列表是否包含元素)