## Trending News

# Calculate dissimilarity between universities with PYTHON!?

picture added with question

### 1 Answer

- husoskiLv 710 months ago
Cool. They're trying to teach you to think in Python when programming in Python. A lot of courses will try to teach Python as if it were C or Java or some such.

Your first friend is that the max() built-in function will compute the maximum of any iterable. So you can start your high-level thinking with:

dissimilarity = max(abs(x - y) for x, y in <something>)

That <something> needs to be an expression that generates a sequence of (x,y) pairs to compare. Here's where your next friend comes in. The zip() built-in function does exactly that. It produces a sequence of tuples from a two or more sequences, with the first elements of each sequence, the second elements of each sequence, and so on. It stops when it reaches the end of the shortest sequence. Try this at at Python 3.x interactive prompt:

>>> zip([1, 2, 3], [2, 5, 7])

<zip object at 0x000001EE25C2D688>

Not helpful, huh? Zip returns a generator object that will produce the tuples one at a time, as needed. You can use the list() function to see all of what it will generate:

>>> list(zip([1, 2, 3], [2, 5, 7]))

[(1, 2), (2, 5), (3, 7)]

To get a feel for what that looks like when you're iterating over it, try this:

>>> for x, y in zip([1, 2, 3], [2, 5, 7]):

. . . . . . print('x =', x, ', y =', y)

x = 1 , y = 2

x = 2 , y = 5

x = 3 , y = 7

Don't type those dots. I'm just approximating indentation. A for loop (and a for expression in a generator) will let you split a tuple into separate variables, just like in an "unpacking" assignment statement:

x, y = 1, 2 # same idea in a simpler setting

So zip(uni_1, uni_2) is what you need for <something> above. To see what the input to max() will look like, use list() instead:

>>> list(abs(x - y) for x, y in zip([1, 2, 3], [2, 5, 7]))

[1, 3, 4]

See? That produced [abs(1-2), abs(2-5), abs(3-7)], and those are the numbers you want to find the maximum of. So, the final version is:

>>> max(abs(x - y) for x, y in zip([1, 2, 3], [2, 5, 7]))

4

Replace [1, 2, 3] and [2, 5, 7] with the names of the sequences for two universities and you have your dissimilarity expression.

- Login to reply the answers