gpt4 book ai didi

How do I split coordinates using python stored in a list into another array/list of their respective axis components(如何使用存储在列表中的Python将坐标拆分到各自轴组件的另一个数组/列表中)

转载 作者:bug小助手 更新时间:2023-10-24 20:20:19 32 4
gpt4 key购买 nike



for example, how do i split the list of coordinates:

例如,如何拆分坐标列表:


coordinates = [(0.24030250759039373, 0.26361380345416574), (0.4054703969436695, 0.21348000864926886), (0.05345487382386427, 0.2434195883426472)]

into

vt.进入,进入


xi = (0.2403025075903937, 0.4054703969436695, 0.05345487382386427)
# and
yi = (0.26361380345416574,0.21348000864926886,0.2434195883426472)

using Python without using simple code

在不使用简单代码的情况下使用Python


I tried doing it with

我试着用它


coordinates = [(0.24030250759039373, 0.26361380345416574), (0.4054703969436695, 0.21348000864926886), (0.05345487382386427, 0.2434195883426472)] 
def obj(coordinates):
for i in range(3):
# Extract coordinates (xi, yi)
xi, yi = coords[2*i], coords[2*i+1]
return (xi, yi)
cord = obj(coords)
print(cord)
# But I could only fetch the first two coordinates, i.e, (0.24030250759039373, 0.26361380345416574), (0.4054703969436695, 0.21348000864926886)

Desired output

期望输出


xi = (0.24030250759039373, 0.4054703969436695, 0.05345487382386427)
yi = (0.26361380345416574, 0.21348000864926886, 0.2434195883426472)

更多回答
优秀答案推荐

Using zip():

使用Zip():


xi, yi = zip(*coordinates)

or using list comprehensions:

或者使用列表理解:


xi = tuple([x[0] for x in coordinates])
yi = tuple([x[1] for x in coordinates])

or if you still wanted to use the same function:

或者,如果您仍想使用相同的功能:


coordinates=[(0.24030250759039373, 0.26361380345416574), (0.4054703969436695, 0.21348000864926886), (0.05345487382386427, 0.2434195883426472)]

def obj(coords):
xi = []
yi = []
for i in range(3):
xi.append(coords[i][0])
yi.append(coords[i][1])
return tuple(xi), tuple(yi)
xi, yi = obj(coordinates)

Notes:

备注:



  1. In your function, the argument name is coordinates, but you use coords in the for loop. Probably a bad idea.

  2. Return is in the for loop too, so it wouldn't actually go through the whole for loop.

  3. Not sure what the 2*i and 2*i+1 are meant to be?



Using zip() as proposed by @Mark is the optimum solution.

按照@Mark建议的方式使用Zip()是最佳解决方案。


However, if you insist on writing your own loop you could do this:

但是,如果您坚持编写自己的循环,您可以这样做:


coordinates = [(0.24030250759039373, 0.26361380345416574), (0.4054703969436695, 0.21348000864926886), (0.05345487382386427, 0.2434195883426472)]

xi = tuple()
yi = tuple()

for x, y in coordinates:
xi = *xi, x
yi = *yi, y

print(xi)
print(yi)

...or...

...或者...


coordinates = [(0.24030250759039373, 0.26361380345416574), (0.4054703969436695, 0.21348000864926886), (0.05345487382386427, 0.2434195883426472)]

xy = [tuple(), tuple()]

for pair in coordinates:
for i, v in enumerate(pair):
xy[i] = *xy[i], v

print(*xy, sep='\n')

Output:

产出:


(0.24030250759039373, 0.4054703969436695, 0.05345487382386427)
(0.26361380345416574, 0.21348000864926886, 0.2434195883426472)

更多回答

Your answer solved my problem!! Thank you so much!!!

你的回答解决了我的问题!!太感谢你了!

No worries :-) glad I could help! If you want to mark it solved I'd appreciate it :-)

无忧:-)很高兴我能帮上忙!如果您想将它标记为已解决,我将不胜感激:-)

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com