a

Asked 2 years ago, Updated 2 years ago, 88 views

.

python list

2022-09-22 20:07

2 Answers

import random
from itertools import chain
from collections import Counter

lottoContainer = (random.sample(range(1, 46, 6) for i in range(10)) #LottoNumber
 Create 10 numbers with 6 numbers to avoid duplication
numbers = chain.from_iterable(lottoContainer) #flatten handling for sorting and counting 
result = sorted(sorted(Counter(numbers).most_common(), key=lambda pair:pair[0], reverse=false), key=lambda pair:pair[1], reverse=True) #alignment and count processing
result

Out[4]: # (number, count) 
[(19, 4),
 (20, 4),
 (32, 4),
 (2, 3),
 (12, 3),
 (15, 3),
 (26, 3),
 (31, 3),
 (4, 2),
 (11, 2),
 (13, 2),
 (14, 2),
 (22, 2),
 (27, 2),
 (30, 2),
 (33, 2),
 (42, 2),
 (44, 2),
 (1, 1),
 (3, 1),
 (5, 1),
 (7, 1),
 (9, 1),
 (21, 1),
 (24, 1),
 (28, 1),
 (29, 1),
 (34, 1),
 (37, 1),
 (40, 1),
 (43, 1)]


2022-09-22 20:07

Count strength of elements x in the list (count)

count(x) is a function that returns the number of x by examining how many x are in the list.

Shouldn't we use the count function?


2022-09-22 20:07

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.