regex 我如何从第一个列表中提取所有数字和字符串到另一个列表中?

7rfyedvj  于 2023-10-22  发布在  其他
关注(0)|答案(5)|浏览(125)

我有第一个列表调用OGList,它有一个字符串列表,之后我想从OGList中提取所有数字和字符串到一个字符串列表和一个数字列表中。
我的输入:

OGList = ['A10', 'BMW320i', 'Nissan NSX200', 'Benz 220c']

numlist = []
strlist = []
otherlist = []

for i in OGList:
    for x in i:
        if x.isalpha():
            strlist.append(x)
        elif x.isdigit():
            numlist.append(x) 
        else:
            otherlist.append(x)

print(numlist)
print(strlist)

我的输出:

['1', '0', '3', '2', '0', '2', '0', '0', '2', '2', '0']
['A', 'B', 'M', 'W', 'i', 'N', 'i', 's', 's', 'a', 'n', 'N', 'S', 'X', 'B', 'e', 'n', 'z', 'c']

但我希望它像我想要的输出一样粘在一起。
我想要的输出是:

['10', '320', '200', '220']
['A', 'BMWi', 'NissanNSX', 'Benzc']

如何修复我的代码?

h7appiyu

h7appiyu1#

因为你用regex标记了这个问题,这里有一个替代方法,使用相同的方法来分隔三个组:

import re
OGList = ['A10', 'BMW320i', 'Nissan NSX200', 'Benz 220c']
types = {"chars": "[a-zA-Z]", "nums": "[0-9]", "other": "[^a-zA-Z0-9]"}
res = {k: [''.join(re.findall(types[k], s)) for s in OGList] for k in types}

之后,res是一个包含不同列表的dict:

{
  'chars': ['A', 'BMWi', 'NissanNSX', 'Benzc'],
  'nums': ['10', '320', '200', '220'],
  'other': ['', '', ' ', ' ']
}

请注意,这里没有elifelse,您必须确保模式是互斥的,并涵盖所有情况,否定^,其他组应该做得很好。

7eumitmz

7eumitmz2#

OGList = ['A10', 'BMW320i', 'Nissan NSX200', 'Benz 220c']
    
    dig_lst = []
    letter_lst = []
    
 # with tobias_k suggestion of using negation classes the code becomes very simple.
    for i in OGList:
      
       letter_lst.append(re.sub(r"[^a-zA-Z]", "", i))
       dig_lst.append(re.sub(r'[^0-9]', '', i))
      
    
 print(dig_lst)
 print(letter_lst)
        
 ['10', '320', '200', '220']
 ['A', 'BMWi', 'NissanNSX', 'Benzc']
9udxz4iz

9udxz4iz3#

您需要为每个输入字符串创建一个单独的输出字符串,而不仅仅是将每个字符追加到输出列表中。

OGList = ['A10', 'BMW320i', 'Nissan NSX200', 'Benz 220c']

numlist = []
strlist = []
otherlist = []

for i in OGList:
    numstring = strstring = otherstring = ""
    for x in i:
        if x.isalpha():
            strstring += x
        elif x.isdigit():
            numstring += x
        else:
            otherstring += x
    if strstring:
        strlist.append(strstring)
    if numstring:
        numlist.append(numstring)
    if otherstring:
        otherlist.append(otherstring)

print(numlist)
print(strlist)
2exbekwf

2exbekwf4#

答案是,在每次插入列表之前,都需要添加一个临时str

OGList = ['A10', 'BMW320i', 'Nissan NSX200', 'Benz 220c']

numlist = []
strlist = []
otherlist = []

for i in OGList:
    mystr = ''
    myint = ''
    myother = ''
    for x in i:
        if x.isalpha():
            mystr += x
        elif x.isdigit():
            myint += x
        else:
            myother += x
   if mystr:
       strlist.append(mystr)
   if myint:
       numlist.append(myint)
   if myother:
       otherlist.append(myother)

print(numlist)
print(strlist)
uqxowvwt

uqxowvwt5#

一个奇怪的基于itertools.groupby()的方法怎么样?

from itertools import groupby

OGList = ['A10', 'BMW320i', 'Nissan NSX200', 'Benz 220c']

def get_char_class(char):
    if char.isalpha():
        return 'a'
    if char.isdigit():
        return 'n'
    return 'o'

grouped = [groupby(word, key=get_char_class) for word in OGList]
group_dict = [{k: ''.join(g) for k, g in group} for group in grouped]
result = {}
for d in group_dict:
    for k, v in d.items():
        result.setdefault(k, []).append(v)
# or you can go bonkers and cook up this monstrosity instead of proper loops
# [result.setdefault(k, []).append(v) for d in group_dict for k, v in d.items()]
{'a': ['A', 'i', 'NSX', 'c'], 'n': ['10', '320', '200', '220'], 'o': [' ', ' ']}

相关问题