PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, November 4, 2022

[FIXED] How to create a new list in Python based on two factors?

 November 04, 2022     key, lambda, list, python, sorting     No comments   

Issue

I have a list of list in Python like this:

vehicle_list = [['car', '123464', '4322445'],   ['car', '64346', '643267'], ['bicycle', '1357', '78543'], 
        ['bicycle', '75325', '75425'], ['car', '8652', '652466'], ['taxi', '653367', '63226'], 
        ['taxi', '96544', '22267'], ['taxi', '86542222', '54433'],     
        ['motorcycle', '675422', '56312'], ['motorcycle', '53225', '88885'], ['motorcycle', '773345', '9977'], 
        ['motorcycle', '3466', '987444'], ['truck', '2455554', '5225544'], ['truck', '2455554', '344543'], 
        ['train', '6543355', '6336']]

I want to return the top 3 vehicles which has the highest number at the end. Like this:

top_vehicle = [['truck', '2455554', '5225544'], ['car', '123464', '4322445'], ['motorcycle', '3466', '987444']]

I have tried sorting this way, but with my result, the vehicles are repeating which I do not want. I want the unique vehicles in my sorted list. I have tried the code this way:

top_vehicle = (sorted(vehicle_list, key=lambda x: int(x[-1]), reverse = True))[:3]
print(top_vehicle)


[['truck', '2455554', '5225544'], ['car', '123464', '4322445'], ['car', '8652', '652466']]

Solution

Method by

  • Get the item with largest number at end of item for vehicle
  • Reverse sort the values of dictionary by the number at end of item
  • Get the first three items
vehicle_list = [['car', '123464', '4322445'],   ['car', '64346', '643267'], ['bicycle', '1357', '78543'],
        ['bicycle', '75325', '75425'], ['car', '8652', '652466'], ['taxi', '653367', '63226'],
        ['taxi', '96544', '22267'], ['taxi', '86542222', '54433'],
        ['motorcycle', '675422', '56312'], ['motorcycle', '53225', '88885'], ['motorcycle', '773345', '9977'],
        ['motorcycle', '3466', '987444'], ['truck', '2455554', '5225544'], ['truck', '2455554', '344543'],
        ['train', '6543355', '6336']]

# Get largest one for each vehicle by dictionary
largest = {}
for item, no1, no2 in vehicle_list:
    if (item in largest and int(largest[item][2]) < int(no2)) or (item not in largest):
        largest[item] = [item, no1, no2]

# Reverse sort list by 3rd value and get first three
top_vehicle = sorted(list(largest.values()), key=lambda x:int(x[2]), reverse=True)[:3]

print(top_vehicle)
# [['truck', '2455554', '5225544'], ['car', '123464', '4322445'], ['motorcycle', '3466', '987444']]


Answered By - Jason Yang
Answer Checked By - Katrina (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing