使用字典查找列表列表的最大值和最小值
Finding maximum and minimum of list of lists using dictionary
我试图在列表的列表中找到最大值和最小值。详细地说,我有这个存储推文的变量:
lines = [['ladybug7501',
'RT SamaritansPurse: You can help the many across #PuertoRico who remain in desperate need after #HurricaneMaria. See how here: …',
'Negative',
-1],
['DyEliana',
'RT daddy_yankee: La madre naturaleza está azotando con potencia a sus hijos. Mi corazón y mis oraciones con mi tierra #PuertoRico y mis he…',
'Neutral',
0],
['waffloesH',
'RT SteveCase: PLEASE HELP: ChefJoseAndres is working tirelessly to feed #PuertoRico, but urgently needs our help: ',
'Neutral',
0],
['SteveLevinePR',
'RT StarrMSS: .elvisduran gave 30K to @Bethenny to charter plane to bring supplies to #PuertoRico HurricaneMaria. He also gave 100K to ',
'Neutral',
0],
['bronxdems',
'RT theCEI: THANK YOU to rubendiazjr and the NY Hispanic Clergy for organizing an amazing event last week in support of PuertoRico! ❤️…',
'Positive',
3]]
它有更多列表,但我只发布了一个示例。为了达到这一点,我已经完成了大部分繁重的工作。我想要做的是打印出具有最高正面词和最高负面词的推文。列表最后一部分的数字越高,它就越积极。 (-1、0 和 3)。我正在尝试打印出与之关联的最高价值的推文。
这是我一直在研究的一些代码:
user_lines = []
for line in lines:
freqs = {}
user_lines.append(line[2])
for i in user_lines:
if i not in freqs:
freqs[i] = 1
else:
freqs[i] += 1
freqs
但这就是我的全部。有人有什么想法吗?
如果你想只在字典中保存最差和最好的推文,你可以iterate
通过推文并保存索引worst/best 评级被放置。之后,您可以将这些索引的信息保存到这样的字典中:
highest = 0
lowest = 0
dic = {}
for i, line in enumerate(lines):
number_of_likes = line[3]
if number_of_likes < lowest:
lowest = i
if number_of_likes > highest:
highest = i
dic['lowest'] = [lines[lowest][3], lines[lowest][1]]
dic['highest'] = [lines[highest][3], lines[highest][1]]
输出:
{'lowest': [-1, 'RT SamaritansPurse: You can help the many across #PuertoRico who remain in desperate need after #HurricaneMaria. See how here: …'], 'highest': [3, 'RT theCEI: THANK YOU to rubendiazjr and the NY Hispanic Clergy for organizing an amazing event last week in support of PuertoRico! ❤️…']}
你可以指定max
和min
的key试试
mini=min(lines, key=lambda x: x[-1])
maxi=max(lines, key=lambda x: x[-1])
print(mini)
print(maxi)
输出:
mini
['ladybug7501', 'RT SamaritansPurse: You can help the many across #PuertoRico who remain in desperate need after #HurricaneMaria. See how here: …', 'Negative', -1]
maxi
['bronxdems', 'RT theCEI: THANK YOU to rubendiazjr and the NY Hispanic Clergy for organizing an amazing event last week in support of PuertoRico! ❤️…', 'Positive', 3]
我试图在列表的列表中找到最大值和最小值。详细地说,我有这个存储推文的变量:
lines = [['ladybug7501',
'RT SamaritansPurse: You can help the many across #PuertoRico who remain in desperate need after #HurricaneMaria. See how here: …',
'Negative',
-1],
['DyEliana',
'RT daddy_yankee: La madre naturaleza está azotando con potencia a sus hijos. Mi corazón y mis oraciones con mi tierra #PuertoRico y mis he…',
'Neutral',
0],
['waffloesH',
'RT SteveCase: PLEASE HELP: ChefJoseAndres is working tirelessly to feed #PuertoRico, but urgently needs our help: ',
'Neutral',
0],
['SteveLevinePR',
'RT StarrMSS: .elvisduran gave 30K to @Bethenny to charter plane to bring supplies to #PuertoRico HurricaneMaria. He also gave 100K to ',
'Neutral',
0],
['bronxdems',
'RT theCEI: THANK YOU to rubendiazjr and the NY Hispanic Clergy for organizing an amazing event last week in support of PuertoRico! ❤️…',
'Positive',
3]]
它有更多列表,但我只发布了一个示例。为了达到这一点,我已经完成了大部分繁重的工作。我想要做的是打印出具有最高正面词和最高负面词的推文。列表最后一部分的数字越高,它就越积极。 (-1、0 和 3)。我正在尝试打印出与之关联的最高价值的推文。
这是我一直在研究的一些代码:
user_lines = []
for line in lines:
freqs = {}
user_lines.append(line[2])
for i in user_lines:
if i not in freqs:
freqs[i] = 1
else:
freqs[i] += 1
freqs
但这就是我的全部。有人有什么想法吗?
如果你想只在字典中保存最差和最好的推文,你可以iterate
通过推文并保存索引worst/best 评级被放置。之后,您可以将这些索引的信息保存到这样的字典中:
highest = 0
lowest = 0
dic = {}
for i, line in enumerate(lines):
number_of_likes = line[3]
if number_of_likes < lowest:
lowest = i
if number_of_likes > highest:
highest = i
dic['lowest'] = [lines[lowest][3], lines[lowest][1]]
dic['highest'] = [lines[highest][3], lines[highest][1]]
输出:
{'lowest': [-1, 'RT SamaritansPurse: You can help the many across #PuertoRico who remain in desperate need after #HurricaneMaria. See how here: …'], 'highest': [3, 'RT theCEI: THANK YOU to rubendiazjr and the NY Hispanic Clergy for organizing an amazing event last week in support of PuertoRico! ❤️…']}
你可以指定max
和min
mini=min(lines, key=lambda x: x[-1])
maxi=max(lines, key=lambda x: x[-1])
print(mini)
print(maxi)
输出:
mini
['ladybug7501', 'RT SamaritansPurse: You can help the many across #PuertoRico who remain in desperate need after #HurricaneMaria. See how here: …', 'Negative', -1]
maxi
['bronxdems', 'RT theCEI: THANK YOU to rubendiazjr and the NY Hispanic Clergy for organizing an amazing event last week in support of PuertoRico! ❤️…', 'Positive', 3]