Python: Get most frequent item in list
问题 I've got a list of tuples, and I want to get the most frequently occurring tuple BUT if there are "joint winners" it should pick between them at random. tups = [ (1,2), (3,4), (5,6), (1,2), (3,4) ] so I want something that would return either (1,2) or (3,4) at random for the above list 回答1: You can first use Counter to find the most repeated tuple. Then find the required tuples and finally randomize and get the first value. from collections import Counter import random tups = [ (1,2), (3,4),