I have a list ['101,122,133,145,150,222,200']
i want to make list as below
['101','122','133','145','150','222','200']
or [101,122,133,145,150,222,200]
CodePudding user response:
You can use .split()
for your required output.
rlist = ['101,122,133,145,150,222,200']
reqlist = []
for _ in rlist:
nlist = _.split(',')
reqlist.append(nlist)
print(reqlist)
This prints
[['101', '122', '133', '145', '150', '222', '200']]
CodePudding user response:
add this code
array = ['101,122,133,145,150,222,200'][0].split(',')
print(array)
CodePudding user response:
l = ['101,122,133,145,150,222,200']
new_list =l[0].split(',')
>>> ['101', '122', '133', '145', '150', '222', '200']
CodePudding user response:
To obtain the elements in string format you can use str.split to to define a delimiter and generate a list based on substrings around it:
list = ['101,122,133,145,150,222,200']
list[0].split(",")
To obtain the numbers in int format, you may cast them. This can be achieved by iterating the previous list, creating a new int object and adding it to a new list.
int_list = []
for x in list[0].split(","):
number = int(x)
int_list.append(number)
Or more succintly with a list comprehension:
list = ['101,122,133,145,150,222,200']
int_list = [int(x) for x in list[0].split(",")]
CodePudding user response:
For the list of integers you could do this:
a = ['101,122,133,145,150,222,200']
b = list(map(int, a[0].split(',')))
print(b)
Output:
[101, 122, 133, 145, 150, 222, 200]