简体   繁体   English

如何在Python中修改列表?

[英]How to modify a list in Python?

Can you help me to modify my list? 你能帮我修改一下我的清单吗? I have a list which consists of three coordinates XY Z. I need to produce a string with the XY coordinates delimited by a comma and each coordinate delimited by a newline. 我有一个由三个坐标XY Z组成的列表。我需要生成一个字符串,其中XY坐标用逗号分隔,每个坐标用换行符分隔。

xyz = ['55.548745 35.547852 5.545', '57.85425 37.524852 7.545', '57.214445 38.587852 6.745']

Result should look like this: 结果应如下所示:

xy = "55.548745,35.547852
     57.854258,37.524852
     57.214445,38.587852"

How can this be done? 如何才能做到这一点? Thank you in advance for your help. 预先感谢您的帮助。

Append the first two items in each sublist to new x , y lists: 将每个子列表中的前两项附加到新的xy列表:

xyz = ['55.548745 35.547852 5.545', '57.85425 37.524852 7.545', '57.214445 38.587852 6.745']

x = []
y = []

for p in xyz:
    x1, y1 = p.split()[:-1]
    x.append(float(x1))
    y.append(float(y1))

print(x)
print(y)

output: 输出:

[55.548745, 57.85425, 57.214445]
[35.547852, 37.524852, 38.587852]

Now you have two lists in the same order as the original list, so corresponding ( x , y ) points can be referenced: 现在您有两个与原始列表顺序相同的列表,因此可以引用相应的( xy )点:

print(x[0], y[0])

returns a tuple of the first coordinate: 返回第一个坐标的元组:

(55.548745, 35.547852)

Here is my solution, use split()[:-1] to remove the z and then it return a list of tuple of x and y 这是我的解决方案,使用split()[:-1]删除z ,然后返回xytuple列表

xyz = ['55.548745 35.547852 5.545', '57.85425 37.524852 7.545', '57.214445 38.587852 6.745']
new_list = [(float(x2), float(y2)) for (x2, y2) in [x1.split()[:-1] for x1 in xyz]]
print(new_list)

Output: 输出:

[(55.548745, 35.547852), (57.85425, 37.524852), (57.214445, 38.587852)]

Here is yet another answer that takes your input list and uses dictionary comprehension to extract the latitude and longitude. 这是另一个答案,它采用您的输入列表并使用字典理解来提取纬度和经度。 Next the code iterates over the two dictionaries and obtains the latitude and longitude values. 接下来,代码迭代两个字典并获得纬度和经度值。

xyz = ['55.548745 35.547852 5.545', '57.85425 37.524852 7.545', '57.214445 38.587852 6.745']

geo_latitude = {y:x.split()[0] for y,x in enumerate(xyz)}
geo_longitude = {y:x.split()[1] for y,x in enumerate(xyz)}

for (latitude_key,latitude_value),(longitude_key,longitude_value) in zip(geo_latitude.items(), geo_longitude.items()):

# I'm using f-strings to format and print the strings
print(f'Lat/Long Coordinates -- {latitude_value}, {longitude_value}')
# outputs
Lat/Long Coordinates -- 55.548745, 35.547852
Lat/Long Coordinates -- 57.85425, 37.524852
Lat/Long Coordinates -- 57.214445, 38.587852

You can use: 您可以使用:

xy = '\n'.join([','.join(coord.split(' ')[0:2]) for coord in xyz])

This iterates through every xyz coordinate, splits them by space, joins the first two coordinates with a comma and produces a list. 这将迭代每个xyz坐标,按空格分割它们,用逗号连接前两个坐标并生成一个列表。 It then joins the list by newlines, creating the desired result. 然后它按新行加入列表,创建所需的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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