【发布时间】:2020-08-01 17:58:09
【问题描述】:
我有一个清单如下:
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"]
我必须找到每个航班的乘客人数,但我无法将 : 加入计数和 elem 入围名单。我给出了下面的函数,cmets 用于描述和输出格式。
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)
【问题讨论】:
标签: python python-3.x list function