Python 如何从元素中删除一个字符以及列表中另一个元素中的相应字符?
我需要将列表中的每个元素与其他元素进行比较。因此,比较Python 如何从元素中删除一个字符以及列表中另一个元素中的相应字符?,python,list,character,del,Python,List,Character,Del,我需要将列表中的每个元素与其他元素进行比较。因此,比较sample[0]和sample[1],sample[0]和sample[2],sample[1]和sample[2] 如果比较中的任何一对具有$,则$,需要删除相应的元素 例如: Sample = ['A$$N','BBBC','$$AA'] 这可能不是最漂亮的代码,但可以完成这项工作 for i in range(len(sample1)): for j in range(i + 1, len(sample1)):
sample[0]
和sample[1]
,sample[0]
和sample[2]
,sample[1]
和sample[2]
如果比较中的任何一对具有$
,则$
,需要删除相应的元素
例如:
Sample = ['A$$N','BBBC','$$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
谢谢,它工作得很好。我更喜欢这样的代码而不是理解
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)