对齐两个列表并对齐对齐和未对齐的对象执行操作

时间:2015-05-20 13:05:10

标签: python algorithm list

我试图找到一种方法来对齐两个对象列表(按其值排序)并通过比较它的值,如果对象在两个列表中对齐,则执行操作,否则执行另一个操作。例如,孩子的名字和年龄,并使用年龄值来对齐他们。

为简化示例,请尝试使用两个排序的整数值列表:

a = (1,2,3,6,7,11,13)
b = (2,3,4,6,7,9)

我想像这样对齐它们:

+----+----+----+----+----+----+----+----+----+
| 01 | 02 | 03 | xx | 06 | 07 | xx | 11 | 13 |
+----+         +----+         +----+----+----+
| xx | 02 | 03 | 04 | 06 | 07 | 09 | xx | xx |
+----+----+----+----+----+----+----+----+----+

对对齐的值执行操作(例如,将其附加到list1):

list1 = [2,3,6,7]

对未对齐的值执行操作(例如将其附加到list2):

list2 = [1,4,9,11,13]

或者只是在对齐或不对齐时进行打印:

1 = miss-aligned
2 = aligned
3 = aligned
4 = miss-aligned
6 = aligned
7 = aligned
9 = miss-aligned
11 = miss-aligned
13 = miss-aligned

我试着这样做但是......

a = (1,2,3,6,7,11,13)
b = (2,3,4,6,7,9)
list1 = []
list2 = []

# find sames and list a different value
for x in a:
    for y in b:
        if x == y:
            list1.append(x)
            print(x," = aligned")
            break
        if y == b[-1]:
            print(x," = miss-aligned")
            list2.append(x)

# find list b different value
for y in b:
    for x in a:
        if x == y:
            break
        if x == a[-1]:
            print(y," = miss-aligned")
            list2.append(y)

print('Same:',list1)
print('Different:',list2)

...我得到了这个非期望的输出(没有为4和9做好的顺序):

1  = miss-aligned
2  = aligned
3  = aligned
6  = aligned
7  = aligned
11  = miss-aligned
13  = miss-aligned
4  = miss-aligned
9  = miss-aligned
Same: [2, 3, 6, 7]
Different: [1, 11, 13, 4, 9]

我尝试了许多其他方式而没有成功。

*编辑:同一列表中没有重复值

2 个答案:

答案 0 :(得分:2)

由于列表已排序,您可以执行以下操作:

def same_diff(a, b):
    sames = []
    diffs = []
    i = 0
    j = 0
    while i < len(a) and j < len(b):
        if a[i] == b[j]:
            sames.append(a[i])
            i += 1
            j += 1
        elif a[i] > b[j]:
            diffs.append(b[j])
            j += 1
        else:
            diffs.append(a[i])
            i += 1
    diffs += a[i:]
    diffs += b[j:]
    return sames, diffs

a = (1,2,3,6,7,11,13)
b = (2,3,4,6,7,9)

same_diff(a, b)
# ([2, 3, 6, 7], [1, 4, 9, 11, 13])

如果您正在使用值而不是附加剩余值,则可以循环使用它们:

def same_diff(a, b):
    sames = []
    diffs = []
    i = 0
    j = 0
    while i < len(a) and j < len(b):
        if a[i] == b[j]:
            print 'aligned', a[i]
            sames.append(a[i])
            i += 1
            j += 1
        elif a[i] > b[j]:
            print 'miss-aligned', b[j]
            diffs.append(b[j])
            j += 1
        else:
            print 'miss-aligned', a[i]
            diffs.append(a[i])
            i += 1
    while i < len(a):
        print 'miss-aligned', a[i]
        diffs.append(a[i])
        i += 1
    while j < len(b):
        print 'miss-aligned', b[j]
        diffs.append(b[j])
        j += 1
    return sames, diffs

会导致:

same_diff(a, b)

miss-aligned 1
aligned 2
aligned 3
miss-aligned 4
aligned 6
aligned 7
miss-aligned 9
miss-aligned 11
miss-aligned 13
# ([2, 3, 6, 7], [1, 4, 9, 11, 13])

答案 1 :(得分:1)

如果你想获得:

  • 两个列表中的元素
  • 仅存在于其中一个列表中的元素

set会派上用场:

a_set = set(a)
b_set = set(b)

same = sorted(a_set.intersection(b_set))
different = sorted(a_set.symmetric_difference(b_set))

print(same)
print(different)

......你会得到:

[2, 3, 6, 7]
[1, 4, 9, 11, 13]

如果错误对齐发生在哪个列表中,您可以进行简单的减法:

mis_aligned_a = b - a  # [9, 4]
mis_aligned_b = a - b  # [1, 11, 13]