简体   繁体   中英

How to split a comma-separated string into a list of strings?

The input is something like: W,79,V,84,U,63,Y,54,X,91

Output should be: ['W', '79', 'V', '84', 'U', '63', 'Y', '54', 'X', '91']

but my code is filled with comma.

a1=list(input())
print(a1)

my output is ['W', ',', '7', '9', ',', 'V', ',', '8', '4', ',', 'U', ',', '6', '3', ',', 'Y', ',', '5', '4', ',', 'X', ',', '9', '1']

How do I remove the commas? Note:cant use build in functions except input(), split(), list.append(), len(list), range(),print()]

You have to split the input using input().split(',') .

a1=input().split(',')
print(a1)

@MarcSances answered your question. But if you just want to remove the commas from the list just use this

a1 = [x for x in list(input()) if x != ',']
print(a1)

Or do it in 3 lines

user_input = list(input())
a1 = [x for x in user_input if x != ',']
print(a1)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM