Fabian Garduño
Fabian Garduño

Reputation: 1

Make Python Compare Lists

Very new to python so I have no idea how to any of this.

Basically I have a list like this:

notes = [60, 62, 64, 60, 67, 69, 71]

I want python to compare that list to 12 other lists that have similar numbers, and find out which of these 12 lists shares the most values with the original list.
How can I go about doing that?

Thanks!

Upvotes: 0

Views: 57

Answers (1)

Lucas Belfanti
Lucas Belfanti

Reputation: 283

I don't know how does the program receive the other lists but this is an example of how could you do this:

notes = [60, 62, 64, 60, 67, 69, 71]

other_notes = [
    [60, 0, 1, 2, 3, 4, 5],
    [60, 62, 1, 2, 3, 4, 5],
    [60, 62, 64, 2, 3, 4, 5],
    [60, 62, 64, 2, 3, 4, 5],
    [60, 62, 64, 60, 3, 4, 5],
    [60, 62, 64, 60, 67, 5],
    [60, 62, 64, 60, 67, 71],
    [60, 0, 1, 2, 3, 4, 5],
    [60, 0, 1, 2, 3, 4, 5],
    [60, 0, 1, 2, 3, 4, 5],
    [60, 0, 1, 2, 3, 4, 5],
    [60, 0, 1, 2, 3, 4, 5]
]

max_value = (-1, -1)

for i in range(len(other_notes)):
    data = set(notes) & set(other_notes[i])
    if len(data) > max_value[0]:
        max_value = (len(data), i)

print("The list that shares more values with the original is {0}. The index position is {1}".
      format(other_notes[max_value[1]], max_value[0]))

Upvotes: 1

Related Questions