Shon Aufdaded Shon Aufdaded - 3 years ago 106
Python Question

Simplifying Python List Comprehension

Can someone simplify the logic behind this piece of code:

for other in prefs if other!=person ]

I tried implementing it like this

for others in prefs:
if others!=person:
scores=[similarity(prefs,person, others),others]

But it only picks the last element of others.
Btw prefs is a 2D dictionary and scores is supposed to be a list of tuples.

Answer Source

That would be the same as making repeated appends of the tuples to a list:

scores = []
for others in prefs:
    if others!=person:
        scores.append((similarity(prefs, person, others), others))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download