简体   繁体   English

如何打印数字小于 9 和数字大于 10 的项目?

[英]How to print the items with the number less than 9, and number greater than 10?

I am writing a program to visualize the items in a list that are smaller than or greater than 9. This is the list I have made:我正在编写一个程序来可视化列表中小于或大于 9 的项目。这是我制作的列表:

list = [("item1",12.5),("item",2.3),("item",7.0)]

I want to be able to print the items with the number less than 9, and then print those with a number greater than 10.我希望能够打印数字小于 9 的项目,然后打印数字大于 10 的项目。

You can use list comprehensions , which provide a simple way to filter lists:您可以使用list comprehensions ,它提供了一种过滤列表的简单方法:

l = [("item1",12.5),("item",2.3),("item",7.0)]
[i for i in l if i[1] > 9]
# [('item1', 12.5)]

Which is equivalent to the following for loop:这相当于以下 for 循环:

new_list = []
for i in l:
    if i[1] > 9:
        new_list.append(i)
print(new_list)
# [('item1', 12.5)]

Or for values smaller than 9 :或者对于小于9值:

[i for i in l if i[1] < 9]
# [('item', 2.3), ('item', 7.0)]

Given a list,给定一个清单,

L = [("item1",12.5),("item",2.3),("item",7.0)]

(note avoiding using the keyword list as a variable name), you can do a list comprehension, for example: (注意避免使用关键字list作为变量名),您可以进行列表理解,例如:

>>> [(item, value) for (item, value) in L if value < 9.0]
[('item', 2.3), ('item', 7.0)]

To change the criteria, change the I f at the end.要改变标准,改变了I在最后F。

Loop/iterate over items in the list循环/迭代列表中的项目

my_list = [("item1",12.5),("item",2.3),("item",7.0)]

for item in my_list:
    if item[1] < 9:  # change <9 to whatever condition you want
        print(item)  

You could use filter function as such:您可以这样使用过滤器功能:

original_list = [("item1",12.5),("item",2.3),("item",7.0)]

filtered_list = list(filter(lambda x: x[1] < 9.0, original_list))

printing this:打印这个:

print(filtered_list)

[('item', 2.3), ('item', 7.0)]
liste = [("item1",12.5),("item",2.3),("item",7.0)]
index=0

while(index<liste.__len__()):
   if liste[index][1] > 9:
      print(liste[index])
   index=index+1

Storing the values in 2 lists将值存储在 2 个列表中

my_list = [("item1",12.5),("item",2.3),("item",7.0)]

less_than_9 = [x for x in my_list if x[1] < 9]
more_than_9 = [x for x in my_list if x[1] > 9]

>>> print(less_than_9)
[("item1",12.5)]
>>> print(more_than_9)
[("item",2.3),("item",7.0)]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果循环中的数字小于前一个数字,该如何打印? - How to print out a number if it is less than the previous number in a loop? Django 查询集,小于或大于版本号 - Django querysets, less than or greater than versioning number 如何测试一个数字是否可以被小于1的十进制整除? (54.10%.10) - how to test if a number is divisible by a decimal less than 1? (54.10 % .10) 如何计算python中大于和小于给定数字集的数字 - How to count the number greater than and less than a given set of numbers in python 打印小于给定最大值的数字的倍数 - Print multiples of a number less than a given max 创建一个新列表 L3,如果 L1 中的数字大于 10 且 L2 中的数字小于 5,则对两个数字求和 - create a new list, L3, that sums the two numbers if the number from L1 is greater than 10 and the number from L2 is less than 5 如何计算列表中的数字大于其后的数字的次数? - How to count the number of times a number in a list is greater than the number after it? 如果数据框列中的数字小于10,如何在数字前添加零 - how to add zero before a number if the number is less than 10 in a dataframe column Python 3 中大于 10^2000 的数的平方根 - square root of a number greater than 10^2000 in Python 3 Python Pandas:选择唯一值数大于 10 的列 - Python Pandas: select column with the number of unique values greater than 10
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM