Bottom Page

• 0 Vote(s) - 0 Average
• 1
• 2
• 3
• 4
• 5
 Occurence Adem Programmer named Tim Posts: 9 Threads: 6 Joined: Sep 2019 Reputation: 0 Likes received: 0 #1 Nov-07-2019, 10:29 PM Hi, I want to print the occurence of each item . For example, you want 5 items in (1,1). Instead of printing 1,1,1,1,1 ; I want it to print 1:"5". I'm struggling with it for hours. Please could you check what s wrong in my function? Thanks! ```def fonction3(n,a,b): import random L=[] L2=[] for k in range(n): r= random.randint(a,b) x= str(r) L.append(x) for item in L: count=L2.count(n) print(item + ":" + "\""+str(count)+"\"" ) print("There are",(len(L)), "items") return L``` Gribouillis Posts: 2,516 Threads: 30 Joined: Jan 2018 Reputation: 242 Likes received: 583 #2 Nov-07-2019, 10:48 PM (This post was last modified: Nov-07-2019, 11:52 PM by Gribouillis. Edited 1 time in total.) First look at this, and take the time to understand it with the python documentation ```>>> L = [1, 5, 2, 1, 2, 3, 5, 1, 5] >>> sorted(set(L)) [1, 2, 3, 5] ```Now replace lines 10 - 12 with ``` for item in sorted(set(L)): count = L.count(item) print(item + ":" + "\""+str(count)+"\"" ) ```You can also remove L2 from the code. Adem likes this post perfringo Da Bishop Posts: 1,396 Threads: 7 Joined: Jun 2018 Reputation: 142 Likes received: 305 #3 Nov-08-2019, 08:18 AM For counting hashable objects there is built-in collections.Counter. This enables counting and representing results in different ways: ```>>> from collections import Counter >>> nums = [1, 2, 3, 1, 2, 5, 6, 7, 7] >>> counts = Counter(nums) >>> counts Counter({1: 2, 2: 2, 7: 2, 3: 1, 5: 1, 6: 1}) ```Print out counted items with their occurrance (sorted): ```>>> print(*(f'{num}:{count}' for num, count in counts.most_common()), sep='\n') 1:2 2:2 7:2 3:1 5:1 6:1 ```How many values were counted: ```>>> sum(counts.values()) 9 ```How many different objects were counted: ```>>> len(counts) 6 ```All values in sorted order: ```>>> print(*counts.elements()) 1 1 2 2 3 5 6 7 7 >>> list(counts.elements()) [1, 1, 2, 2, 3, 5, 6, 7, 7] ```It doesn't throw an error if asked for object (key) not present, it just 'nicely' says that count is zero: ```>>> counts[1] 2 >>> counts[100] 0 ``` jefsummers and Adem like this post I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame. Adem Programmer named Tim Posts: 9 Threads: 6 Joined: Sep 2019 Reputation: 0 Likes received: 0 #4 Nov-08-2019, 03:17 PM (Nov-08-2019, 08:18 AM)perfringo Wrote: For counting hashable objects there is built-in collections.Counter. This enables counting and representing results in different ways: ```>>> from collections import Counter >>> nums = [1, 2, 3, 1, 2, 5, 6, 7, 7] >>> counts = Counter(nums) >>> counts Counter({1: 2, 2: 2, 7: 2, 3: 1, 5: 1, 6: 1}) ```Print out counted items with their occurrance (sorted): ```>>> print(*(f'{num}:{count}' for num, count in counts.most_common()), sep='\n') 1:2 2:2 7:2 3:1 5:1 6:1 ```How many values were counted: ```>>> sum(counts.values()) 9 ```How many different objects were counted: ```>>> len(counts) 6 ```All values in sorted order: ```>>> print(*counts.elements()) 1 1 2 2 3 5 6 7 7 >>> list(counts.elements()) [1, 1, 2, 2, 3, 5, 6, 7, 7] ```It doesn't throw an error if asked for object (key) not present, it just 'nicely' says that count is zero: ```>>> counts[1] 2 >>> counts[100] 0 ``` « Next Oldest | Next Newest »

Top Page

 Possibly Related Threads... Thread Author Replies Views Last Post Delete minimum occurence in a string RavCOder 10 609 Nov-12-2019, 01:08 PM Last Post: RavCOder

Forum Jump:

Users browsing this thread: 1 Guest(s)