带值字符串基于值选择

2024-06-16 15:28:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个有两个值的集合

list = [('coin unit', 9.0), ('normal margin requirements', 8.5), ('futures industry', 8.2), ('wild cryptocurrency market', 7.333333333333334), ('biggest financial institutions', 6.833333333333334), ('futures market', 1.0), ('biggest banks', 0.5), ('cryptocurrency frenzy', 0.5)]

如何仅保留那些得分大于1的值


Tags: marginunitmarketlistrequirementscryptocurrencycoinnormal
2条回答
list_of_stuff = [('coin unit', 9.0), ('normal margin requirements', 8.5), ('futures industry', 8.2), ('wild cryptocurrency market', 7.333333333333334), ('biggest financial institutions', 6.833333333333334), ('futures market', 1.0), ('biggest banks', 0.5), ('cryptocurrency frenzy', 0.5)]
newlist = [x for x in list_of_stuff if x[1] > 1.0]
print newlist

将导致

[('coin unit', 9.0), ('normal margin requirements', 8.5), ('futures industry', 8.2), ('wild cryptocurrency market', 7.333333333333334), ('biggest financial institutions', 6.833333333333334)]

What about without loop :

list_1 = [('coin unit', 9.0), ('normal margin requirements', 8.5), ('futures industry', 8.2), ('wild cryptocurrency market', 7.333333333333334), ('biggest financial institutions', 6.833333333333334), ('futures market', 1.0), ('biggest banks', 0.5), ('cryptocurrency frenzy', 0.5)]

print(list(filter(lambda x:x[1]>1,list_1)))

输出:

[('coin unit', 9.0), ('normal margin requirements', 8.5), ('futures industry', 8.2), ('wild cryptocurrency market', 7.333333333333334), ('biggest financial institutions', 6.833333333333334)]

注意:千万不要用list作为变量名,因为list在python中是一个关键字

相关问题 更多 >