如何从python列表的每个元素中删除前缀?

2024-09-28 17:06:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个python列表,其中包含以下项目:

[ 1.1 ] 1. a electronic bill presentment system.
[ 1.2 ] a network.
[ 1.3 ] a plurality of first stations, each associated with a respective one of a plurality of users and operable to transmit first requests for bills of its associated user via the network.
[ 1.4 ] a central network station configured to receive the transmitted first requests for bills and to transmit, responsive to each of the received first requests, bill availability information for the associated user via the network, wherein each of the plurality of first stations is configured to receive the transmitted bill availability information for its associated user and is operable to transmit second requests for bills of its associated user via the network.
[ 1.5 ] a plurality of second network stations, each associated with a respective one of a plurality of billers, configured to receive the transmitted second requests for bills and to transmit, responsive thereto, the requested bills of the associated user via the network.
[ 1.6 ] wherein the bill availability information for the associated user identifies those of the plurality of billers having a bill available for that user without identifying an amount of the bill of each of the identified billers for the associated user.
[ 2.1 ] 2. a method for presenting electronic bills.
[ 2.2 ] storing, at a plurality of different locations, electronic bills of a plurality of different billers for a plurality of different users.
[ 2.3 ] storing identifiers of the stored electronic bills at a location different than the plurality of different locations.
[ 2.4 ] transmitting a first request for the stored electronic bills for a first of the plurality of users.
[ 2.5 ] transmitting one or more of the stored identifiers of the stored electronic bills for the first user responsive to the transmitted first request, each of the transmitted one or more identifiers being associated with a respective one of the stored electronic bills of a different one of the plurality of billers.
[ 2.6 ] transmitting a second request for at least one of the stored electronic bills identified by the transmitted one or more identifiers.
[ 2.7 ] transmitting the at least one identified stored electronic bill responsive to the transmitted second request.
[ 2.8 ] wherein the transmitted one or more identifiers identifies the stored electronic bills without identifying an amount of the identified stored electronic bills.

我只需要删除每个项目的前缀,但不知道如何删除这个。你知道吗

例如

我需要删除[2.8](space) [2.7](space)。上面的每一行代表列表中的项目。 同样像[ 1.1 ] 1. a electronic bill presentment system.,我需要删除[ 1.1 ] 1.

我的代码函数删除如下,我使用的逻辑是先使用空格拆分,然后删除非alpha值。你知道吗

但它不能正常工作。你知道吗

请帮忙。你知道吗

TextDictionaryValuesList = list(TextDictionary.values()) 
# You can make a test list using above given items of mylist

def remove_non_alpha(splitlist):
    for j in range(0, len(splitlist)):
        if(splitlist[j].isalpha()):
            splitlist[j] = splitlist[j]
        else:
            splitlist[j] = ""       

    return splitlist


for i in range(0, len(TextDictionaryValuesList)):

    print(TextDictionaryValuesList[i])

    splitlist = TextDictionaryValuesList[i].split(" ")
    splitlist = remove_non_alpha(splitlist)
    TextDictionaryValuesList[i] = splitlist

print(TextDictionaryValuesList)

Tags: ofthetoforonefirstelectronicbill
3条回答
import re

data = ["[ 1.1 ] 1. a electronic bill presentment system.","[ 1.2 ] a network."]

result = [re.search('[a-z,A-Z].*',i).group(0) for i in data]
print(result)

应该使用正则表达式将模式替换为空字符串

>>> re.sub(r'\[\s?\d\.\d\s?]\s?(\d(\.\s)?)?', '', '[ 1.1 ] 1. a electronic bill presentment system.')
'a electronic bill presentment system.'

下面是一种使用^{}的方法:

import re
l = ['[ 1.1 ] 1. a electronic bill presentment system.','[ 1.2 ] a network.']

[re.sub(r'\[\s*\d+\.*\d*\s*\]\s+(?:\d+\.\s*)?', '', s) for s in l]
# ['a electronic bill presentment system.', 'a network.']

demo


使用更大的字符串列表进行测试:

l = ['[ 1.1 ] 1. a electronic bill presentment system.',\
'[ 1.2 ] a network.',\
'[ 1.3 ] a plurality of first stations, each associated with a respective one of a plurality of users and operable to transmit first requests for bills of its associated user via the network.',\
'[ 1.5 ] a plurality of second network stations, each associated with a respective one of a plurality of billers, configured to receive the transmitted second requests for bills and to transmit, responsive thereto, the requested bills of the associated user via the network.',\
'[ 1.6 ] wherein the bill availability information for the associated user identifies those of the plurality of billers having a bill available for that user without identifying an amount of the bill of each of the identified billers for the associated user.',\
'[ 2.1 ] 2. a method for presenting electronic bills.']

[re.sub(r'\[\s*\d+\.*\d*\s*\]\s+(?:\d+\.\s*)?', '', s) for s in l]

['a electronic bill presentment system.',
 'a network.',
 'a plurality of first stations, each associated with a respective one of a plurality of users and operable to transmit first requests for bills of its associated user via the network.',
 'a plurality of second network stations, each associated with a respective one of a plurality of billers, configured to receive the transmitted second requests for bills and to transmit, responsive thereto, the requested bills of the associated user via the network.',
 'wherein the bill availability information for the associated user identifies those of the plurality of billers having a bill available for that user without identifying an amount of the bill of each of the identified billers for the associated user.',
 'a method for presenting electronic bills.']

相关问题 更多 >