如何从元素中删除一个字符和列表中另一个元素中的相应字符?
How to remove a character from element and the corresponding character in another element in a list?
Sample = ['A$$N','BBBC','$$AA']
我需要将每个元素与列表中的每个其他元素进行比较。所以比较sample[0]
和sample[1]
、sample[0]
和sample[2]
、sample[1]
和sample[2]
。
如果比较中任意对有$
,则$
,需要剔除对应的元素
例如:
sample[0] and sample[1] Output1 : ['AN','BC']
sample[0] and sample[2] Output2 : ['N', 'A']
sample[1] and sample[2] Output3 : ['BC','AA']
for i in range(len(sample1)):
for j in range(i + 1, len(sample1)):
if i == "$" or j == "$":
#Need to remove "$" and the corresponding element in the other list
#Print the pairs
这可能不是最漂亮的代码,但可以胜任。
from itertools import combinations
sample = ['A$$N','BBBC','$$AA']
output = []
for i, j in combinations(range(len(sample)), 2):
out = ['', '']
for pair in zip(sample[i], sample[j]):
if '$' not in pair:
out[0] += pair[0]
out[1] += pair[1]
output.append(out)
print(output)
Sample = ['A$$N','BBBC','$$AA']
我需要将每个元素与列表中的每个其他元素进行比较。所以比较sample[0]
和sample[1]
、sample[0]
和sample[2]
、sample[1]
和sample[2]
。
如果比较中任意对有$
,则$
,需要剔除对应的元素
例如:
sample[0] and sample[1] Output1 : ['AN','BC']
sample[0] and sample[2] Output2 : ['N', 'A']
sample[1] and sample[2] Output3 : ['BC','AA']
for i in range(len(sample1)):
for j in range(i + 1, len(sample1)):
if i == "$" or j == "$":
#Need to remove "$" and the corresponding element in the other list
#Print the pairs
这可能不是最漂亮的代码,但可以胜任。
from itertools import combinations
sample = ['A$$N','BBBC','$$AA']
output = []
for i, j in combinations(range(len(sample)), 2):
out = ['', '']
for pair in zip(sample[i], sample[j]):
if '$' not in pair:
out[0] += pair[0]
out[1] += pair[1]
output.append(out)
print(output)