0
How to change list of tuples in list of str?
5 odpowiedzi
+ 5
a =[("h", ), ("e", ), ("y", )]
b = ''.join([tup[0] for tup in a])
# There's way too many different ways. Please include code because each solution may be different
+ 3
Cut it using join.
The double quote isn't really a character to strip, it's to denote that what's inside is a string.
Knowing that and that tuples ( <val1>, [val2],...) are just containers like lists (not characters to strip either) there are several ways you can do it.
Simon provided a great example that satisfies the requirements. Give his code a lookthough and test it out
+ 2
# A different scenario:
c = [("h", "e", "y", ), ("t", "h", "e", "r", "e", )]
d = ["".join(tup) for tup in c]
print(d)
# As Slick said the details depend on what exactly you want to do.
+ 2
I see. Thank you!
0
Thank you for answers.
I have a file with list of tuples and I should convert it in list of strings. I should write a function.
I wrote something like this:
def some_func(param):
return " ".join(str(i).strip(")").strip("(") for i in param)
param = [("h", "e", "y", ), ("t", "h", "e", "r", "e", )]
print(some_func(param))
My problem was that I didn't know how to cut "()" maybe without strip()