社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

从Python中模式重复的列表中删除重复字符

rwszymczakiii • 5 年前 • 1746 次点击  

我正在监视发送如下数据的串行端口:

['','a','a','a','a','a','a','','b','b','b','b','b','b','b','b',
 '','','c','c','c','c','c','c','','','','d','d','d','d','d','d','d','d',
 '','','e','e','e','e','e','e','','','a','a','a','a','a','a',
 '','','','b','b','b','b','b','b','b','b','b','','','c','c','c','c','c','c',
 '','','','d','d','d','d','d','d','','','e','e','e','e','e','e',
 '','','a','a','a','a','a','a','','b','b','b','b','b','b','b','b',
 '','','c','c','c','c','c','c','','','','d','d','d','d','d','d','d','d',
 '','','e','e','e','e','e','e','','','a','a','a','a','a','a',
 '','','','b','b','b','b','b','b','b','b','b','','','c','c','c','c','c','c',
 '','','','d','d','d','d','d','d','','','e','e','e','e','e','e','','']

我需要把它转换成:

['a','b','c','d','a','b','c','d','a','b','c','d','a','b','c','d']

所以我删除了重复项和空字符串,但也保留了模式自身重复的次数。

我还没搞清楚。有人能帮忙吗?

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/52511
 
1746 次点击  
文章 [ 7 ]  |  最新文章 5 年前
dev
Reply   •   1 楼
dev    5 年前

使用set方法可以从列表中删除重复项

数据=

 ['','a','a','a','a','a','a','','b','b','b','b','b','b','b','b',
 '','','c','c','c','c','c','c','','','','d','d','d','d','d','d','d','d',
 '','','e','e','e','e','e','e','','','a','a','a','a','a','a',
 '','','','b','b','b','b','b','b','b','b','b','','','c','c','c','c','c','c',
 '','','','d','d','d','d','d','d','','','e','e','e','e','e','e',
 '','','a','a','a','a','a','a','','b','b','b','b','b','b','b','b',
 '','','c','c','c','c','c','c','','','','d','d','d','d','d','d','d','d',
 '','','e','e','e','e','e','e','','','a','a','a','a','a','a',
 '','','','b','b','b','b','b','b','b','b','b','','','c','c','c','c','c','c',
 '','','','d','d','d','d','d','d','','','e','e','e','e','e','e','','']

打印(集合(数据))

facehugger
Reply   •   2 楼
facehugger    5 年前
from itertools import groupby
from operator import itemgetter
# data <- your data
a = [k for k, v in groupby(data) if k] # approach 1
b = list(filter(bool, map(itemgetter(0), groupby(data)))) # approach 2
assert a == b
print(a)

结果:

['a', 'b', 'c', 'd', 'e', 'a', 'b', 'c', 'd', 'e', 'a', 'b', 'c', 'd', 'e', 'a', 'b', 'c', 'd', 'e']
bdbd
Reply   •   3 楼
bdbd    5 年前

类似于:

result = []
before = ''
for e in data:
    if e and e != before:
        result.append(e)
        before = e
print(result)
Osman Mamun
Reply   •   4 楼
Osman Mamun    5 年前

你可以用 itertools.groupby :

如果你的名单是 ll

ll = [i for i in ll if i]
out = []
for k, g in groupby(ll, key=lambda x: ord(x)):
    out.append(chr(k))
print(out)
#prints ['a', 'b', 'c', 'd', 'e', 'a', 'b', 'c', 'd', 'e', ...
lkdhruw
Reply   •   5 楼
lkdhruw    5 年前

你需要这样的东西

li = ['','a','a','a','a','a','a','','b','b','b','b','b','b','b','b','','','c','c','c','c','c','c','','','','d','d','d','d','d','d','d','d','','','e','e','e','e','e','e','','','a','a','a','a','a','a','','','','b','b','b','b','b','b','b','b','b','','','c','c','c','c','c','c','','','','d','d','d','d','d','d','','','e','e','e','e','e','e','','','a','a','a','a','a','a','','b','b','b','b','b','b','b','b','','','c','c','c','c','c','c','','','','d','d','d','d','d','d','d','d','','','e','e','e','e','e','e','','','a','a','a','a','a','a','','','','b','b','b','b','b','b','b','b','b','','','c','c','c','c','c','c','','','','d','d','d','d','d','d','','','e','e','e','e','e','e','','']

new_li = []
e_ = ''

for e in li:
    if len(e) > 0 and e_ != e:
        new_li.append(e)
        e_ = e

print(new_li)

输出

['a', 'b', 'c', 'd', 'e', 'a', 'b', 'c', 'd', 'e', 'a', 'b', 'c', 'd', 'e', 'a', 'b', 'c', 'd', 'e']
murtuzakothawala
Reply   •   6 楼
murtuzakothawala    5 年前

您可以在列表上循环并添加适当的连接。对于您期望的响应,您只需要确定前一个字符是否与当前字符不同

    current_sequence = ['','a','a','a','a','a','a','','b','b','b','b','b','b','b','b','','','c','c','c','c','c','c','','','','d','d','d','d','d','d','d','d','','','e','e','e','e','e','e','','','a','a','a','a','a','a','','','','b','b','b','b','b','b','b','b','b','','','c','c','c','c','c','c','','','','d','d','d','d','d','d','','','e','e','e','e','e','e','','','a','a','a','a','a','a','','b','b','b','b','b','b','b','b','','','c','c','c','c','c','c','','','','d','d','d','d','d','d','d','d','','','e','e','e','e','e','e','','','a','a','a','a','a','a','','','','b','b','b','b','b','b','b','b','b','','','c','c','c','c','c','c','','','','d','d','d','d','d','d','','','e','e','e','e','e','e','','']

sequence_list = []
for x in range(len(current_sequence)):
    if current_sequence[x]:
        if current_sequence[x] != current_sequence[x-1]:
            sequence_list.append(current_sequence[x])

print(sequence_list)
kaya3
Reply   •   7 楼
kaya3    5 年前

下面是一个使用列表理解和 itertools.zip_longest :仅当元素不是空字符串且不等于下一个元素时才保留该元素。您可以使用迭代器跳过第一个元素,以避免切片列表的成本。

from itertools import zip_longest

def remove_consecutive_duplicates(lst):
    ahead = iter(lst)
    next(ahead)
    return [ x for x, y in zip_longest(lst, ahead) if x and x != y ]

用法:

>>> remove_consecutive_duplicates([1, 1, 2, 2, 3, 1, 3, 3, 3, 2])
[1, 2, 3, 1, 3, 2]
>>> remove_consecutive_duplicates(my_list)
['a', 'b', 'c', 'd', 'e', 'a', 'b', 'c', 'd', 'e', 'a', 'b', 'c', 'd',
 'e', 'a', 'b', 'c', 'd', 'e']

我假设没有由空字符串分隔的重复项(例如。 'a', '', 'a' ),或者不希望删除此类重复项。如果这个假设是错误的,那么您应该首先过滤掉空字符串:

>>> example = ['a', '', 'a']
>>> remove_consecutive_duplicates([ x for x in example if x ])
['a']