"Pythonic" Method to Parse a String of Comma-Separated Integers into a List of Integers

Is there a way I can convert a list of string into a list of int in python/flask?

Access the first element Then split string at , and map with integer datatype it returns a lazy iterator convert it into list

data = list(map(int,data[0].split(",")))

Convert comma-separated values into integer list in pandas dataframe

There are 2 steps - split and convert to integers, because after split values are lists of strings, solution working well also if different lengths of lists (not added Nones):

df['qty'] = df['qty'].apply(lambda x: [int(y) for y in x.split(',')])

Or:

df['qty'] = df['qty'].apply(lambda x: list(map(int, x.split(','))))

Alternative solutions:

df['qty'] = [[int(y) for y in x.split(',')] for x in df['qty']]
df['qty'] = [list(map(int, x.split(','))) for x in df['qty']]

How to convert a string of space- and comma- separated numbers into a list of int?

Split on commas, then map to integers:

map(int, example_string.split(','))

Or use a list comprehension:

[int(s) for s in example_string.split(',')]

The latter works better if you want a list result, or you can wrap the map() call in list().

This works because int() tolerates whitespace:

>>> example_string = '0, 0, 0, 11, 0, 0, 0, 0, 0, 19, 0, 9, 0, 0, 0, 0, 0, 0, 11'
>>> list(map(int, example_string.split(','))) # Python 3, in Python 2 the list() call is redundant
[0, 0, 0, 11, 0, 0, 0, 0, 0, 19, 0, 9, 0, 0, 0, 0, 0, 0, 11]
>>> [int(s) for s in example_string.split(',')]
[0, 0, 0, 11, 0, 0, 0, 0, 0, 19, 0, 9, 0, 0, 0, 0, 0, 0, 11]

Splitting on just a comma also is more tolerant of variable input; it doesn't matter if 0, 1 or 10 spaces are used between values.

Python - Convert string with numbers into a list of integers

Split the problem into two parts.

1 Split the text into a list with the data you need and filter the other.

num_list = my_string.split(',')

2 Convert the data into what type you want it

integers = [int(x) for x in num_list]

Then you might clean it up

integs = [int(x) for x in my_string.split(',')]

Convert string of numbers to list of numbers

As simple as this:

in Python2:

print [int(s) for s in l.split(',')]

and in Python3 simply wrapped with a parentheses:

print([int(s) for s in l.split(',')])

Convert string (comma separated) to int list in pandas Dataframe

Since your column contains strings and integers, you want probably something like this:

def to_integers(column_value):
if not isinstance(column_value, int):
return [int(v) for v in column_value.split(',')]
else:
return column_value

df.loc[:, 'column_name'] = df.loc[:, 'column_name'].apply(to_integers)

Python parse comma-separated number into int

>>> a = '1,000,000'
>>> int(a.replace(',', ''))
1000000
>>>

How to convert comma-delimited string to list in Python?

You can use the str.split method.

>>> my_string = 'A,B,C,D,E'
>>> my_list = my_string.split(",")
>>> print my_list
['A', 'B', 'C', 'D', 'E']

If you want to convert it to a tuple, just

>>> print tuple(my_list)
('A', 'B', 'C', 'D', 'E')

If you are looking to append to a list, try this:

>>> my_list.append('F')
>>> print my_list
['A', 'B', 'C', 'D', 'E', 'F']


Related Topics



Leave a reply



Submit