简体   繁体   English

在Python中删除列表中的第一个字符

[英]Removing the first char some words on a list in Python

Could someone please help me with this code in python:有人可以帮我在 python 中使用此代码:

I have the following list.我有以下清单。

filtered_sent = ['colombia', 'nnueva', 'revolución', 'nindustrial', 'npropuestas', 'foco', 'tecnologías', 'convergentes', 'ne', 'industrias', 'nvolumen', 'ncolombia', 'nartista', 'federico', 'uribe', 'npropuestas', 'foco', 'tecnologías', 'convergentes', 'ne', 'industrias', 'ntomo', 'ncolombia', 'nueva', 'nrevolución', 'nindustrial', 'vicepresidencia', 'república', 'colombia', 'ministerio', 'ciencia', 'tecnología', 'innovación', 'elías', 'niño', 'ruiz', 'jean', 'paul', 'allain', 'josé', 'alejandro', 'montoya', 'juan', 'luis']

and I want to remove the first character of each word that starts with 'n'.我想删除每个以“n”开头的单词的第一个字符。 (for example: 'nnueva', 'nartista', 'nindustrial', etc) (例如:“nnueva”、“nartista”、“nindustrial”等)

I have tried this code but it is removing, from ALL words of the list, the first character of the word so it doesn´t work for me:我试过这段代码,但它从列表的所有单词中删除了单词的第一个字符,所以它对我不起作用:

lista_A = [] 
for it in filtered_sent:
    for j in it:
        if j[0] == 'n':
            lista_A.append(it[1:])

I would really appreciate if someone could show me my mistake, thanks :)如果有人能告诉我我的错误,我将不胜感激,谢谢:)

There is some logical issue with your code in if block. if 块中的代码存在一些逻辑问题。

It's better to compare both index and first char of the word.最好比较单词的索引和第一个字符。

This code works.此代码有效。

lista_A = [] 
for it in filtered_sent:
    for i,j in enumerate(it):
        if j == 'n' and i == 0:
            lista_A.append(it)
        
print(lista_A)

你可以试试:

filtered = [i[1:] if i[0] == 'n' else i for i in filtered_sent]

使用内置的str函数:

new_list = [word.removeprefix('n') for word in filtered_sent]

你可以试试:

list(map(lambda word: word[1:] ,filter(lambda x:(x[0]=="n"),filtered_sent)))

Answering the question, the mistake is the second for loop ("for j in it:").回答这个问题,错误是第二个 for 循环(“for j in it:”)。 There's no need to iterate through each letter of each word.无需遍历每个单词的每个字母。 Instead of checking only if the first letter of each word is equal to 'n', your code is checking if any letter of the word is equal to 'n'.您的代码不是仅检查每个单词的第一个字母是否等于 'n',而是检查该单词的任何字母是否等于 'n'。 At each iteration of the inner for loop, both j and j[0] are a reference to the same specific letter.在内部 for 循环的每次迭代中, j 和 j[0] 都是对同一特定字母的引用。

You can add some print statements to your code to better understand what is happening.您可以在代码中添加一些打印语句以更好地了解正在发生的事情。

lista_A = [] 
for it in filtered_sent:
    for j in it:
        print(f'it: {it}')
        print(f'j: {j}')
        print(f'j[0]: {j[0]}')
        if j[0] == 'n':
            lista_A.append(it[1:])

Try removing the second for loop, and change the if clause to check the first character of the word:尝试删除第二个 for 循环,并更改 if 子句以检查单词的第一个字符:

lista_A = [] 
for it in filtered_sent:
  if it[0] == 'n':
    lista_A.append(it[1:])

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

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