Current code:
reps = ['x 5', 'x 5', 'x 3', 'x 5', 'x 5', 'x 5 ']
b = [90, 122, 135, 146, 168, 191]
print(str(list(zip(b,reps))).replace(',',''))
here is the current output:
[(90 'x 5') (112 'x 5') (135 'x 3') (146 'x 5') (168 'x 5') (191 'x 5 ')]
here is my goal output:
[(90 x 5) (112 x 5) (135 x 3) (146 x 5) (168 x 5) (191 x 5 )]
How would I remove those single quotes?
I tried using replace but I'm not sure how to both replace the commas separating the weight and the reps, as well as the quotations around the rep range.
I also tried replacing the quotations in the list reps on a separate line of code but once it is printed within the zip they were added back.
CodePudding user response:
Not a thing of beauty, but if your only goal is to end up with exactly the output you specified then this will do that:
reps = ['x 5', 'x 5', 'x 3', 'x 5', 'x 5', 'x 5 ']
b = [90, 122, 135, 146, 168, 191]
# creates a list of strings of the form: "(X x Y)"
arr = [f"({y} {x})" for x, y in zip(reps, b)]
# prints the list as a single string surrounded by brackets
print(f"[{' '.join(arr)}]")
Output:
[(90 x 5) (122 x 5) (135 x 3) (146 x 5) (168 x 5) (191 x 5 )]
CodePudding user response:
It sounds like cosmetic output, but the following will work for your case:
print('[' ' '.join(f"({x} {y})" for x, y in zip(b, reps)) ']')
[(90 x 5) (122 x 5) (135 x 3) (146 x 5) (168 x 5) (191 x 5 )]
CodePudding user response:
if you want that exact format you can use
print(
str(list(map(lambda tup: f"({str(tup[0])} {tup[1]})",list(zip(b,reps))))).replace("'",'').replace(",",'')
)
print(str([f"({x} {y})" for x, y in zip(b, reps)]).replace(",", "").replace("'",""))
Maybe is not the most efficient way, but it works
CodePudding user response:
TL;DR - You can't, you will always see it with quotation marks, else you will need to literally print the tuple as a set of characters.
Reading from the docs of the print method (I've marked the important sentence):
print(*objects, sep=' ', end='\n', file=None, flush=False)
Print objects to the text stream file, separated by sep and followed by end. sep, end, file, and flush, if present, must be given as keyword arguments. All non-keyword arguments are converted to strings like str() does and written to the stream, separated by sep and followed by end. Both sep and end must be strings; they can also be None, which means to use the default values. If no objects are given, print() will just write end.
Because x 5
makes no valid object in python, it is being converted to a string, to 'x 5'
.