Posts: 230 Threads: 39 Joined: Mar 2020 Hi, i have the following code: import random def newlist(x): y = [] for i in x: if i not in y: y.append(i) return y a = [] for i_nd in range(0, 1000): n = random.randint(0, 100) a.append(n) print(newlist(a)) and for some reason, even though i indicated in for i_nd in range(0, 1000): a range from 0 to 1000 - it only gives me 100 numbers....(when what i want is 1,000 numbers in the range from 0 to 100) do you know what can it be ? Posts: 2,167 Threads: 35 Joined: Sep 2016 Dec-02-2022, 03:03 PM (This post was last modified: Dec-02-2022, 03:04 PM by Yoriz.) It is not possible to have 1000 unique numbers in the range from 0 to 100. Posts: 4,874 Threads: 78 Joined: Jan 2018 It is because the call to newlist() removes duplicates. Posts: 453 Threads: 16 Joined: Jun 2022 Dec-02-2022, 03:04 PM (This post was last modified: Dec-02-2022, 03:05 PM by rob101.) You have a list of 1000 random numbers, before you pass that list to your function. Your function is then striping out all the duplicates, which leaves all the numbers from 0 to 100. If you put in y.sort() before the return, you see what I mean: def newlist(x): y = [] for i in x: if i not in y: y.append(i) y.sort() return y Sig: >>> import this The UNIX philosophy: "Do one thing, and do it well." "The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse "Everything should be made as simple as possible, but not simpler." :~ Albert Einstein Posts: 6,920 Threads: 22 Joined: Feb 2020 You shufle 20 decks of standard playing cards. You remove all the duplicate cards so there are only one of each. How many cards remain? Do you see how this relates to your question? Posts: 230 Threads: 39 Joined: Mar 2020 yea i know (one deck), but the thing is i do want duplicates... okay here i fixed it... import random def newlist(x): y = [] for i in x: y.append(i) y.sort() return y a = [] for i_nd in range(0, 1000): n = random.randint(0, 100) a.append(n) print(len(a)) print(newlist(a)) thank you everyone
heyy where did my last message went ? okay i'll write it again, i have the current code: import random from collections import Counter def newlist(x): y = [] for i in x: y.append(i) y.sort() return y a = [] for i_nd in range(0, 1000): n = random.randint(0, 100) a.append(n) b = a b.sort() my_List = dict(Counter(b)) print(my_List) print(len(a)) print(newlist(a)) Output: {0: 7, 1: 14, 2: 6, 3: 13, 4: 7, 5: 10, 6: 4, 7: 11, 8: 12, 9: 7, 10: 14, 11: 9, 12: 7, 13: 7, 14: 8, 15: 11, 16: 4, 17: 6, 18: 16, 19: 10, 20: 7, 21: 16, 22: 7, 23: 6, 24: 9, 25: 7, 26: 11, 27: 12, 28: 9, 29: 10, 30: 12, 31: 4, 32: 8, 33: 7, 34: 11, 35: 9, 36: 8, 37: 17, 38: 7, 39: 11, 40: 7, 41: 9, 42: 12, 43: 11, 44: 10, 45: 13, 46: 12, 47: 11, 48: 15, 49: 16, 50: 12, 51: 7, 52: 10, 53: 13, 54: 18, 55: 5, 56: 7, 57: 7, 58: 7, 59: 11, 60: 11, 61: 8, 62: 14, 63: 9, 64: 16, 65: 4, 66: 8, 67: 12, 68: 11, 69: 17, 70: 11, 71: 12, 72: 6, 73: 9, 74: 10, 75: 11, 76: 7, 77: 11, 78: 12, 79: 4, 80: 9, 81: 9, 82: 5, 83: 16, 84: 12, 85: 15, 86: 12, 87: 7, 88: 13, 89: 8, 90: 13, 91: 15, 92: 14, 93: 8, 94: 6, 95: 16, 96: 7, 97: 7, 98: 11, 99: 6, 100: 4}
now, suppose i want to order the output so the order of the numbers will be by their prevalence...and not by the numeric order as it is at the moment... what shall be done ? Posts: 6,920 Threads: 22 Joined: Feb 2020 Dec-02-2022, 05:00 PM (This post was last modified: Dec-02-2022, 05:03 PM by deanhystad.) Read the Counter documentation Posts: 453 Threads: 16 Joined: Jun 2022 I'm struggling to understand why you have so much code. What does your code do, that this stripped down version does not do (sorting, aside)? import random from collections import Counter a = [] # holds a list of 1000 numbers; 0 to 100 for i_nd in range(0, 1000): n = random.randint(0, 100) a.append(n) number_dict = dict(Counter(a)) # renamed Sig: >>> import this The UNIX philosophy: "Do one thing, and do it well." "The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse "Everything should be made as simple as possible, but not simpler." :~ Albert Einstein Posts: 230 Threads: 39 Joined: Mar 2020 yea (rob101), you're right, i stripped it down as well, no need for the function above... Dean - i read the documentation on python.org, not much there, anyway - i read/searched some more info on other sites but haven't found something useful... Posts: 2,167 Threads: 35 Joined: Sep 2016 Dec-02-2022, 06:18 PM (This post was last modified: Dec-02-2022, 06:18 PM by Yoriz.) It can be reduced to import random from collections import Counter counter = Counter(random.randint(0, 100) for _ in range(1000)) print(counter.most_common()) Output: [(54, 18), (64, 18), (74, 16), (1, 16), (89, 15), (96, 15), (37, 14), (55, 14), (30, 13), (63, 13), (99, 13), (3, 13), (24, 13), (4, 13), (12, 12), (46, 12), (47, 12), (22, 12), (65, 12), (13, 12), (52, 12), (88, 12), (10, 12), (21, 12), (18, 12), (0, 12), (23, 12), (90, 12), (41, 11), (82, 11), (50, 11), (81, 11), (83, 11), (92, 11), (5, 11), (51, 11), (95, 11), (40, 11), (97, 11), (68, 11), (16, 11), (2, 11), (62, 11), (73, 11), (60, 11), (98, 11), (9, 11), (67, 10), (36, 10), (43, 10), (100, 10), (15, 10), (61, 10), (86, 10), (11, 10), (38, 10), (93, 9), (84, 9), (48, 9), (7, 9), (49, 9), (35, 9), (77, 9), (8, 9), (34, 9), (56, 9), (94, 9), (42, 9), (58, 8), (28, 8), (31, 8), (78, 8), (32, 8), (29, 8), (87, 8), (6, 8), (44, 8), (33, 8), (25, 8), (75, 8), (14, 8), (70, 8), (39, 8), (66, 7), (72, 7), (20, 7), (26, 7), (69, 7), (59, 6), (53, 6), (27, 6), (19, 6), (91, 6), (80, 6), (85, 5), (71, 5), (45, 5), (79, 5), (17, 4), (76, 4), (57, 2)] |