, . ( ) 3- :
d = [
('Hello', 'my', 1),
('Hello', 'world', 2),
('my', 'name', 3),
('my', 'house', 1)
]
For each word in the first position, you want to find the word in the 2nd position most often. Sort the data according to the first word (any order, only to group them), and then by the score (descending).
d.sort(lambda t1,t2: cmp(t2[2],t1[2]) if (t1[0]==t2[0]) else cmp(t1[0],t2[0]))
Finally, iterating through the resulting array, tracking the last word encountered and adding only when a new word is encountered in the 1st position.
L = []
last_word = ""
for word1, word2, count in d:
if word1 != last_word:
L.append((word1,word2))
last_word = word1
print L
By running this code, you will get [('Hello', 'world'), ('my', 'name')].
source
share