在列表中删除重复项,忽略其他项目。

huangapple go评论71阅读模式
英文:

Removing a repetitive item in a list, ignores other items

问题

I have a list of words in Polish and I want to remove the single words in this list. That means I only want sentences. That's why I have an If with this condition if len(item.split()) <= 1:. The problem occurs when I run the function. It removes only this word: Powrót and ignores the words that come after it like Energetyka.

_webTextList=['MOTORYZACJA', 'Szukaj w serwisie', 'Powrót', 'Energetyka', 'Powrót', 'Gazownictwo', 'Powrót', 'Górnictwo', 'Powrót', 'Hutnictwo', 'Powrót', 'Nafta', 'Powrót', 'Chemia', ...]  #the list of words and sentences.

def remove_one_words_from_list(_webTextList):
    new_list = [item for item in _webTextList if len(item.split()) > 1]
    return new_list

I can not figure out what I'm doing wrong. Thanks for the help.

英文:

i have a list of words in Polish and i want to remove the single words in this list. that means i only want sentences. that's why i have a an If with this condition if len(item.split())&lt;=1: . the Problem occourse when i run the function. it removes only this word: Powr&#243;t and ignores the words that come after it like Energetyka.

_webTextList=[&#39;MOTORYZACJA&#39;, &#39;Szukaj w serwisie&#39;, &#39;Powr&#243;t&#39;, &#39;Energetyka&#39;, &#39;Powr&#243;t&#39;, &#39;Gazownictwo&#39;, &#39;Powr&#243;t&#39;, &#39;G&#243;rnictwo&#39;, &#39;Powr&#243;t&#39;, &#39;Hutnictwo&#39;, &#39;Powr&#243;t&#39;, &#39;Nafta&#39;, &#39;Powr&#243;t&#39;, &#39;Chemia&#39;, ...]#the list of words and sentenses.

def remove_one_words_from_list(_webTextList):
 for item in _webTextList:
   if len(item.split())&lt;=1:
     _webTextList.remove(item)
 return _webTextList   

i can not figure out what i'm doing wrong. tnx for the help.

答案1

得分: 3

Output: [&#39;Szukaj w serwisie&#39;]

英文:

Use a list comprehension and keep the strings that contain a space:

_webTextList = [&#39;MOTORYZACJA&#39;, &#39;Szukaj w serwisie&#39;, &#39;Powr&#243;t&#39;, &#39;Energetyka&#39;, &#39;Powr&#243;t&#39;, &#39;Gazownictwo&#39;, &#39;Powr&#243;t&#39;, &#39;G&#243;rnictwo&#39;, &#39;Powr&#243;t&#39;, &#39;Hutnictwo&#39;, &#39;Powr&#243;t&#39;, &#39;Nafta&#39;, &#39;Powr&#243;t&#39;, &#39;Chemia&#39;]

_webTextList = 
展开收缩

NB. if you really want to use the length of split's output as condition:

展开收缩
.

Output: [&#39;Szukaj w serwisie&#39;]

答案2

得分: 1

你正在遇到的问题与在迭代列表时修改列表有关。在循环内部修改列表可能会导致意外行为,例如跳过某些元素。

可以改为创建一个新的列表:

def remove_one_words_from_list(_webTextList):
    new_list = []
    for item in _webTextList:
        if len(item.split()) > 1:
            new_list.append(item)
    return new_list

_webTextList = ['MOTORYZACJA', 'Szukaj w serwisie', 'Powrót', 'Energetyka', 'Powrót', 'Gazownictwo', 'Powrót', 'Górnictwo', 'Powrót', 'Hutnictwo', 'Powrót', 'Nafta', 'Powrót', 'Chemia', ...]

filtered_list = remove_one_words_from_list(_webTextList)
print(filtered_list)
英文:

The issue you're experiencing is related to modifying the list while iterating over it. Modifying the list inside the loop can cause unexpected behavior, such as skipping certain elements.

Instead create a new list:

def remove_one_words_from_list(_webTextList):
    new_list = []
    for item in _webTextList:
        if len(item.split()) &gt; 1:
            new_list.append(item)
    return new_list

_webTextList = [&#39;MOTORYZACJA&#39;, &#39;Szukaj w serwisie&#39;, &#39;Powr&#243;t&#39;, &#39;Energetyka&#39;, &#39;Powr&#243;t&#39;, &#39;Gazownictwo&#39;, &#39;Powr&#243;t&#39;, &#39;G&#243;rnictwo&#39;, &#39;Powr&#243;t&#39;, &#39;Hutnictwo&#39;, &#39;Powr&#243;t&#39;, &#39;Nafta&#39;, &#39;Powr&#243;t&#39;, &#39;Chemia&#39;, ...]

filtered_list = remove_one_words_from_list(_webTextList)
print(filtered_list)

huangapple
  • 本文由 发表于 2023年6月29日 20:39:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76581153.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定