Python 向列表中添加多个元组
我想添加线的所有坐标,如下所示Python 向列表中添加多个元组,python,Python,我想添加线的所有坐标,如下所示 coordinates = [] for line in lines: for x1,y1,x2,y2 in line: coordinates.extend((x1,y1),(x2,y2)) 稍后检索x和y坐标以进行进一步计算 x,y = zip(*coordinates) 我得到错误类型错误:extend只接受给定的一个参数2。我怎样才能在不打两次电话的情况下实现上述目标 coordinates.extend
coordinates = []
for line in lines:
for x1,y1,x2,y2 in line:
coordinates.extend((x1,y1),(x2,y2))
稍后检索x和y坐标以进行进一步计算
x,y = zip(*coordinates)
我得到错误类型错误:extend只接受给定的一个参数2。我怎样才能在不打两次电话的情况下实现上述目标
coordinates.extend((x1,y1))
coordinates.extend((x2,y2))
元素应该在元组或列表中
coordinates.extend(((x1, y1), (x2, y2)))
还有通过以下方式实现的行为:
coordinates.extend((x1, y1))
coordinates.extend((x2, y2))
可以通过
coordinates.extend((x1, y1, x2, y2))
元素应该在元组或列表中
coordinates.extend(((x1, y1), (x2, y2)))
还有通过以下方式实现的行为:
coordinates.extend((x1, y1))
coordinates.extend((x2, y2))
可以通过
coordinates.extend((x1, y1, x2, y2))
坐标延伸[x1,y1,x2,y2]
或者你可以把整件事列在一个清单中:
from itertools import chain
coardinates = list(chain.from_iterable(((x1,y1), (x2,y2))) for line in lines for x1,y1,x2,y2 in line)
coordinates = [pair for line in lines for pair in (line[:2], line[2:])]
坐标延伸[x1,y1,x2,y2]
或者你可以把整件事列在一个清单中:
from itertools import chain
coardinates = list(chain.from_iterable(((x1,y1), (x2,y2))) for line in lines for x1,y1,x2,y2 in line)
coordinates = [pair for line in lines for pair in (line[:2], line[2:])]
在extend中使用list作为extend迭代列表并附加其中的所有元素
坐标延伸[x1,y1,x2,y2]
co=[]
co.extend[1,2,3,4]
公司
[1,2,3,4]
在extend中使用list作为extend迭代列表并附加其中的所有元素
坐标延伸[x1,y1,x2,y2]
co=[]
co.extend[1,2,3,4]
公司
[1,2,3,4]
您可以将整个过程概括为一种理解:
from itertools import chain
coardinates = list(chain.from_iterable(((x1,y1), (x2,y2))) for line in lines for x1,y1,x2,y2 in line)
coordinates = [pair for line in lines for pair in (line[:2], line[2:])]
或者,如果行本身不是元组:
coordinates = [p for l in lines for p in map(tuple, (l[:2], l[2:]))]
您可以将整个过程概括为一种理解:
from itertools import chain
coardinates = list(chain.from_iterable(((x1,y1), (x2,y2))) for line in lines for x1,y1,x2,y2 in line)
coordinates = [pair for line in lines for pair in (line[:2], line[2:])]
或者,如果行本身不是元组:
coordinates = [p for l in lines for p in map(tuple, (l[:2], l[2:]))]
坐标延伸[x1,y1,x2,y2]?另外,它是坐标,除非你是指其他东西。@Jean-Françoisfare将生成一个列表,他只需要一个list@Jean-Françoisfare我相信你可以让listcomp与itertools.chain一起工作,请看我的回答没问题。我们在这里讨论python.coordinates.extend[x1,y1,x2,y2]?另外,它是坐标,除非你是指其他东西。@Jean-Françoisfare将生成一个列表,他只需要一个list@Jean-Françoisfare我相信你可以让listcomp与itertools.chain一起工作,请看我的回答没问题。我们都在这里讨论python。你甚至可以使用第三个for循环来避免使用itertools和force对列表进行右对齐,比如[x1,y1,x2,y2对x1,y1,x2,y2进行直线对齐的坐标]?[x1,y1,x2,y2对x1,y1,x2,y2进行直线对齐的坐标]你甚至可以使用第三个for循环来避免使用itertools和force对列表进行右对齐,类似于[x1,y1,x2,y2,x1,y1,x2,y2的串联坐标]?[x1,y1,x2,x2,y2的串联坐标,x1,y1,x2,y2的串联坐标]