searching a list of strings for integers

RedPen

Given the following list of strings:

my_list = ['element0 123 321\n', 'element1 223 32221\n', 'element2 19823 328771\n', ... ]

how can I split each entry into a list of tuples:

[ (123, 321), (223, 32221), (19823, 328771), ... ]

In my other poor attempt, I managed to extract the numbers, but I encountered a problem, the element placeholder also contains a number which this method includes! It also doesn't write to a tuple, rather a list.

numbers = list()

for s in my_list:
    for x in s: 
        if x.isdigit():
            numbers.append((x))
numbers
Willem Van Onsem

We can first build a regex that identifies positive integers:

from re import compile

INTEGER_REGEX = compile(r'\b\d+\b')

Here \d stands for digit (so 0, 1, etc.), + for one or more, and \b are word boundaries.

We can then use INTEGER_REGEX.findall(some_string) to identify all positive integers from the input. Now the only thing left to do is iterate through the elements of the list, and convert the output of INTEGER_REGEX.findall(..) to a tuple. We can do this with:

output = [tuple(INTEGER_REGEX.findall(l)) for l in my_list]

For your given sample data, this will produce:

>>> [tuple(INTEGER_REGEX.findall(l)) for l in my_list]
[('123', '321'), ('223', '32221'), ('19823', '328771')]

Note that digits that are not separate words will not be matched. For instance the 8 in 'see you l8er' will not be matched, since it is not a word.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related