转换字典中的列表



我需要转换字典中的列表detail_flights。列表的一部分之后(它更长,但结构总是相同的):

detail_flights = ['22:20 – 23:55nBGY Bergamo Orio al Serion‐nBCN Barcellona-El Pratndiretton1h 35mn6:20 – 8:00nBCN Barcellona-El Pratn‐nBGY Bergamo Orio al Seriondiretton1h 40m', '22:20 – 23:55nBGY Bergamo Orio al Serion‐nBCN Barcellona-El Pratndiretton1h 35mn12:05 – 13:45nBCN Barcellona-El Pratn‐nBGY Bergamo Orio al Seriondiretton1h 40m']

我想把列表的所有信息放在下面的标题中:

detail_headers = ['departure_time', 'departure_airport', 'to_delete', 'arrival_airport', 'type_flight', 'duration']

我想获得这样的东西:

detail_flights = [{'departure_time': '22:20 – 23:55', 'departure_airport': 'BGY Bergamo Orio al Serio', 'to_delete': '-', 'arrival_airport': 'BCN Barcellona-El Pra', 'type_flight': 'diretto', 'duration': '1h 35', 'departure_time': '6:20 – 8:00', 'departure_airport': 'BCN Barcellona-El Prat', 'arrival_airport': 'BGY Bergamo Orio al Serio', 'type_flight': 'diretto', 'duration': '1h 40m'}, {....}]

这是我的代码:

def listToString(s): 
str1 = " " 
return (str1.join(detail_flights))

detail_flights = listToString(detail_flights)
detail_flights = detail_flights.split(sep='n')
from itertools import zip_longest
detail_headers = ['departure_time', 'departure_airport', 'to_delete', 'arrival_airport', 'type_flight', 'duration']
d1=zip_longest(detail_headers,detail_flights)
print (dict(d1))

这是我的输出,它不能正常工作:

{'departure_time': '22:20 – 23:55', 'departure_airport': 'BGY Bergamo Orio al Serio', 'to_delete': '‐', 'arrival_airport': 'BCN Barcellona-El Prat', 'type_flight': 'diretto', 'duration': '1h 35m', None: '1h 45m'}
  • Splitdetail_flights byn获取航班详细信息列表。
  • 使用zip制作dict
  • 的键、值对
detail_flights = ['22:20 – 23:55nBGY Bergamo Orio al Serion‐nBCN Barcellona-El Pratndiretton1h 35mn6:20 – 8:00nBCN Barcellona-El Pratn‐nBGY Bergamo Orio al Seriondiretton1h 40m', '22:20 – 23:55nBGY Bergamo Orio al Serion‐nBCN Barcellona-El Pratndiretton1h 35mn12:05 – 13:45nBCN Barcellona-El Pratn‐nBGY Bergamo Orio al Seriondiretton1h 40m']
detail_headers = ['departure_time', 'departure_airport', 'to_delete', 'arrival_airport', 'type_flight', 'duration']
detail_flights = detail_flights[0].split("n")
dict(zip(detail_headers,detail_flights))
>> {'departure_time': '22:20 – 23:55',
'departure_airport': 'BGY Bergamo Orio al Serio',
'to_delete': '‐',
'arrival_airport': 'BCN Barcellona-El Prat',
'type_flight': 'diretto',
'duration': '1h 35m'}
detail_flights = ['22:20 – 23:55nBGY Bergamo Orio al Serion‐nBCN Barcellona-El Pratndiretton1h 35mn6:20 – 8:00nBCN Barcellona-El Pratn‐nBGY Bergamo Orio al Seriondiretton1h 40m', '22:20 – 23:55nBGY Bergamo Orio al Serion‐nBCN Barcellona-El Pratndiretton1h 35mn12:05 – 13:45nBCN Barcellona-El Pratn‐nBGY Bergamo Orio al Seriondiretton1h 40m']

detail_headers = ['departure_time', 'departure_airport', 'to_delete', 'arrival_airport', 'type_flight', 'duration']
dictionary = dict(zip(detail_flights , detail_headers ))
print(dictionary)

输出:

{'22:20 – 23:55nBGY Bergamo Orio al Serion‐nBCN Barcellona-El Pratndiretton1h 35mn6:20 – 8:00nBCN Barcellona-El Pratn‐nBGY Bergamo Orio al Seriondiretton1h 40m': 'departure_time', '22:20 – 23:55nBGY Bergamo Orio al Serion‐nBCN Barcellona-El Pratndiretton1h 35mn12:05 – 13:45nBCN Barcellona-El Pratn‐nBGY Bergamo Orio al Seriondiretton1h 40m': 'departure_airport'}

相关内容

  • 没有找到相关文章

最新更新