我有一个如下所示的列表:
<type 'list'>
[('crap', 0), ('jou', 0), ('gaat', 0), ('awesome', 0), ('awesome?', 0), ('met', 0), ('nou', 0), ('useless', 0), ('bananen', -1.4447848426728962), ('appels', -1.8073549220576042)]
我现在要做的是过滤'bananen'值 - &gt; -1.44
但是,如果我这样做:
item = [elem for elem in top_neg if elem[1] == 'bananen']
print(item)
这给了我一个空项目。对这里出了什么问题的想法?
答案 0 :(得分:0)
print([elem for elem in top_neg if elem[0] == 'bananen'])
你正在测试错误的元素
答案 1 :(得分:0)
我认为最好将列表转换为字典,字典的好处包括:
查看示例:
sample_list = [('crap', 0), ('jou', 0), ('gaat', 0), ('awesome', 0), ('awesome?', 0), ('met', 0), ('nou', 0), ('useless', 0), ('bananen', -1.4447848426728962), ('appels', -1.8073549220576042)]
sample_dic = dict(sample_list)
sample_dic.get('bananen', -99)
# -1.4447848426728962
sample_dic.get('banaeeeeeeeee',-99)
# -99
sample_dic.get('appels', -99)
# -1.8073549220576042