简体   繁体   English

一种删除列表中仅一个元素的连续重复项的pythonic方法

[英]A pythonic way to delete successive duplicates of only one element in a list

I know there are many other similar questions posted, but there is a difference in mine that makes it unsolvable with their answers.我知道发布了许多其他类似的问题,但我的不同之处在于他们的答案无法解决。

I have several lists of characters that may have multiple consecutive spaces, of which I need to keep only one.我有几个可能有多个连续空格的字符列表,我只需要保留一个。 Repetitions of any other character should remain.应保留任何其他字符的重复。 I did it in the following way:我是通过以下方式做到的:

myList = ['o', 'e', 'i', ' ', ' ', ' ', 'l', 'k', ' ', ' ', ' ', ' ', ' ', 'j', 'u']
myList_copy = [myList[0]]

for i in range(1, len(myList):
    if not(myList[i] == ' ' and myList[i-1] == ' '):
        myList_copy.append(myList[i])

which successfully gives me这成功地给了我

['o', 'e', 'i', ' ', 'l', 'k', ' ', 'j', 'u', ' ']

I don't really think this is a very good, fast way to do it.我真的不认为这是一个非常好的、快速的方法。

I have seen posts like this one (and others) which have similar questions.我看过类似这样的帖子(和其他帖子)有类似的问题。 However, see that I actually need to remove only repeated spaces.但是,请注意我实际上只需要删除重复的空格。 Maybe what I need help with is using groupby to do this, but that's why the new post.也许我需要帮助的是使用 groupby 来做到这一点,但这就是新帖子的原因。

Thanks in advance.提前致谢。

Yes,Using groupby is a good idea:是的,使用groupby是个好主意:

import itertools

myList = ['o', 'e', 'i', ' ', ' ', ' ', 'l', 'k', ' ', ' ', ' ', ' ', ' ', 'j', 'u']
result = [key for key,group in itertools.groupby(myList)])

# ['o', 'e', 'i', ' ', 'l', 'k', ' ', 'j', 'u']

If you want to get another elements also duplicate,you can use this:如果你想让另一个元素也重复,你可以使用这个:

myList = ['o', 'e', 'i', 'i' , ' ', ' ', ' ', 'l', 'k', ' ', ' ', ' ', ' ', ' ', 'j', 'u']
result = []
for key,group in itertools.groupby(myList):
    if key != ' ': # ' 'string
        for j in group:
            result.append(j)
    else: result.append(key)
print(result)

Another simple?另一个简单? way to do it:方法:

  1. Join each item in the myList to create a string加入myList中的每个项目以创建一个字符串
  2. Split the string by whitespace用空格分割字符串
  3. Join with a space加入空格
  4. Convert the string into a list将字符串转换为列表
myList = ['o', 'e', 'i', ' ', ' ', ' ', 'l', 'k', ' ', ' ', ' ', ' ', ' ', 'j', 'u']

new = list(' '.join(''.join(myList).split()))
print(new)
['o', 'e', 'i', ' ', 'l', 'k', ' ', 'j', 'u']

this is the same as yours but in one line这和你的一样,但在一行

myList_copy = [myList[x] for x in range(len(myList)) if not(myList[x] == ' ' and myList[x-1] == ' ')]

How about using numpy?使用 numpy 怎么样? Try this code.试试这个代码。

import numpy as np
myList = ['o', 'e', 'i', ' ', ' ', ' ', 'l', 'k', ' ', ' ', ' ', ' ', ' ', 'j', 'u']
myList = np.array(myList)
myList = [myList[0]] + list(myList[1:][~((myList[1:] == myList[:-1]) & (myList[1:] == ' '))])
print(myList)

You can use zip in a list comprehension to compare each character with the previous one and exclude spaces that are preceded by another space:您可以在列表推导中使用 zip 将每个字符与前一个字符进行比较,并排除前面有另一个空格的空格:

myList = [ c for p,c in zip([""]+myList,myList) if (p,c) != (' ',' ') ]

same approach can be used on a string可以在字符串上使用相同的方法

myList = [ c for p,c in zip("."+myString, myString) if (p,c) != (' ',' ') ]

but split() would probably be more concise if you have a string and want a string as output:但是如果您有一个字符串并且想要一个字符串为 output,则 split() 可能会更简洁:

myString = " ".join(myString.split())

What about using a pandas Series and shifting the results?使用 pandas 系列并改变结果怎么样?

import pandas as pd
serie = pd.Series(['o', 'e', 'i', ' ', ' ', ' ', 'l', 'k', ' ', ' ', ' ', ' ', ' ', 'j', 'u'])
index = ~(serie == serie.shift(1))
serie = serie[index]

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

相关问题 用一个元素创建列表或将其保留为空的“ pythonic”方法是什么? - What is the “pythonic” way to create a list with one element or just keep it empty? Pythonic方法删除列表中的反向重复项 - Pythonic way of removing reversed duplicates in list 从列表列表中删除项目:pythonic方式 - delete items from list of list: pythonic way 在列表中查找重复项,并在每一个之后添加连续的字母字符 - Find duplicates in list and add successive alphabetic characters after each one 当期望列表仅包含单个元素时,获取列表元素的Python方法? - Pythonic way to get list element when expectation is that list contains only a single element? 如果满足条件,修改列表元素的Python方法 - Pythonic way to modify list element if condition is met 什么是Pythonic方法来删除列表中的双重副本但允许三元组/更大? - What is a Pythonic way to remove doubled duplicates in a list but allow triplets/greater? 有没有更 Pythonic 的方法来查找文件夹的重复项并将它们的路径作为元组列表返回? - Is there a more pythonic way to find duplicates of folders and return their paths as a list of tuples? 识别列表中连续重复项的最 Pythonic 方法是什么? - What's the most Pythonic way to identify consecutive duplicates in a list? Pythonic方式从列表中获取唯一的项目 - Pythonic way to take the only item from a list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM