简体   繁体   English

当每个字符相同时,如何从列表中删除元素(Python)?

[英]How to remove elements from list when each character is identical (Python)?

sample = ['AAAA','ABCB','CCCC','DDEF']

I need to eliminate all elements where every character is identical to itself in the element eg. 我需要消除元素中每个字符都与自身相同的所有元素。 AAAAA,CCCCC AAAAA,CCCCC

output = ['ABCB','DDEF']

sample1 =[]
for i in sample:
    for j in i:
       if j == j+1:    #This needs to be corrected to if all elements in i identical to each other i.e. if all "j's" are the same
        sample1.pop(i)

print sample 打印样本

 sample = ['AAAA','ABCB','CCCC','DDEF']
 output = [sublist for sublist in sample if len(set(sublist)) > 1]

EDIT to answer comment. 编辑以回答评论。

sample = [['CGG', 'ATT'], ['ATT', 'CCC']]
output = []
for sublist in sample:
    if all([len(set(each)) > 1 for each in sublist]):
        output.append(sublist)

# List comprehension (doing the same job as the code above)
output2 = [sublist for sublist in sample if 
           all((len(set(each)) > 1 for each in sublist))]
sample = ['AAAA','ABCB','CCCC','DDEF']

sample1 = []
for i in sample:
    if len(set(i)) > 1:
        sample1.append(i)

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

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