在列表中查找第二个最小数量

时间:2017-04-18 04:35:27

标签: python python-2.7

需要对列表进行排序

students = [['Prashant',32],['Pallavi',36],['Dheeraj',39],['pawan',36],
['Shivam',40],['amir',36]]

我需要按字母排序的第二个最低分数的学生名单: 恩。拳头最小= 32 = prashant 所以第二分钟= 36 我需要那个得分为36的学生名单,按字母顺序排序:

output: Amir Pallavi Pawan

5 个答案:

答案 0 :(得分:1)

假设:

>>> students
[['Prashant', 32], ['Pallavi', 36], ['Dheeraj', 39], ['Pawan', 36], ['Shivam', 40], ['Amir', 36]]

怎么样:

>>> sorted(st3 for st3 in students if
...   st3[1]==min(st2[1] for st2 in students if st2[1]!=min(st[1] for st in students)))
[['Amir', 36], ['Pallavi', 36], ['Pawan', 36]]

答案 1 :(得分:1)

使用students = [['Prashant',32],['Pallavi',36],['Dheeraj',39],['pawan',36], ['Shivam',40],['amir',36]] import heapq 获得两个最小的分数然后取两者中的较大分数是非常有用的:

by_scores = {}
for n,s in students:
    by_scores.setdefault(s, []).append(n)

second_lowest_score = max(heapq.nsmallest(2, by_scores.keys()))
for name in sorted(by_scores[second_lowest_score]):
    print name

为避免过滤列表,请按照其分数对所有名称进行分组:

{
"gameQuestion": [
    "English League Championship: What will be the match result?",
    "2017 Boston Marathon: Which COUNTRY will the MEN'S WINNER represent?",
    "MLB: Who will WIN this matchup?",
    "English League Championship (Huddersfield Town @ Derby County): Will Derby SCORE in the 2nd Half?",
    "English Premier League: What will be the match result?",
    "MLB: Who will WIN this matchup?",
    "NBA Eastern Conference Playoffs - 1st Rd (Cavaliers lead 1-0): Who will WIN this matchup?",
    "NBA (IND@CLE): Which PLAYER will SCORE a HIGHER PERCENTAGE of their TEAM'S TOTAL POINTS in the 1st Half?",
    "NHL Eastern Conference Playoffs - 1st Rd (Series tied 1-1): Who will WIN this matchup?",
    "NHL Eastern Conference Playoffs - 1st Rd (Series tied 1-1): Who will WIN this matchup?",
    "MLB: Who will WIN this matchup?",
    "MLB: Who will WIN this matchup?",
    "MLB: Who will WIN this matchup?",
    "MLB: Who will WIN this matchup?",
    "NBA (IND@CLE): Will a 3-POINTER be MADE in the FIRST 2 MINUTES of the 3rd Quarter?",
    "NBA Western Conference Playoffs - 1st Rd (Spurs lead 1-0): What will be the GAME RESULT?",
    "NHL Western Conference Playoffs - 1st Rd (Predators lead 2-0): Who will WIN this matchup?",
    "NHL Western Conference Playoffs - 1st Rd (Ducks lead 2-0): Who will WIN this matchup?",
    "MLB: Who will WIN this matchup?",
    "MLB: Who will WIN this matchup?"
],
"propVal": [
    "m57338o58525",
    "m57338o58526",
    "m57336o58521",
    "m57336o58522",
    "m57329o4111",
    "m57329o12",
    "m57316o793",
    "m57316o726",
    "m57319o58515",
    "m57319o58516",
    "m57322o423",
    "m57322o461",
    "m57323o517",
    "m57323o515",
    "m57327o206",
    "m57327o15",
    "m57330o14",
    "m57330o35",
    "m57331o21",
    "m57331o148",
    "m57298o27453",
    "m57298o112",
    "m57320o58517",
    "m57320o58518",
    "m57318o58513",
    "m57318o58514",
    "m57325o481",
    "m57325o479",
    "m57326o463",
    "m57326o5964",
    "m57333o19384",
    "m57333o78",
    "m57334o3",
    "m57334o5"
],
"info": [
    "Opponents",
    " Aston Villa: Win or Draw",
    "@ Fulham: Win",
    "Kenya",
    " Any Other Country",
    " Tampa Bay Rays (6-7) Snell",
    " @ Boston Red Sox (7-5) Wright",
    " Yes: Derby Scores 1+ Goals in 2nd Half",
    " No: No Derby Goal in 2nd Half",
    " Arsenal: Win",
    " @ Middlesbrough: Win or Draw",
    " Chicago White Sox (6-5) Holland",
    " @ New York Yankees (8-4) Montgomery",
    " Indiana Pacers (42-40)",
    " @ Cleveland Cavaliers (51-31)",
    " Paul George (IND)",
    " LeBron James (CLE) or Tie",
    " Ottawa Senators (44-28-10)",
    " @ Boston Bruins (44-31-7)",
    " Washington Capitals (55-19-8)",
    " @ Toronto Maple Leafs (40-27-15)",
    " Pittsburgh Pirates (6-6) Nova",
    " @ St. Louis Cardinals (3-9) Lynn",
    " Milwaukee Brewers (7-6) Anderson",
    " @ Chicago Cubs (6-6) Lackey",
    " Cleveland Indians (5-7) Salazar",
    " @ Minnesota Twins (7-5) Gibson",
    " Los Angeles Angels (6-7) Chavez",
    " @ Houston Astros (8-4) Morton",
    " Yes: 3PM in First 2 Min of 3rd Qtr",
    " No: No 3PM in First 2 Min of 3rd Qtr",
    " Grizzlies: Win or Single Digit Loss",
    " @ Spurs: Win By Double Digits",
    " Chicago Blackhawks (50-23-9)",
    " @ Nashville Predators (41-29-12)",
    " Anaheim Ducks (46-23-13)",
    " @ Calgary Flames (45-33-4)",
    " Miami Marlins (7-5) Koehler",
    " @ Seattle Mariners (5-8) Miranda",
    " Arizona Diamondbacks (8-5) Ray",
    " @ Los Angeles Dodgers (7-6) McCarthy"
]

答案 2 :(得分:0)

排序时可以使用多个键:

>>> import operator
>>> students = [['Prashant',32],['Pallavi',36],['Dheeraj',39],['Pawan',36],['Shivam',40],['Amir',36]]
>>> sorted_students = sorted(students, key=operator.itemgetter(1, 0))
>>> sorted_students
[['Prashant', 32], ['Amir', 36], ['Pallavi', 36], ['Pawan', 36], ['Dheeraj', 39], ['Shivam', 40]]

答案 3 :(得分:0)

这一个:

students = [['Prashant',32],['Pallavi',36],['Dheeraj',39],['Pawan',36],['Shivam',40],['Amir',36]]
names = []
s = set(i[1] for i in students)
sec_min =  sorted(s)[1]
for j in students:
if (j[1] == sec_min):
    names.append (j[0])
names.sort()
print names

答案 4 :(得分:0)

students = [['Prashant',32],['Pallavi',36],['Dheeraj',39],['pawan',36],['Shivam',40],['amir',36]]

stud = dict()

'''
Save students list into a dictionay with score as dictionary index
i.e {32: 'Prashant', 36: 'Pallavi','pawan','amir'
'''
for student in students:
    if student[1] in stud:
    stud[student[1]].append(student[0])
else:
    stud[student[1]]=[]
    stud[student[1]].append(student[0])


sorted_names=sorted(list(map(str.title,stud[sorted(stud.keys())[1]])))
for student in sorted_names:
    print(student)

'''
BREAKDOWN
scores = sorted(stud.keys())
second_score = scores[1]
second_score_students = stud[second_score]
second_score_students = list(map(str.title,second_score_students))
sorted_names=sorted(second_score_students)
'''