Problem Formulation and Solution Overview
π Related Tutorial: How to convert a list of integers to a list of strings in Python?
β₯οΈ Info: Are you AI curious but you still have to create real impactful projects? Join our official AI builder club on Skool (only $5): SHIP! - One Project Per Month
Example:
- Given string:
"30022145, 30022192, 30022331, 30022345, 30022359" - Desired list:
[30022145, 30022192, 30022331, 30022345, 30022359]
Method 1: Use List Comprehension
This option uses Python’s built-in functions split() and int() in conjunction with List Comprehension to convert a String into a List of Integers.
string_ids = ("30022145, 30022192, 30022331, 30022345, 30022359").split(',')
int_ids = [int(x) for x in string_ids]
print(int_ids)Above declares a String containing five (5) Finxter IDs inside brackets:("30022145, 30022192, 30022331, 30022345, 30022359").
π‘Note: This string is wrapped inside brackets () to allow us to append another function (split()).
Then split() is appended to the above String, and the comma (,) character is passed as an argument. This argument lets split() know where to break the String. The results save to string_ids.
At this point, strings_ids contains a List of Strings:
['30022145', ' 30022192', ' 30022331', ' 30022345', ' 30022359'] |
Next, List Comprehension is used to loop through each element (ID) and convert it to an Integer value (int(x)). The results save to int_ids and are output to the terminal as a List of Integers.
[30022145, 30022192, 30022331, 30022345, 30022359] |
Method 2: Use Regex
This option requires an additional library, regex, to perform the task. The function re.findall() is called to locate, extract and convert the data into a List of Integers.
import re
string_ids = "30022145 30022192 30022331 30022345 30022359"
string_ids = re.findall('\d+', string_ids)
int_ids = [int(x) for x in string_ids]
print(int_ids)Above, the regex library is imported. Click here if this library requires installation.
Then, a string containing five (5) Finxter IDs separated by the space character (' ') is declared and saved to string_ids.
Next, re.findall() is called and passed two (2) arguments:
- A
regexpattern ('\d+'). This pattern searches for one (1) or more decimals (integers) inside the second argument (each match is separated with the space (' ') character). - The second argument
string_ids, declared earlier.
At this point, strings_ids contains a List of Strings.
['30022145', '30022192', '30022331', '30022345', '30022359'] |
Finally, List Comprehension is used to loop through each element (ID) and convert it to an Integer value (int(x)). The results save to int_ids and are output to the terminal as a List of Integers.
[30022145, 30022192, 30022331, 30022345, 30022359] |
Method 3: Use isdigit()
This option uses List Comprehension in conjunction with isdigit() to loop through the String to extract and convert the data.
string_ids = "30022145 30022192 30022331 30022345 30022359" int_ids = [int(x) for x in string_ids.split() if x.isdigit()] print(int_ids)
Above, a String containing five (5) Finxter IDs separated by the space character (' ') is declared and saved to string_ids.
Then, List Comprehension is used to loop through string_ids split into substrings (elements) and convert each element to an Integer if it is a digit (x.isdigit()). The results save to int_ids and are output to the terminal as a List of Integers.
π‘Note: By default, split() assumes the separating character is a space.
If true, no argument needs to be passed.
[30022145, 30022192, 30022331, 30022345, 30022359] |
β¨ A Finxter Favorite!
Minimum Code, Maximum Results!
Method 4: Use List, Map() and Split()
This option uses List, map(), and split() to extract and convert the String into a List of Integers.
string_ids = "30022145 30022192 30022331 30022345 30022359" int_ids = list(map(int, string_ids.split())) print(int_ids)
Above, a String containing five (5) Finxter IDs separated by the space character (' ') is declared and saved to string_ids.
Then, the map() function is called and passed two (2) parameters:
- A function:
int(). - An
iterable:string_ids. Thisiterableis split into aListof Strings on the space character (‘ ‘).
The results convert to a List of Integers, saves to int_ids and outputs to the terminal.
[30022145, 30022192, 30022331, 30022345, 30022359] |
Method 5: Use NumPy
This option requires an additional library, NumPy, to perform the task. The function np.fromstring() is called to extract and convert the data into a List of Integers.
string_ids = "30022145 30022192 30022331 30022345 30022359" int_ids = np.fromstring(string_ids, dtype=int, sep=' ') print(int_ids)
Above, the NumPy library is imported. Click here if this library requires installation.
Then, a String containing five (5) Finxter IDs separated by the space character (' ') is declared and saved to string_ids.
Next, np.fromstring() is called and passed three (3) arguments:
- The String:
string_ids. - The Data Type:
dtype=int. - The Separating Characer:
sep= ' '.
The results save to int_ids and are output to the terminal.
[30022145 30022192 30022331 30022345 30022359] |
Bonus: Split String Every Nth Character
What happens if the String does not have a separating character: it’s all one (1) string? How could we split this String every eight (8) characters? With slicing, of course!
string_ids = "3002214530022192300223313002234530022359" int_ids = [int(string_ids[i:i+8]) for i in range(0, len(string_ids), 8)] print(int_ids)
Above, a String containing five (5) Finxter IDs joined together is declared and saved to string_ids.
Then, slicing is used to iterate through string_ids, splitting it every 8th character and converting it to an Integer. The results save to int_ids and output to the terminal.
[30022145, 30022192, 30022331, 30022345, 30022359] |
β¨ Another Finxter Favorite!
Minimum Code, Maximum Results!
Summary
Programmer Humor
π±ββοΈ Programmer 1: We have a problem
π§ββοΈ Programmer 2: Letβs use RegEx!
π±ββοΈ Programmer 1: Now we have two problems
… yet – you can easily reduce the two problems to zero as you polish your “RegEx Superpower in Python“. π