Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将列表中的元素附加到另一个列表中_Python_List_Coordinates_Elements - Fatal编程技术网

Python 将列表中的元素附加到另一个列表中

Python 将列表中的元素附加到另一个列表中,python,list,coordinates,elements,Python,List,Coordinates,Elements,你好。我需要你们的帮助。我有两个清单如下所示 List_1= [ 'Sta Pno Azimuth Distance Latitude Departure ', 'T1 X 170.7011111 22.236 21.9438 -3.593 ', 'T1 X 170.0 20.0 19.6962 -3.473

你好。我需要你们的帮助。我有两个清单如下所示

List_1= [
    'Sta     Pno     Azimuth      Distance     Latitude     Departure ', 
    'T1      X       170.7011111   22.236       21.9438      -3.593    ', 
    'T1      X       170.0        20.0         19.6962      -3.473    ', 
    'T2      X       30.22833333   6.083        -5.2559      -3.0625   ', 
    'T3      X       154.5155556   98.212       88.6562      -42.2573  ', 
    'T4      CHB     351.4977778   93.637       -92.6079     13.844    ', 
    '' ]

List_2= [
    'Sta     Northing     Easting   ', 
    'T1      2000         2000      ',
    'T2      1500         1600      ', 
    'T3      2400         2200      ', 
    'T4      2600         2800      ', 
    '' ]
我想把车站的北距和东距附加到第一个列表中。请帮忙。到目前为止,我尝试使用zip函数,然后将这两个列表组合起来,但问题是我不知道如何通过使用元素的一部分作为与另一个元素匹配的引用来将一个元素与另一个元素匹配

# deal with headings separately from rest of data.
# also split each heading into a list of string rather than single string.
l1_headings, l2_headings = List_1.pop(0).split(), List_2.pop(0).split()
# put each row of List_1 in a list of dictionary using headings as keys
l1_dicts = [{k:v for k, v in zip(l1_headings, row.split())} for row in List_1 if row != '']
# put each row of List_2 in a dictionary of dictionaries indexed by 'Sta'
l2_by_Sta = {}
for row in List_2:
    if row == '': continue
    d = {k:v for k, v in zip(l2_headings, row.split())}
    l2_by_Sta[d['Sta']] = d
# update l1_dicts from data in l2_by_Sta
for d in l1_dicts:
    d.update(l2_by_Sta[d['Sta']])
l1_目录现在包含:

[{'Azimuth': '170.7011111',
  'Departure': '-3.593',
  'Distance': '22.236',
  'Easting': '2000',
  'Latitude': '21.9438',
  'Northing': '2000',
  'Pno': 'X',
  'Sta': 'T1'},
 {'Azimuth': '170.0',
  'Departure': '-3.473',
  'Distance': '20.0',
  'Easting': '2000',
  'Latitude': '19.6962',
  'Northing': '2000',
  'Pno': 'X',
  'Sta': 'T1'},
 {'Azimuth': '30.22833333',
  'Departure': '-3.0625',
  'Distance': '6.083',
  'Easting': '1600',
  'Latitude': '-5.2559',
  'Northing': '1500',
  'Pno': 'X',
  'Sta': 'T2'},
 {'Azimuth': '154.5155556',
  'Departure': '-42.2573',
  'Distance': '98.212',
  'Easting': '2200',
  'Latitude': '88.6562',
  'Northing': '2400',
  'Pno': 'X',
  'Sta': 'T3'},
 {'Azimuth': '351.4977778',
  'Departure': '13.844',
  'Distance': '93.637',
  'Easting': '2800',
  'Latitude': '-92.6079',
  'Northing': '2600',
  'Pno': 'CHB',
  'Sta': 'T4'}]

如果您需要将此数据写入文本文件,请使用csv.DictWriter。

您应该阅读“如何询问堆栈溢出问题”一节。请向我们展示您的尝试,并更具体地说明您遇到的问题。因此,这不是一个让人们为你编写代码的网站。请参阅。对不起,我将编辑我的问题。。这是我第一次…哇,它工作得很好。非常感谢。我本来打算把它转换成一个字典列表,但不幸的是,我不能拿出一个代码来给出和你一样的结果。我真的很棒,先生。非常感谢你。