Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to modify a list in Python?

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.

like image 952
linasster Avatar asked Feb 26 '26 21:02

linasster


1 Answers

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.

like image 182
Chi Avatar answered Mar 01 '26 14:03

Chi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!