3

Can you help me to modify my list? I have a list which consists of three coordinates X Y Z. I need to produce a string with the X Y coordinates delimited by a comma and each coordinate delimited by a newline.

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.

2
  • The result seems a little ambiguous to me, do you want the output to be a list of strings with coordinates comma delimited in each string, or do you want the result to be a list of tuples with floating point numbers? Commented May 15, 2019 at 15:26
  • @DaichiJameson I would like to have the output as a list of strings. That the each new group of x y coordinates would be in a new line. Thanks Commented May 22, 2019 at 13:22

4 Answers 4

1

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. It then joins the list by newlines, creating the desired result.

Sign up to request clarification or add additional context in comments.

Comments

1

Append the first two items in each sublist to new x,y lists:

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:

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

returns a tuple of the first coordinate:

(55.548745, 35.547852)

Comments

1

Here is my solution, use split()[:-1] to remove the z and then it return a list of tuple of x and y

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)]

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.