I'm a newbie to programming and I'm creating a matchmaking program, to compare personality scores I'm using lists to store, here is my code:
`ps = int(personality_score)
potential_partners = partners.Partners()
while potential_partners.available():
partner = []
personality_scores = []
a = potential_partners.get_name()
f = int(potential_partners.get_personality_score())
if ps == f:
print("This is your match" + a)
else:
g = abs(int(ps - f))
h = int(g)
personality_scores.append(h)
partner.append(a)
partner_compatability =list(zip(personality_scores, partner))
partner_compatability.sort(key = operator.itemgetter(0))
for sub in partner_compatability:
print(sub)`
I've looked at multiple questions and answers related to this and none are working for me, my output from the list is this:
`[['Mary Smith', 1]]
[['Juan Lopez', 5]]
[['Leslie Liu', 11]]
[['Tatiana Ivanov', 15]]
[['Andre Leroy', 11]]
[['Sam Augusta', 7]]
[['Adalbert Weber', 1]]`
but should be ordered from lowest score to highest:
`[['Mary Smith', 1]]
[['Adalbert Weber', 1]]
[['Juan Lopez', 5]]
[['Sam Augusta', 7]]
[['Leslie Liu', 11]]
[['Andre Leroy', 11]]
[['Tatiana Ivanov', 15]]`
(I'm also a newbie programmer and till there is a better answer) you could do what I did: just reverse the order and then sort.
lst = [['Mary Smith', 1],
['Juan Lopez', 5],
['Leslie Liu', 11],
['Tatiana Ivanov', 15],
['Andre Leroy', 11],
['Sam Augusta', 7],
['Adalbert Weber', 1]]
Then:
sorted_lst = [[k,v] for v,k in lst]
sorted_lst.sort()
print(sorted_lst)
You can directly sort the partner_compatability
sorted_list = partner_compatability.sort(key=lambda x: x[1])
I tried like this, as there are no list values provided
In [67]: a
Out[67]: [8, 9, 3]
In [68]: b
Out[68]: [5, 6, 3]
In [69]: partner_compatability = list(zip(a, b))
In [70]: partner_compatability
Out[70]: [(8, 5), (9, 6), (3, 3)]
In [71]: partner_compatability.sort(key=lambda x: x[1])
In [72]: partner_compatability
Out[72]: [(3, 3), (8, 5), (9, 6)]
From your comment,
partner_compatability = [[(1, 'Mary Smith')],
[(5, 'Juan Lopez')],
[(11, 'Leslie Liu')],
[(15, 'Tatiana Ivanov')],
[(11, 'Andre Leroy')],
[(7, 'Sam Augusta')],
[(1, 'Adalbert Weber')]]
partner_compatability.sort(key=lambda x: x[0][0])
In [71]: partner_compatability
Out[71]:
[[(1, 'Mary Smith')],
[(1, 'Adalbert Weber')],
[(5, 'Juan Lopez')],
[(7, 'Sam Augusta')],
[(11, 'Leslie Liu')],
[(11, 'Andre Leroy')],
[(15, 'Tatiana Ivanov')]]
import operator
data = [
['Mary Smith', 1],
['Juan Lopez', 5],
['Leslie Liu', 11],
['Tatiana Ivanov', 15],
['Andre Leroy', 11],
['Sam Augusta', 7],
['Adalbert Weber', 1],
]
data.sort(key = operator.itemgetter(1))
for sub in data:
print(sub)
prints
['Mary Smith', 1]
['Adalbert Weber', 1]
['Juan Lopez', 5]
['Sam Augusta', 7]
['Leslie Liu', 11]
['Andre Leroy', 11]
['Tatiana Ivanov', 15]
Though not well known, the operator function should be faster than the equivalent lambda. There is also attrgetter
. Both can be used when the key should be a tuple of multiple items.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.