在比较python中的两个元素时,如何指示一个元组中的顺序变化?

发布于 2025-02-12 18:32:12 字数 782 浏览 1 评论 0原文

我面临一个比较以下元素的问题:

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。

假设:

  • 列表和元素的长度将始终是
  • Corrputed_list中任何元素的顺序

相等,您可能建议解决此问题的建议?

I am facing a problem with comparing following tuples:

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))

The output of both is as follows:

(('A', 'ABBA'), ('B', 'BDDA'), ('C', 'CDDA'), ('D', 'ABBA')),
(('A', 'ABBA'), ('B', 'CDDA'), ('C', 'BDDA'), ('D', 'ABBA'))

I would like to somehow print out the updated tuple, where an algorithm could find and indicate improper element assigned to value, as follows:

(('A', 'ABBA'), ('C', 'CDDA'), ('B', 'BDDA'), ('D', 'ABBA'))

or at least yield the list with proper order:

['A','C','B','D']

As the elements in proper/corrupted lists may be duplicated I cannot (or am not able to) use dict.

Assumptions:

  • lengths of both lists and elements will always be equal
  • the order of any element in corrputed_list may differ

What could you suggest to solve this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尸血腥色 2025-02-19 18:32:12

不确定要理解您的问题,但这似乎可以得到您想要的结果:

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'))

extisouly它没有优化或使用任何内置功能,我只是做了一个快速的POC

Not sure to understand your question but this seems to get the result you want:

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'))

Obvisouly it is not optimised nor using any builtin functions, I just did a quick POC

若沐 2025-02-19 18:32:12

我认为您需要修复链条,以防它使用该序列有问题,所以类似的东西

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)

I think you need to fix the chain in case it has an issue using the sequence so something like this

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文