I hope to convert a list of strings to a list of a long string. For example, I hope to convert ['c++', 'python', 'sklearn', 'java'] to ["'c++', 'python', 'sklearn', 'java'"]. Namely, the original list have some strings, the target list should have a long string which includes the small string.
I have tried the ' '.join([str(elem) for elem in s]), but the results are not in a list.
s = ['c++', 'python', 'sklearn', 'java']
listToStr = ' '.join([str(elem) for elem in s])
print listToStr
The expected output is:
["'c++', 'python', 'sklearn', 'java'"]
The actual output is:
c++ python sklearn java
This can be done using one line of code and no loop/join by turning the list to a string and taking the brackets off. Then wrap it in a list.
print [str(s)[1:-1]]
Join it with commas, add the single quotes and put it in a list:
myList= [", ".join(["'"+elem+"'" for elem in s])]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.