简体   繁体   English

从列表元素中删除数字

[英]Removing digits from list elements

I have a list of job titles (12,000 in total) formatted in this way: 我有一个以这种方式格式化的职位名单(总共12,000):
Career_List = ['1) ABLE SEAMAN', '2) ABRASIVE GRADER', '3) ABRASIVE GRINDER']

How do I remove the numbers, parentheses, and spaces from the list elements so that I end up with this output: 如何从列表元素中删除数字,括号和空格,以便最终得到此输出:
Career_List_Updated = ['ABLE SEAMAN', 'ABRASIVE GRADER', 'ABRASIVE GRINDER']

I know that I am unable to simply remove the first three characters because I have more than ten items in my list. 我知道我无法简单地删除前三个字符,因为我的列表中有十个以上的项目。

利用str.lstrip()和其余strip函数接受多个字符作为参数的事实。

Career_List_Updated =[career.lstrip('0123456789) ') for career in Career_List]

Split each career at the first space; 在第一个空间分开每个职业; keep the rest of the line. 保持其余部分。

Career_List = ['1) ABLE SEAMAN', '2) ABRASIVE GRADER', '3) ABRASIVE GRINDER', '12000) ZEBRA CLEANER']
Career_List_Updated = []

for career in Career_List:
    job = career.split(' ', 1)
    Career_List_Updated.append(job[1])

print Career_List_Updated

Output: 输出:

['ABLE SEAMAN', 'ABRASIVE GRADER', 'ABRASIVE GRINDER', 'ZEBRA CLEANER']

One-line version: 单行版:

Career_List_Updated = [career.split(' ', 1)[1] \
                       for career in Career_List]

We want to find the first index that STOPS being a bad character and return the rest of the string, as follows. 我们想找到STOPS是一个坏字符的第一个索引,并返回字符串的其余部分,如下所示。

def strip_bad_starting_characters_from_string(string):
    bad_chars = set(r"'0123456789 )") # set of characters we don't like
    for i, char in enumerate(string):
        if char not in bad_chars
            # we are at first index past "noise" digits
            return string[i:]

career_list_updated = [strip_bad_starting_characters_from_string(string) for string in career_list]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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