如何在Python中将字符串添加到列表中?

如何在Python中将字符串添加到列表中?,python,list,Python,List,我有一个如下所示的值列表: 15,100,25.0 -50,-50,50.0 -20,120,70,40 200,-100,25,5 前两行表示圆的值,第三行表示矩形,第四行表示多边形。我希望输出如下所示: c 15,100,25.0 c -50,-50,50.0 r -20,120,70,40 p 200,-100,25,5 for shape in list_of_shapes: print(shape) 我不知道如何为每一行添加字母。我有一个for循环,用于遍历字符串中的信息以将其打

我有一个如下所示的值列表:

15,100,25.0
-50,-50,50.0
-20,120,70,40
200,-100,25,5
前两行表示圆的值,第三行表示矩形,第四行表示多边形。我希望输出如下所示:

c 15,100,25.0
c -50,-50,50.0
r -20,120,70,40
p 200,-100,25,5
for shape in list_of_shapes: print(shape)
我不知道如何为每一行添加字母。我有一个for循环,用于遍历字符串中的信息以将其打印出来

for shapes in list_of_shapes:
    print(",".join(str(item) for item in shapes))
以下是我的一些代码:

list_of_shapes = []

while p >= 0:

    #Here is a bunch of code

    list_of_shapes.append("c")
    list_of_shapes.append(circle) # appends the data for the circles
while p2 >= 0:
    #bunch of code
    list_of_shapes.append("r")
    list_of_shapes.append(rect) # appends the data for the rectangle
while p3 >= 0:
    #code
    list_of_shapes.append("p")
    list_of_shapes.append(poly) # appends the data for the polygon

    return list_of_shapes
一旦我为所有人这样做,我最终会得到:

c
15,100,25.0
c
-50,-50,50.0
r
-20,120,70,40
p
200,-100,25,5

任何帮助都将不胜感激:)

您所做的只是向列表中添加额外的字符串,而不是向字符串本身追加/预加

从上面的代码中,如果您只需要一个字符串列表,您可能可以这样做:

list_of_shapes = []

while p >= 0:

    #Here is a bunch of code

    list_of_shapes.append("c {0}".format(','.join(circle))) # append data for circles by converting the circle list into a string
while p2 >= 0:
    #bunch of code
    list_of_shapes.append("r {0}".format(','.join(rect))) # append data for rect by converting the rect list into a string
while p3 >= 0:
    #code
    list_of_shapes.append("p {0}".format(','.join(poly))) # append data for polygon by converting the poly list into a string

return list_of_shapes
然后,您可以这样打印此列表:

c 15,100,25.0
c -50,-50,50.0
r -20,120,70,40
p 200,-100,25,5
for shape in list_of_shapes: print(shape)
请注意,在所有
while
块中,您现在只执行一次
list\u形状

这使用允许您以特定方式格式化字符串的


但是,如果您希望单独保留所有列表数据(而不是将其完全设置为字符串),则类似Snakes和Coffee所建议的方法会起作用。

您所做的只是向列表中添加额外的字符串,而不是向字符串本身追加/预加字符串

从上面的代码中,如果您只需要一个字符串列表,您可能可以这样做:

list_of_shapes = []

while p >= 0:

    #Here is a bunch of code

    list_of_shapes.append("c {0}".format(','.join(circle))) # append data for circles by converting the circle list into a string
while p2 >= 0:
    #bunch of code
    list_of_shapes.append("r {0}".format(','.join(rect))) # append data for rect by converting the rect list into a string
while p3 >= 0:
    #code
    list_of_shapes.append("p {0}".format(','.join(poly))) # append data for polygon by converting the poly list into a string

return list_of_shapes
然后,您可以这样打印此列表:

c 15,100,25.0
c -50,-50,50.0
r -20,120,70,40
p 200,-100,25,5
for shape in list_of_shapes: print(shape)
请注意,在所有
while
块中,您现在只执行一次
list\u形状

这使用允许您以特定方式格式化字符串的


但是,如果您希望单独保留所有列表数据(而不是使其完全成为一个字符串),则类似Snakes和Coffee所建议的方法会起作用。

要将所需字母附加到列表中,可以执行以下操作:

list = [15,100,25.0]
list_with_letter = ["c"] + list

要将所需信函附加到列表中,您可以执行以下操作:

list = [15,100,25.0]
list_with_letter = ["c"] + list

问题在于:

while p >= 0:

    #Here is a bunch of code

    list_of_shapes.append("c")
    list_of_shapes.append(circle) # appends the data for the circles
while p2 >= 0:
    #bunch of code
    list_of_shapes.append("r")
    list_of_shapes.append(rect) # appends the data for the rectangle
while p3 >= 0:
    #code
    list_of_shapes.append("p")
    list_of_shapes.append(poly) # appends the data for the polygon

    return list_of_shapes
这将为您提供
['c','r','p',]
。您可以这样做:

while p >= 0:

    #Here is a bunch of code
    list_of_shapes.append(("c", circle)) # appends the data for the circles
while p2 >= 0:
    #bunch of code
    list_of_shapes.append(("r", rect)) # appends the data for the rectangle
while p3 >= 0:
    #code
    list_of_shapes.append(("p", poly)) # appends the data for the polygon

    return list_of_shapes
这基本上是将每个形状与其分类配对。然后,您可以执行以下操作进行打印:

for shape_type, shape in list_of_shapes:
    print("{} {}".format(shape_type, ",".join(str(item) for item in shapes)))

问题在于:

while p >= 0:

    #Here is a bunch of code

    list_of_shapes.append("c")
    list_of_shapes.append(circle) # appends the data for the circles
while p2 >= 0:
    #bunch of code
    list_of_shapes.append("r")
    list_of_shapes.append(rect) # appends the data for the rectangle
while p3 >= 0:
    #code
    list_of_shapes.append("p")
    list_of_shapes.append(poly) # appends the data for the polygon

    return list_of_shapes
这将为您提供
['c','r','p',]
。您可以这样做:

while p >= 0:

    #Here is a bunch of code
    list_of_shapes.append(("c", circle)) # appends the data for the circles
while p2 >= 0:
    #bunch of code
    list_of_shapes.append(("r", rect)) # appends the data for the rectangle
while p3 >= 0:
    #code
    list_of_shapes.append(("p", poly)) # appends the data for the polygon

    return list_of_shapes
这基本上是将每个形状与其分类配对。然后,您可以执行以下操作进行打印:

for shape_type, shape in list_of_shapes:
    print("{} {}".format(shape_type, ",".join(str(item) for item in shapes)))