Python Forum
Beginner at Python. Trying to count certain integer from random string of code
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Beginner at Python. Trying to count certain integer from random string of code
#1
I hope my title wasn't too long. I have code to make a random string of numbers; 0 or 1. I want it to count and print how many times 1 appears in the string of numbers. This is what i have so far:
import random
def Rand(Start, end, num):
    res = [ ]

    for j in range (num):
        res.append(random.randint(start, end))

    return res
num = 1000
start = 0
end = 1
pie = (Rand(start, end, num))

def count(lst): 
      
    return sum(lst)

lst = pie
print(count(lst))
I feel that i have no idea if its actually counting whether its 1 or not. I'm kind of figuring there's a better way to do what I'm trying to accomplish. Thanks for helping!
Reply
#2
Python strings have method .count() for counting purpose:

>>> s = '1100101'
>>> s.count('1')
4
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.
Reply
#3
(Oct-14-2019, 05:38 AM)perfringo Wrote: Python strings have method .count() for counting purpose:

>>> s = '1100101'
>>> s.count('1')
4

It is working for list as well

>>> s=['1','0','1','0','1','1','0']
>>> s.count('1')
4
Reply
#4
For counting hashable items there is collections.Counter().

>>> from collections import Counter
>>> s = '1100101'                                                                         
>>> Counter(s)                                                                            
Counter({'1': 4, '0': 3})
>>> Counter(s)['1']
4
Writing your own counter for specific needs is pretty simple. Following is counting even numerics in a string (it will raise ValueError if character is not numeric):

>>> numeric = '23542'
>>> sum(1 for char in numeric if int(char) % 2 == 0)
3
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Sample random, unique string pairs from a list without repetitions walterwhite 1 402 Nov-19-2023, 10:07 PM
Last Post: deanhystad
  Beginner: Code not work when longer list raiviscoding 2 765 May-19-2023, 11:19 AM
Last Post: deanhystad
  Row Count and coloumn count Yegor123 4 1,268 Oct-18-2022, 03:52 AM
Last Post: Yegor123
  Changing a string value to a numerical value using python code and a lamda function Led_Zeppelin 6 1,540 Jul-05-2022, 11:29 PM
Last Post: deanhystad
  I want to simplify this python code into fewer lines, it's about string mandaxyz 5 2,048 Jan-15-2022, 01:28 PM
Last Post: mandaxyz
  Why does 'nothing' count as a string? BashBedlam 3 1,615 Nov-10-2021, 12:41 AM
Last Post: BashBedlam
  generating random string unique forever Skaperen 5 2,287 Aug-20-2021, 07:15 AM
Last Post: Gribouillis
  Question about change hex string to integer sting in the list (python 2.7) lzfneu 1 2,491 May-24-2021, 08:48 AM
Last Post: bowlofred
  Reducing JSON character count in Python for a Twitter Bot johnmitchell85 2 45,942 Apr-28-2021, 06:08 PM
Last Post: johnmitchell85
Photo Creating column in python based on the other colums value count Bartek635 2 2,894 Apr-15-2021, 03:47 PM
Last Post: Bartek635

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020