Python 具有基于值的值选择的字符串
我有一个有两个值的集合Python 具有基于值的值选择的字符串,python,list,selection,Python,List,Selection,我有一个有两个值的集合 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
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的值
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)]
没有循环的情况如何:
输出:
[('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中是一个关键字。如果b>1,请使用al=[(a,b)作为l中的a,b]
谢谢@PatrickHaugh!我不知道这个词!我同意,但我用的是他的变量,应该修正它。。。并且做到了:-)
[('coin unit', 9.0), ('normal margin requirements', 8.5), ('futures industry', 8.2), ('wild cryptocurrency market', 7.333333333333334), ('biggest financial institutions', 6.833333333333334)]