【问题标题】:Python code to print elements from list that meet certain criteria [duplicate]Python代码从列表中打印满足特定条件的元素[重复]
【发布时间】:2019-09-27 07:28:37
【问题描述】:

我有一个包含 22 个整数(范围从 1 到 9)的列表,并且想要创建/打印一个仅包含大于 5 的整数的新列表。

这是我迄今为止尝试过的 - 结果(显然)是多次打印“the_list” - 即次数 = 5 以上的实例数。

the_list = [1, 7, 6, 5, 4, 3, 2, 3, 4, 5, 6, 7, 6, 5, 4, 3, 2, 3, 4, 5, 6, 7]
print(the_list)

k=5
tl2=[]
for i in the_list:
    if i > k :
        tl2.append(the_list)

【问题讨论】:

  • 你考虑过使用列表理解吗? new_list = [i for i in the_list if i>5].
  • 代码中唯一的错误`tl2.append(i)`

标签: python-3.x


【解决方案1】:

试试这个代码:

>>> the_list = [1, 7, 6, 5, 4, 3, 2, 3, 4, 5, 6, 7, 6, 5, 4, 3, 2, 3, 4, 5, 6, 7]
>>> print(the_list)
[1, 7, 6, 5, 4, 3, 2, 3, 4, 5, 6, 7, 6, 5, 4, 3, 2, 3, 4, 5, 6, 7]
>>> the_filtered_list = list(filter(lambda x: x > 5, the_list))
>>> print(the_filtered_list)
[7, 6, 6, 7, 6, 6, 7]

编辑

另一种选择是使用生成器表达式:

>>> the_filtered_list = list(i for i in the_list if i > 5)
>>> print(the_filtered_list)
[7, 6, 6, 7, 6, 6, 7]


编辑:

我最初的回答确实很慢而且内存效率很低。以下是几种可能性的比较。选择哪一个取决于列表有多大以及以后要做什么。

>>> import random
>>> import timeit
>>> import sys
>>> 
>>> the_list = [random.randrange(1, 10) for _ in range(100)]
>>> 
>>> timeit.timeit('filter(lambda x: x > 5, the_list)', setup=f'the_list = {the_list}')
0.15890196000000856
>>> timeit.timeit('[i for i in the_list if i > 5]', setup=f'the_list = {the_list}')
2.633208761999981
>>> timeit.timeit('(i for i in the_list if i > 5)', setup=f'the_list = {the_list}')
0.227755295999998
>>> 
>>> timeit.timeit('list(filter(lambda x: x > 5, the_list))', setup=f'the_list = {the_list}')
7.5565902380000125
>>> timeit.timeit('list(i for i in the_list if i > 5)', setup=f'the_list = {the_list}')
3.599053368
>>> 
>>> sys.getsizeof(filter(lambda x: x > 5, the_list))
64
>>> sys.getsizeof([i for i in the_list if i > 5])
440
>>> sys.getsizeof((i for i in the_list if i > 5))
128
>>> 
>>> sys.getsizeof(list(filter(lambda x: x > 5, the_list)))
480
>>> sys.getsizeof(list(i for i in the_list if i > 5))
480

【讨论】:

  • list(filter(lambda... 当理解更短且更具可读性时......只是缺少反对票:)
  • 与列表理解相比,使用 lambda 的过滤器要慢得多。 new_list = [i for i in the_list if i>5] 应该是理想的解决方案。
  • 为什么the_filtered_list = list(i for i in the_list if i > 5) 可以直接使用the_filtered_list = [i for i in the_list if i > 5]
  • @PatrickArtner 因为它更快,内存效率更高。通过速度和内存使用比较查看我的编辑
  • 应该注意的是,“内存效率”取决于你想进一步做的事情——例如。如果您首先可以拥有list,我看不出list(generator) 的意义。
【解决方案2】:

问题是您要附加列表,而不是数字“i”

the_list = [1, 7, 6, 5, 4, 3, 2, 3, 4, 5, 6, 7, 6, 5, 4, 3, 2, 3, 4, 5, 6, 7]
print(the_list)

k=5
tl2=[]
# the_list refers to the entire list
# i is an element in the list
for i in the_list:
   if i > k :
        # append  the number 'i'  if it is greater than k
        tl2.append(i)
print (t12)

【讨论】:

    【解决方案3】:
    the_list = [1, 7, 6, 5, 4, 3, 2, 3, 4, 5, 6, 7, 6, 5, 4, 3, 2, 3, 4, 5, 6, 7]
    print(the_list)
    k=5
    new_ls = [x for x in the_list if x >k]
    print(new_ls)
    

    试试这个解决方案

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-06-07
      • 2018-09-07
      • 1970-01-01
      • 2020-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多