我的列表如下:
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']
答案 0 :(得分:0)
尝试一下:
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)
输出:
['15206', '07', '69', '370', '20', '6224']
答案 1 :(得分:0)
尝试一下:
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)
答案 2 :(得分:0)
我更愿意为此使用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)
输出:
['7', '69', '370', '20', '6224']
编辑: 刚读过,您希望保留最少的字符数。只需在内部循环中添加条件:
if x[:j+1] not in patterns or j==len(x)-2:
new_patterns.append(x[j:])
输出:
['07', '69', '370', '20', '6224']