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 None
s):
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
Index of Duplicates Items in a Python List
Problem with Multi Threaded Python App and Socket Connections
How to Remove Packages Installed with Python's Easy_Install
How to Format a String Using a Dictionary in Python-3.X
How to Set the Aspect Ratio in Matplotlib
Why Do I Get Typeerror: Can't Multiply Sequence by Non-Int of Type 'Float'
How to Color Python Logging Output
Difference Between Methods and Functions, in Python Compared to C++
Pandas Make New Column from String Slice of Another Column
How to Make File Creation an Atomic Operation
How to Resolve Typeerror: Can Only Concatenate Str (Not "Int") to Str
How to Check the Versions of Python Modules
Custom Filter in Django Admin on Django 1.3 or Below
Python Os.Path.Join on Windows
Keyerror: 'Tcl_Library' When I Use Cx_Freeze
What Do the Python File Extensions, .Pyc .Pyd .Pyo Stand For