Solved Python - list of tuples query

November 11, 2020 at 15:13:50
Specs: AIX 5.3, 4 CPU / 32 GB RAM
Hello,

I have a list of tuples (name, score). Fred and Steve appear twice. How do I retain only the highest name,score pair?

Starting list:
[('Steve', 100), ('Amy', 100), ('Fred', 100), ('Sandy', 96), ('Stephen', 80), ('Fred', 75), ('Steve', 67), ('Zeb', 60), ('Jane', 55)]

Desired list:
[('Steve', 100), ('Amy', 100), ('Fred', 100), ('Sandy', 96), ('Stephen', 80), ('Zeb', 60), ('Jane', 55)]

Thanks,


See More: Python - list of tuples query


✔ Best Answer
November 16, 2020 at 13:43:38
@nbrane thanks for responding with the advice. I ended up using this routine.


tupleList = [('Steve', 100), ('Amy', 100), ('Fred', 100), ('Sandy', 96), ('Stephen', 80), ('Fred', 75), ('Steve', 67), ('Zeb', 60), ('Jane', 55)]
print(tupleList)
tupleList.sort(key= lambda x: x[1], reverse = True)

uniqueName = []
uniqueList = []
MAX = len(tupleList)   # Get max elements

for i in range(MAX):
    nameScoreList = list(tupleList[i]) # Convert tuple to list for each name/score pair
    firstPart = nameScoreList[0]
    secondPart = nameScoreList[1]

    if firstPart not in uniqueName:
        uniqueName.append(firstPart)    # Append all unique names, firstPart
        uniqueList.append( (firstPart,secondPart) ) # Append (firstPart, secondPart) to new list

print(uniqueList)




#1
November 12, 2020 at 01:02:41
Are all these questions about Python homework by any chance?

Reply ↓  Report •

#2
November 12, 2020 at 01:16:07
No, I am a little old for school work by a factor of decades. I am comfortable with shelling scripting however making a concerted effort to get to grips with Python.

As per my profile stats:
Member Since February 9, 2008

message edited by user57


Reply ↓  Report •

#3
November 12, 2020 at 02:37:16
You may find learning more effective if you make a real effort to solve the problems by yourself rather than asking for answers.

Reply ↓  Report •

Related Solutions

#4
November 14, 2020 at 20:24:35
I would start with a format template to normalize names left-just, scores right-just. Then sort hi-to-lo and only take the first instance of field#1 (name) which will be highest (or only) score for that name. I don't know what media your data is delivered:
string, array, or file, to the Python code.

Reply ↓  Report •

#5
November 16, 2020 at 13:43:38
✔ Best Answer
@nbrane thanks for responding with the advice. I ended up using this routine.


tupleList = [('Steve', 100), ('Amy', 100), ('Fred', 100), ('Sandy', 96), ('Stephen', 80), ('Fred', 75), ('Steve', 67), ('Zeb', 60), ('Jane', 55)]
print(tupleList)
tupleList.sort(key= lambda x: x[1], reverse = True)

uniqueName = []
uniqueList = []
MAX = len(tupleList)   # Get max elements

for i in range(MAX):
    nameScoreList = list(tupleList[i]) # Convert tuple to list for each name/score pair
    firstPart = nameScoreList[0]
    secondPart = nameScoreList[1]

    if firstPart not in uniqueName:
        uniqueName.append(firstPart)    # Append all unique names, firstPart
        uniqueList.append( (firstPart,secondPart) ) # Append (firstPart, secondPart) to new list

print(uniqueList)



Reply ↓  Report •

#6
November 16, 2020 at 18:27:55
Ah, you caught the "sort reverse", which I failed to address. Good catch, and sorry for the bad throw! ;)

message edited by nbrane


Reply ↓  Report •

Ask Question