1

If i have a list strings:

first = []
last = []

my_list = ['  abc   1..23',' bcd    34..405','cda        407..4032']

how would i append the numbers flanking the .. to their corresponding lists ? to get:

first = [1,34,407]
last = [23,405,4032]

i wouldn't mind strings either because i can convert to int later

first = ['1','34','407']
last = ['23','405','4032']
O.rka
  • 29,847
  • 68
  • 194
  • 309

4 Answers4

3

Use re.search to match the numbers between .. and store them in two different groups:

import re

first = []
last = []

for s in my_list:
  match = re.search(r'(\d+)\.\.(\d+)', s)
  first.append(match.group(1))
  last.append(match.group(2))

DEMO.

João Silva
  • 89,303
  • 29
  • 152
  • 158
  • what does the r' during re. ? also, what does \d+ do ? – O.rka Sep 23 '12 at 23:23
  • @draconisthe0ry: `\d` matches a single digit, and `\d+` matches *one or more* digits. `r'` is Python's raw string notation (http://en.wikipedia.org/wiki/String_literal#Raw_strings). Take a look at this other question for examples of when not using `r'` can impact your matches: http://stackoverflow.com/questions/2241600/python-regex-r-prefix. – João Silva Sep 23 '12 at 23:37
3

I'd use a regular expression:

import re
num_range = re.compile(r'(\d+)\.\.(\d+)')

first = []
last = []

my_list = ['  abc   1..23',' bcd    34..405','cda        407..4032']

for entry in my_list:
    match = num_range.search(entry)
    if match is not None:
        f, l = match.groups()
        first.append(int(f))
        last.append(int(l))

This outputs integers:

>>> first
[1, 34, 407]
>>> last
[23, 405, 4032]
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
2

One more solution.

for string in my_list:
    numbers = string.split(" ")[-1]
    first_num, last_num = numbers.split("..")
    first.append(first_num)
    last.append(last_num)

It will throw a ValueError if there is a string with no spaces in my_list or there is no ".." after the last space in some of the strings (or there is more than one ".." after the last space of the string).

In fact, this is a good thing if you want to be sure that values were really obtained from all the strings, and all of them were placed after the last space. You can even add a try…catch block to do something in case the string it tries to process is in an unexpected format.

Arseny
  • 5,159
  • 4
  • 21
  • 24
0
 first=[(i.split()[1]).split("..")[0] for i in my_list]
 second=[(i.split()[1]).split("..")[1] for i in my_list]
raton
  • 418
  • 5
  • 14