Различается ли порядок двух python списков визуально? - PullRequest
0 голосов
/ 01 апреля 2020

У меня есть два списка, которые имеют одинаковые элементы, но в другом порядке.

foo = ["A", "B", "C", "D", "E", "F"]
bar = ["C", "A", "B", "D", "E", "F"]

Теперь я хочу знать, какие элементы были поменяны местами, и представить их на графике, с некоторыми визуальными подсказками для чтобы было легче заметить перетасовку.

График должен иметь цветные линии между рассеивающими элементами. Примерно так ниже, но с цветами и в matplotlib.

   A   B   C  D  E  F
v---\---\--^
C    A  B     D  E  F

Ответы [ 2 ]

0 голосов
/ 01 апреля 2020

Расширяя пример буквенных знаков для поиска позиций, вы можете использовать аннотирование, чтобы указать позиции массива, а затем использовать индекс, чтобы найти позицию свопов в массиве.

Как показано в следующем примере:

import matplotlib.pyplot as plt

foo = ["A", "B", "C", "D", "E", "F"]

bar = ["C", "A", "B", "D", "E", "F"]


# put data point names on plots
for num in range(len(bar)):
    plt.annotate(bar[num], # this is the text
        (num,1), # this is the point to label
        textcoords="offset points", # how to position the text
        xytext=(0,10), # distance from text to points (x,y)
        ha='center') # horizontal alignment can be left, right or center

for num in range(len(foo)):
    plt.annotate(foo[num], # this is the text
        (num,2), # this is the point to label
        textcoords="offset points", # how to position the text
        xytext=(0,10), # distance from text to points (x,y)
        ha='center') # horizontal alignment can be left, right or center

plt.plot(foo,len(foo)*[1],'bo-')
plt.plot(bar,len(bar)*[2],'bo-')


for f, b, in zip(foo, bar):
    if f != b:
        print(f,b)
        bar_position = bar.index(f)
        foo_position = foo.index(f)

        swappositions = [bar_position,foo_position]

        plt.plot(swappositions,[1,2],'r--') # indicates dashed red line for swapped elements


#turn off ticks
plt.tick_params(
    axis='x',          # changes apply to the x-axis
    which='both',      # both major and minor ticks are affected
    bottom=False,      # ticks along the bottom edge are off
    top=False,         # ticks along the top edge are off
    labelbottom=False) # labels along the bottom edge are off

plt.show()

Дает следующий участок:

enter image description here

0 голосов
/ 01 апреля 2020

Для начала будут перечислены все элементы, которые не сопоставлены с одним и тем же значением

for f, b, in zip(foo, bar):
    if f != b:
        print(f, b)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...