简体   繁体   中英

Splitting Python string into list of pairs

I have data in the following format:

"22.926 g 47.377 g 73.510 g 131.567 g 322.744 g"

What I would like to do is to split it into a list such that value and units are grouped together, eg:

["22.926 g","47.377 g","73.510 g","131.567 g","322.744 g"]

Of course, in Python 2.7, I can do this the hard way:

result = []
tokens = "22.926 g 47.377 g 73.510 g 131.567 g 322.744 g".split()
for index,item in enumerate(tokens[::2]):
    result.append(item+" "+tokens[index+1])

but I hoped that there is a slightly more elegant way for doing this?

With regex (and the re.findall method)you could obtain what you need :

import re
text="22.926 g 47.377 g 73.510 g 131.567 g 322.744 g"
re.findall("\d+\.\d+ g", text)
>>>['22.926 g', '47.377 g', '73.510 g', '131.567 g', '322.744 g']

But keep in mind that when solving a problem with regex we often end with 2 problems ;)

In one-line! Split the string, and use list comprehension to get the desired output by removing all g and appending g !

s="22.926 g 47.377 g 73.510 g 131.567 g 322.744 g"
>>> [x+' g' for x in s.split() if x!='g']

Output

['22.926 g', '47.377 g', '73.510 g', '131.567 g', '322.744 g']

what about splitting according to " g" and strip/filter out empty fields, re-add the g suffix afterwards, in one line:

["{} g".format(x.strip()) for x in "22.926 g 47.377 g 73.510 g 131.567 g 322.744 g".split(" g") if x]

result:

['22.926 g', '47.377 g', '73.510 g', '131.567 g', '322.744 g']
a = "22.926 g 47.377 g 73.510 g 131.567 g 322.744 g".split()
c = [" ".join((v, g)) for v,g in zip(a[:-1:2], a[1::2])]
data = "22.926 g 47.377 g 73.510 g 131.567 g 322.744 g"
sep = 'g'
result = ['{digit} {sep}'.format(digit=d.strip(), sep=sep) for d in data[:-1].split(sep)]

Not very elegant, but how about:

s = "22.926 g 47.377 g 73.510 g 131.567 g 322.744 g"
l = s.strip('g').split('g')
l = [item + 'g' for item in l]

我不知道这是否是最美丽的方式,但它是一个单线解决方案:

[s.lstrip() + "g" for s in text.split("g") if s]

I don't think there is something wrong with your solution, but the usual approach would probably consists of a regular expression

import re

input = "22.926 g 47.377 g 73.510 g 131.567 g 322.744 g"
result = p.findall(r'(\d+\.\d+ g)', input)
print(result)

prints

['22.926 g', '47.377 g', '73.510 g', '131.567 g', '322.744 g']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM