问题
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, e.g.:
["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?
回答1:
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])]
回答2:
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 ;)
回答3:
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']
回答4:
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']
回答5:
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)]
回答6:
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]
回答7:
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']
回答8:
I don't know if this is the most beautiful way but it's a one-line solution:
[s.lstrip() + "g" for s in text.split("g") if s]
来源:https://stackoverflow.com/questions/47202751/splitting-python-string-into-list-of-pairs