I have following tuple:
我有以下元组:
vertices = ([0,0],[0,0],[0,0]);
And on each loop I want to append the following list:
在每个循环中,我想附加以下列表:
[x, y]
How should I approach it?
我该怎么办呢?
5 个解决方案
#1
5
You can't append a list
to a tuple
because tuples are "immutable" (they can't be changed). It is however easy to append a tuple to a list:
您不能将列表附加到元组,因为元组是“不可变的”(它们不能更改)。然而,将元组附加到列表很容易:
vertices = [(0, 0), (0, 0), (0, 0)]
for x in range(10):
vertices.append((x, y))
You can add tuples together to create a new, longer tuple, but that strongly goes against the purpose of tuples, and will slow down as the number of elements gets larger. Using a list in this case is preferred.
您可以将元组一起添加以创建一个新的,更长的元组,但这很有可能违背元组的目的,并且随着元素数量的增加而减慢。在这种情况下使用列表是首选。
#2
1
You can't modify a tuple. You'll either need to replace the tuple with a new one containing the additional vertex, or change it to a list. A list is simply a modifiable tuple.
你不能修改元组。您需要使用包含附加顶点的新元组替换元组,或将其更改为列表。列表只是一个可修改的元组。
vertices = [[0,0],[0,0],[0,0]]
for ...:
vertices.append([x, y])
#3
1
You can concatenate two tuples:
您可以连接两个元组:
>>> vertices = ([0,0],[0,0],[0,0])
>>> lst = [10, 20]
>>> vertices = vertices + tuple([lst])
>>> vertices
([0, 0], [0, 0], [0, 0], [10, 20])
#4
1
You probably want a list, as mentioned above. But if you really need a tuple, you can create a new tuple by concatenating tuples:
如上所述,您可能需要一个列表。但是如果你真的需要一个元组,你可以通过连接元组来创建一个新的元组:
vertices = ([0,0],[0,0],[0,0])
for x in (1, 2):
for y in (3, 4):
vertices += ([x,y],)
Alternatively, and for more efficiency, use a list while you're building the tuple and convert it at the end:
或者,为了提高效率,在构建元组时使用列表并在结尾处转换它:
vertices = ([0,0],[0,0],[0,0])
#...
vlist = list(vertices)
for x in (1, 2):
for y in (3, 4):
vlist.append([x, y])
vertices = tuple(vlist)
At the end of either one, vertices
is:
在任何一个的末尾,顶点是:
([0, 0], [0, 0], [0, 0], [1, 3], [1, 4], [2, 3], [2, 4])
#5
0
Not sure I understand you, but if you want to append x,y to each vertex you can do something like :
不确定我理解你,但是如果你想将x,y附加到每个顶点,你可以做类似的事情:
vertices = ([0,0],[0,0],[0,0])
for v in vertices:
v[0] += x
v[1] += y
#1
5
You can't append a list
to a tuple
because tuples are "immutable" (they can't be changed). It is however easy to append a tuple to a list:
您不能将列表附加到元组,因为元组是“不可变的”(它们不能更改)。然而,将元组附加到列表很容易:
vertices = [(0, 0), (0, 0), (0, 0)]
for x in range(10):
vertices.append((x, y))
You can add tuples together to create a new, longer tuple, but that strongly goes against the purpose of tuples, and will slow down as the number of elements gets larger. Using a list in this case is preferred.
您可以将元组一起添加以创建一个新的,更长的元组,但这很有可能违背元组的目的,并且随着元素数量的增加而减慢。在这种情况下使用列表是首选。
#2
1
You can't modify a tuple. You'll either need to replace the tuple with a new one containing the additional vertex, or change it to a list. A list is simply a modifiable tuple.
你不能修改元组。您需要使用包含附加顶点的新元组替换元组,或将其更改为列表。列表只是一个可修改的元组。
vertices = [[0,0],[0,0],[0,0]]
for ...:
vertices.append([x, y])
#3
1
You can concatenate two tuples:
您可以连接两个元组:
>>> vertices = ([0,0],[0,0],[0,0])
>>> lst = [10, 20]
>>> vertices = vertices + tuple([lst])
>>> vertices
([0, 0], [0, 0], [0, 0], [10, 20])
#4
1
You probably want a list, as mentioned above. But if you really need a tuple, you can create a new tuple by concatenating tuples:
如上所述,您可能需要一个列表。但是如果你真的需要一个元组,你可以通过连接元组来创建一个新的元组:
vertices = ([0,0],[0,0],[0,0])
for x in (1, 2):
for y in (3, 4):
vertices += ([x,y],)
Alternatively, and for more efficiency, use a list while you're building the tuple and convert it at the end:
或者,为了提高效率,在构建元组时使用列表并在结尾处转换它:
vertices = ([0,0],[0,0],[0,0])
#...
vlist = list(vertices)
for x in (1, 2):
for y in (3, 4):
vlist.append([x, y])
vertices = tuple(vlist)
At the end of either one, vertices
is:
在任何一个的末尾,顶点是:
([0, 0], [0, 0], [0, 0], [1, 3], [1, 4], [2, 3], [2, 4])
#5
0
Not sure I understand you, but if you want to append x,y to each vertex you can do something like :
不确定我理解你,但是如果你想将x,y附加到每个顶点,你可以做类似的事情:
vertices = ([0,0],[0,0],[0,0])
for v in vertices:
v[0] += x
v[1] += y