【发布时间】:2010-09-30 14:43:31
【问题描述】:
我想拆分这样的字符串
'foofo21'
'bar432'
'foobar12345'
进入
['foofo', '21']
['bar', '432']
['foobar', '12345']
有人知道在 python 中执行此操作的简单方法吗?
【问题讨论】:
我想拆分这样的字符串
'foofo21'
'bar432'
'foobar12345'
进入
['foofo', '21']
['bar', '432']
['foobar', '12345']
有人知道在 python 中执行此操作的简单方法吗?
【问题讨论】:
我会通过以下方式使用re.match 来解决这个问题:
import re
match = re.match(r"([a-z]+)([0-9]+)", 'foofo21', re.I)
if match:
items = match.groups()
print(items)
>> ("foofo", "21")
【讨论】:
re.split('(\d+)', t)
【讨论】:
str().rstrip() 方法的速度大约快了 4 倍。此外,它不需要再次导入。
另一种选择:
>>> [re.split(r'(\d+)', s) for s in ('foofo21', 'bar432', 'foobar12345')]
[['foofo', '21', ''], ['bar', '432', ''], ['foobar', '12345', '']]
【讨论】:
[filter(None, re.split(r'(\d+)', s)) for s in ('foofo21','a1')] 删除空字符串
>>> r = re.compile("([a-zA-Z]+)([0-9]+)")
>>> m = r.match("foobar12345")
>>> m.group(1)
'foobar'
>>> m.group(2)
'12345'
所以,如果您有一个具有该格式的字符串列表:
import re
r = re.compile("([a-zA-Z]+)([0-9]+)")
strings = ['foofo21', 'bar432', 'foobar12345']
print [r.match(string).groups() for string in strings]
输出:
[('foofo', '21'), ('bar', '432'), ('foobar', '12345')]
【讨论】:
我总是提出 findall() =)
>>> strings = ['foofo21', 'bar432', 'foobar12345']
>>> [re.findall(r'(\w+?)(\d+)', s)[0] for s in strings]
[('foofo', '21'), ('bar', '432'), ('foobar', '12345')]
请注意,我使用的正则表达式比之前的大多数答案更简单(键入更少)。
【讨论】:
这是一个简单的函数,可以从任意长度的字符串中分隔多个单词和数字,re 方法只分隔前两个单词和数字。我认为这将在未来对其他人有所帮助,
def seperate_string_number(string):
previous_character = string[0]
groups = []
newword = string[0]
for x, i in enumerate(string[1:]):
if i.isalpha() and previous_character.isalpha():
newword += i
elif i.isnumeric() and previous_character.isnumeric():
newword += i
else:
groups.append(newword)
newword = i
previous_character = i
if x == len(string) - 2:
groups.append(newword)
newword = ''
return groups
print(seperate_string_number('10in20ft10400bg'))
# outputs : ['10', 'in', '20', 'ft', '10400', 'bg']
【讨论】:
不使用正则表达式,使用 isdigit() 内置函数,仅当开始部分是文本,后面部分是数字时才有效
def text_num_split(item):
for index, letter in enumerate(item, 0):
if letter.isdigit():
return [item[:index],item[index:]]
print(text_num_split("foobar12345"))
输出:
['foobar', '12345']
【讨论】:
import re
s = raw_input()
m = re.match(r"([a-zA-Z]+)([0-9]+)",s)
print m.group(0)
print m.group(1)
print m.group(2)
【讨论】:
这有点长,但对于字符串中有多个随机放置的数字的情况更通用。此外,它不需要导入。
def getNumbers( input ):
# Collect Info
compile = ""
complete = []
for letter in input:
# If compiled string
if compile:
# If compiled and letter are same type, append letter
if compile.isdigit() == letter.isdigit():
compile += letter
# If compiled and letter are different types, append compiled string, and begin with letter
else:
complete.append( compile )
compile = letter
# If no compiled string, begin with letter
else:
compile = letter
# Append leftover compiled string
if compile:
complete.append( compile )
# Return numbers only
numbers = [ word for word in complete if word.isdigit() ]
return numbers
【讨论】:
这是解决该问题的简单方法,不需要regex:
user = input('Input: ') # user = 'foobar12345'
int_list, str_list = [], []
for item in user:
try:
item = int(item) # searching for integers in your string
except:
str_list.append(item)
string = ''.join(str_list)
else: # if there are integers i will add it to int_list but as str, because join function only can work with str
int_list.append(str(item))
integer = int(''.join(int_list)) # if you want it to be string just do z = ''.join(int_list)
final = [string, integer] # you can also add it to dictionary d = {string: integer}
print(final)
【讨论】:
item = int(item) # searching for integers in your string!!!!???