在比较以下元组时,我面临一个问题:
chains = ['A','B','C','D']
proper_list = ['ABBA','BDDA','CDDA','ABBA']
corrupted_list = ['ABBA','CDDA','BDDA','ABBA']
proper_set = tuple(zip(chains, proper_list))
corrupted_set = tuple(zip(chains, corrupted_list))两者的产出如下:
(('A', 'ABBA'), ('B', 'BDDA'), ('C', 'CDDA'), ('D', 'ABBA')),
(('A', 'ABBA'), ('B', 'CDDA'), ('C', 'BDDA'), ('D', 'ABBA'))我想以某种方式打印更新的元组,其中算法可以找到并指示分配给值的不适当元素,如下所示:
(('A', 'ABBA'), ('C', 'CDDA'), ('B', 'BDDA'), ('D', 'ABBA'))或至少以适当的顺序交出清单:
['A','C','B','D']由于正确/损坏列表中的元素可能重复,所以我不能(或不能)使用dict。
假设:
列表和元素的
你有什么建议来解决这个问题?
发布于 2022-07-04 13:58:35
不一定能理解你的问题,但这似乎得到了你想要的结果:
chains = ['A','B','C','D']
proper_list = ['ABBA','BDDA','CDDA','ABBA']
corrupted_list = ['ABBA','CDDA','BDDA','ABBA']
proper_set = tuple(zip(chains, proper_list))
corrupted_set = tuple(zip(chains, corrupted_list))
result = ()
result_indexes = []
def idxExluded(val, ex):
for i in range(len(corrupted_list)):
if (corrupted_list[i] == val and (i not in ex)):
return i
return -1
for s in proper_set:
key = s[0]
val = s[1]
for cor_set in corrupted_set:
if (cor_set[0] == key and cor_set[1] == val):
result = result + ((key, val), )
result_indexes.append(idxExluded(val, result_indexes))
break
if (cor_set[1] == val):
result = result + ((key, val), )
result_indexes.append(idxExluded(val, result_indexes))
break;
print(result) # Mapped result
# (('A', 'ABBA'), ('B', 'BDDA'), ('C', 'CDDA'), ('D', 'ABBA'))
def mapIdxs(val):
return result_indexes[result.index(val)]
result_sorted = list(result)
result_sorted.sort(key=mapIdxs)
result_sorted = tuple(result_sorted)
print(result_sorted) # Sorted mapped result
# (('A', 'ABBA'), ('C', 'CDDA'), ('B', 'BDDA'), ('D', 'ABBA'))很明显,它没有被优化,也没有使用任何内置函数,我只是做了一个快速的POC。
发布于 2022-07-04 14:30:38
我认为您需要修复这条链,以防它在使用序列时出现问题,所以如下所示
chains = ['A','B','C','D']
proper_list = ['ABBA','BDDA','CDDA','ABBA']
corrupted_list = ['ABBA','CDDA','BDDA','ABBA']
proper_set = tuple(zip(chains, proper_list))
corrupted_set = tuple(zip(chains, corrupted_list))
result = []
for x,y in corrupted_set:
if (x,y) in proper_set:
# if the current tuple is actually a proper tuple just append it
result.append((x,y))
else:
# if it's not a proper tuple we need to fix the chain so find the sequence in the proper list then pick the correspoding chain
i = proper_list.index(y)
result.append((chains[i],y))
print(result)https://stackoverflow.com/questions/72857423
复制相似问题