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-22 17:34:52 31 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

进入


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

不用担心:-)很高兴我能帮忙!如果你想标记它已解决,我将不胜感激:-)

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