2

I have a list which contains strings: list = ['string', 'string', 'string', ...]
Those strings are like: 'NumberDescription 33.3'
I want to extract only the numbers without the 'NumberDescription' part.

I've tried it with regex and the filter function with re.match. But this results in an empty list.

dat_re = re.compile(r'\d+.\d')  
dat_list = list(filter(dat_re.match, list))

As I said, I only want the numbers from the list and in a last step, I want to convert the elements of the list into floating numbers.

bharatk
  • 4,202
  • 5
  • 16
  • 30
Corbjn
  • 276
  • 1
  • 10

3 Answers3

1

There are several points here:

  1. Use re.search since re.match only searches for the match at the string start,
  2. Escape the dot as it is a special regex metacharacter
  3. You filter the list only with filter(...), you do not extract the values.
  4. If you plan to find the digit+.digit+ first occurrence you may use a regex like \d+\.\d+
  5. If your items are all in the string number format use s.split()[-1] to get the number, no need for a regex

Use

dat_list = [float(dat_re.search(x).group()) for x in l if dat_re.search(x)]

Or, if the format is fixed

dat_list = [float(x.split()[-1]) for x in l]

See the Python demo:

import re
l = ['string 23.3', 'NumberDescription 33.35']
dat_re = re.compile(r'\d+\.\d+')
dat_list = [float(dat_re.search(x).group()) for x in l if dat_re.search(x)]
print(dat_list)
# => [23.3, 33.35]
print([float(x.split()[-1]) for x in l])
# => [23.3, 33.35]
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
1
list_strings=['1','2','3']
for i in list_strings:
    num_list.append(int(i))
or

list_num = [int(x) for x in list_strings]

Check this sample code once.

Satya
  • 11
  • 3
1

To extract float value directly from the list:

import re
l = ['string 23.3', 'string 33.35', 'string 44.55']
dat_list = list(float(match.group(1)) for match in map(re.compile('(\d+\.\d+)').search, l))
print(dat_list)

OUPUT:

 [23.3, 33.35, 44.55]
Charif DZ
  • 14,415
  • 3
  • 21
  • 40