且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何按给定索引的项目过滤元组列表并将其转换为列表字典

更新时间:2022-12-12 08:44:06

您无法在字典中获得所需的结果,因为字典中不包含重复的键(类似于普通的英语词典:其中的单词可能拼写相同,但发音有所不同.

You can't have the desired result in a dictionary as dictionaries don't contain duplicate keys (similar to a normal English dictionary: where the words might spell the same but there are differences in pronunciation).

所需结果可以再次存储到列表中.

The desired result can be stored again into a list.

newDict = {}
result = []

Lista =[('amazon', 'Amazon', 1.0), ('amazon', 'Alexa', 0.8), ('amazon', 'microsoft', 0.6), ('amazon', 'Amazon Pay', 0.7), ('amazon', 'Prime', 0.4),('alien', 'jack' , 0.0), ('alien', 'dell', 0.6), ('alien', 'apple', 0.0), ('alien', 'orange', 0.0), ('alien', 'fig', 0.0)]

for items in Lista:
    if items[2] > 0.0:
        newDict[items[0]] = items[1]
        result.append(newDict)
        newDict = {}

print result