简体   繁体   中英

How to join a single element (consisting of two values) in a list with a seperator “:” in Python?

I have a list given below:

 ticket_list=["AI567:MUM:LON:014","AI077:MUM:LON:056", "BA896:MUM:LON:067", "SI267:MUM:SIN:145","AI077:MUM:CAN:060","SI267:BLR:MUM:148","AI567:CHE:SIN:015","AI077:MUM:SIN:050","AI077:MUM:LON:051","SI267:MUM:SIN:146"]

I have to find the number of passengers per flight but I am unable to join the count and elem with : in a finallist. I have given the function below and comments are for description and output format.

 def find_passengers_per_flight():
'''Write the logic to find and return a list having number of passengers traveling per flight based on the details in the ticket_list
In the list, details should be provided in the format:
[flight_no:no_of_passengers, flight_no:no_of_passengers, etc.].'''
listairline=[]
count=0
finallist=[]
for i in ticket_list:
    # listairline=[]
    list2=i.split(":")
    listairline.append(list2[0])
for elem in listairline:
    count=listairline.count(elem)
    if elem not in finallist:
        finallist.append(elem,":",count) #here it is the line which needs to be modified. Kindly help
print (finallist)    

Use collections.defaultdict to count the tickets per flight:

from collections import defaultdict

ticket_list=["AI567:MUM:LON:014","AI077:MUM:LON:056", "BA896:MUM:LON:067", "SI267:MUM:SIN:145","AI077:MUM:CAN:060","SI267:BLR:MUM:148","AI567:CHE:SIN:015","AI077:MUM:SIN:050","AI077:MUM:LON:051","SI267:MUM:SIN:146"]

flight_ticket_counts = defaultdict(int)
for ticket in ticket_list:
    flight_no, *_ = ticket.split(":")
    flight_ticket_counts[flight_no] += 1

print([f"{k}:{v}" for k, v in flight_ticket_counts.items()])
# ['AI567:2', 'AI077:4', 'BA896:1', 'SI267:3']

Or as a one-liner using collections.Counter :

from collections import Counter

flight_ticket_counts = Counter(ticket.split(":")[0] for ticket in ticket_list)

print([f"{k}:{v}" for k, v in flight_ticket_counts.items()])
# ['AI567:2', 'AI077:4', 'BA896:1', 'SI267:3']

If you want the number of passengers, then you can modify the above approach to do so:

flight_passenger_counts = defaultdict(int)
for flight in ticket_list:
    flight_no, _, _, no_passengers = flight.split(":")
    flight_passenger_counts[flight_no] += int(no_passengers)

print([f"{k}:{v}" for k, v in flight_passenger_counts.items()])
# ['AI567:29', 'AI077:217', 'BA896:67', 'SI267:439']

Lets do that with one-liner:

ticket_list=["AI567:MUM:LON:014","AI077:MUM:LON:056", "BA896:MUM:LON:067", "SI267:MUM:SIN:145","AI077:MUM:CAN:060","SI267:BLR:MUM:148","AI567:CHE:SIN:015","AI077:MUM:SIN:050","AI077:MUM:LON:051","SI267:MUM:SIN:146"]

[i+':'+str([i.split(':')[0] for i in ticket_list].count(i.split(':')[0])) for i in sorted(set([i.split(':')[0] for i in ticket_list])) ]

Or in a simpler way:

res = [i.split(':')[0] for i in ticket_list]
res = [i+':'+str(res.count(i.split(':')[0])) for i in sorted(set(res)) ]

Or a more faster way:

from collections import Counter
res = Counter([i.split(':')[0] for i in ticket_list])
[i+':'+str(res[i]) for i in res]

All the above gives:

['AI567:2', 'AI077:4', 'BA896:1', 'SI267:3']

To me it looks like the question is asking for you to convert your answers to a string to get the same format your input was in. You only need to change the line to this:

finallist.append(elem + ":" + str(count))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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