element in a list and element in other list,元素在一个list,且在另一个list

  • 在数据量大的时候使用set,把list转为集合,此方法适合用于大数据量筛选数据
a = [1,2,3,4,5,6,7,9]
b = [6,7,8,9,10]

s1 = list(set(a).intersection(set(b)))
print(s1)
  • 在数据量小的时候使用for循环也可,但是数据量大的时候耗时太长
main_list = []
for i in list_2:
    if i in list_1:
        main_list.append(i)
  • 在数据量小的时候使用 列表 表达式和for循环是一样的效果
        main_list = [i for i in list1 if i in list_2]

相关文章:

  • 2021-08-02
  • 2021-12-03
  • 2021-12-03
  • 2022-12-23
  • 2022-12-23
  • 2021-11-04
  • 2021-12-03
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-02
  • 2022-12-23
相关资源
相似解决方案