简体   繁体   English

Python - 如何从长字符串列表中删除特定的第一个单词

[英]Python - how to remove a specific first word from list of long strings

I have a list as shown below.我有一个如下所示的列表。 I want to remove the first word which will always be 'Value' from my list of strings.我想从我的字符串列表中删除第一个始终为“值”的单词。 I am able to do this by indexing (code below) but is there any way to do this by identifying the actual phrase 'Value'?我可以通过索引(下面的代码)来做到这一点,但是有没有办法通过识别实际的短语“价值”来做到这一点?

Example list示例列表

my_lst=['Value today is great weather.','Value final dataset.','Value for money.']

new_lst=[]
for i in my_lst:
    new_lst.append(i[6:])

You want to remove a prefix, so...你想删除一个前缀,所以......

>>> 'Value foo bar'.removeprefix('Value ')
'foo bar'

A generic way to remove prefix words could be done as:删除前缀词的通用方法可以如下完成:

target_word = "Value"
if mystring.startswith(target_word):
    mystring = mystring[len(target_word):]

You can loop this and rename your variables as required.您可以循环并根据需要重命名变量。

Like this?像这样?

my_lst=['Value today is great weather.','Value final dataset.','Value for money.']

new_lst=[]
for i in my_lst:
    if i[0:5]=="Value":
        new_lst.append(i[6:])
print(new_lst)

Consider using regular expression substitution:考虑使用正则表达式替换:

new_lst = [re.sub('^Value\s+', '', w) for w in my_lst]
#['today is great weather.', 'final dataset.', 'for money.']

Explanation: "^Value\s+" is the word "Value" at the beginning of a string ( "^" ), followed by one or more ( "+" ) spaces ( "\s" ).说明: "^Value\s+"是字符串开头的单词"Value" ( "^" ),后跟一个或多个 ( "+" ) 空格 ( "\s" )。

You can use list comprehension with str.startswith() as:您可以将列表理解str.startswith()一起使用:

my_lst = ['Value today is great weather.', 'Value final dataset.', 'Value for money.']
my_word = 'Value'

new_list = [s[len(my_word):] for s in my_lst if s.startswith(my_word)]
# where `new_list` will give you list as:
#    [' today is great weather.', ' final dataset.', ' for money.']

If you are on Python 3.9+ , you can use str.removeprefix (skipping str.startswith check) as:如果您使用的是Python 3.9+ ,则可以使用str.removeprefix (跳过str.startswith check)作为:

my_lst = ['Value today is great weather.', 'Value final dataset.', 'Value for money.']
my_word = 'Value'

new_list = [s.removeprefix(my_word) for s in my_lst]
# [' today is great weather.', ' final dataset.', ' for money.']

what about this one:这个如何:

new_list = [word.split('Value ')[-1] for word in mylst]

You can removeprefix like this (only for Python >3.9):您可以像这样删除前缀(仅适用于removeprefix >3.9):

>>> my_lst = ['Value today is great weather.', 'Value final dataset.', 'Value for money.']
>>> phrase = 'Value '
>>> [x.removeprefix(phrase) for x in my_lst]
['today is great weather.', 'final dataset.', 'for money.']

You may use:您可以使用:

new_lst = " ".join(my_lst.split()[1:])

It will remove the first world of a sentence.它将删除句子的第一个世界。

use regular expression sub and look for Value at the start of the sentence使用正则表达式 sub 并在句子的开头查找 Value

my_list=['Value today is great weather.','Value final dataset.','Value for money.']

for i in range(len(my_list)):
    my_list[i]=re.sub('^Value ','',my_list[i])

print(my_list)

output: output:

['today is great weather.', 'final dataset.', 'for money.']

You can use:您可以使用:

my_lst.replace("value", "") my_lst.replace("值", "")

"Replace" is used to replace one specific word with another word. “替换”用于用另一个词替换一个特定的词。

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

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