Reputation: 209
I have a list of values that looks like this:
15,100,25.0
-50,-50,50.0
-20,120,70,40
200,-100,25,5
The first two lines represent values for a circle, the third for a rectangle and the fourth for a polygon. I would like the output to look like this:
c 15,100,25.0
c -50,-50,50.0
r -20,120,70,40
p 200,-100,25,5
I'm not sure how I can add the letter for each of the lines. I have a for-loop that I'm using to go through the information in the string to print them out.
for shapes in list_of_shapes:
print(",".join(str(item) for item in shapes))
Here's some of my code:
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
Once I do this for all of them, I end up getting:
c
15,100,25.0
c
-50,-50,50.0
r
-20,120,70,40
p
200,-100,25,5
Any help would be greatly appreciated :)
Upvotes: 1
Views: 11705
Reputation: 79893
What you're doing is just adding extra strings to the list, rather than appending/prepending to the strings themselves.
From your code above, you could probably do this, if you just wanted a list of strings:
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
You would then just print this list out like this:
for shape in list_of_shapes: print(shape)
Note that in all while
blocks, you now only execute list_of_shapes.append
once.
This uses str.format()
which allows you to format a string a specific way.
However, if you want to retain all the list data separately (rather than make it entirely a string), something like what Snakes and Coffee suggests would work.
Upvotes: 5
Reputation: 8837
The problem is in here:
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
This gives you ['c', <circle>, 'r', <rect>, 'p', <poly>]
. You could do this:
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
This basically pairs each shape with its classification. Then you can print by doing this:
for shape_type, shape in list_of_shapes:
print("{} {}".format(shape_type, ",".join(str(item) for item in shapes)))
Upvotes: 2
Reputation: 655
To append your desired letter to list you can do:
list = [15,100,25.0]
list_with_letter = ["c"] + list
Upvotes: 0