繁体   English   中英

删除字符串列表的重复部分

[英]Remove repeated parts of an list of strings

我有一个如下列表:

list = ['15206', '15207', '15269', '15370', '15220', '16224']

我需要根据第一个删除每个值的重复部分:

['15206', '07', '69', '370', '20', '6224']

有谁知道我该怎么做? 我正在使用 Python。

这是实际的代码:

lcod = ['15206', '15207', '15269', '15370', '15220', '16224']

poped = {} # this dict store the number of letters removed in each code

# Each iteration removes 1 letter
for i in range(10):

    for seq, cod in enumerate(lcod):
        poped[seq] = 0

        if seq >= 1:
            for seqLetter, letter in enumerate(cod):
                # the "seqLetter <= 3" means that it only can remove 3 letters of each code
                if letter == lcod[0][seqLetter-poped[seq]] and seqLetter <= 3:
                    lcod[seq] = cod[1:]
                    poped[seq] += 1

print(lcod)

返回['15206', '5207', '5269', '370', '220', '224']

尝试这个:

list1 = ['15206', '15207', '15269', '15370', '15220', '16224']
list2 = []
for x in list1:
    count = 0
    value = ''
    for y in range(0,len(x)):
        if list1[0][y] == x[y]:
            if count == 1:
                value = value + x[y]
            elif y >= len(x)-2:
                value = value + x[y]
        else:
            value = value + x[y]
            count = 1
    list2.append(value)
list2[0] = list1[0]
print(list2)

Output:

['15206', '07', '69', '370', '20', '6224']

尝试这个:

List = ['15206', '15207', '15269', '15370', '15220', '16224']

for i, x in enumerate(List.copy()[1:], start=1):
    num = 0
    for a, b in zip(List[0], x):
        if a == b:
            num += 1
        else:
            break
    if num >= 1:
        Run = True
        List[i] = List[i][num:]
print(List)

为此,我更喜欢使用 set 。 它们也可能更快,更容易理解。 我认为它可以更简洁。

lcod = ['15206', '15207', '15269', '15370', '15220', '16224']

patterns = {lcod[0][:i+1] for i in range(len(lcod[0]))}
new_patterns = []
for i, x in enumerate(lcod[1:]):
    for j in range(len(x)):
        if x[:j+1] not in patterns:
            new_patterns.append(x[j:])
            break
print(new_patterns)

Output:

['7', '69', '370', '20', '6224']

编辑:刚才读到您希望保留最少最后一个字符。 只需为内部循环添加条件:

        if x[:j+1] not in patterns or j==len(x)-2:
            new_patterns.append(x[j:])

Output:

['07', '69', '370', '20', '6224']

暂无
暂无

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

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