简体   繁体   English

使用字典查找列表列表的最大值和最小值

[英]Finding maximum and minimum of list of lists using dictionary

I'm trying to find the maximum and minimum value within a list of lists.我试图在列表列表中找到最大值和最小值。 To elaborate, I have this variable which stores tweets:详细地说,我有这个存储推文的变量:

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]]

It has a lot more lists but I only posted a sample.它有更多列表,但我只发布了一个示例。 I've done most of the grunt work to get to this point.我已经完成了大部分繁重的工作来达到这一点。 What I'm trying to do is print out the tweet which has the highest positive words and the highest negative.我想要做的是打印出具有最高积极词和最高消极词的推文。 The higher the number on the last slice of the list, the more positive it is.列表最后一个切片上的数字越高,它就越积极。 (The -1, 0, and 3). (-1、0 和 3)。 I'm trying to print out the tweet which has the highest value associated with it.我正在尝试打印出与之相关的最高价值的推文。

This is some code I've been playing around with:这是我一直在玩的一些代码:


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

But that's all I have.但这就是我所拥有的。 Anyone have any ideas?有人有想法么?

You could try it by specifying the key of max and min您可以通过指定maxmin的键来尝试

mini=min(lines, key=lambda x: x[-1])
maxi=max(lines, key=lambda x: x[-1])

print(mini)
print(maxi)

Output: Output:

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]

If you want to save only the worst and the best rated tweet in a dictionary, you could iterate through the tweets and save the index where the worst/best rating is placed.如果您只想在字典中保存最差和评分最高的推文,您可以iterate推文并保存放置最差/最佳评分的索引。 After that you could save the informations of these indices into a dictionary like this:之后,您可以将这些索引的信息保存到这样的字典中:

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]]

output: output:

{'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! 🇵🇷❤️…']}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM