Closed Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: Total number count

  1. #1
    Lord Cat's Avatar
    Lord Cat is offline Newbie
    Join Date
    Feb 2009
    Location
    Somewhere but i just forgot
    Posts
    8
    Rep Power
    0

    Total number count

    Hey i'm Looking for a way to be able to put numbers into a list and then get the computer to tell me how many of that number there is

    Like say i had a list ('1,1,10,18,34,134,345,1001')
    And i wanted to know how many 1's there are in total of every number including '1001'
    than it would tell me how many
    Like 1's = 7
    how would that be done?

    In python plz ,I'm a noob to python so i need some help,Ty
    There are 10 kinds of people in this world,
    Those that can read binary and those who can't

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    deer dance's Avatar
    deer dance is offline Learning Programmer
    Join Date
    Feb 2009
    Location
    Indiana
    Posts
    40
    Rep Power
    0

    Re: Total number count

    What version are you using?
    Ameteur programmer
    HTML Expert
    Official Lunatic

  4. #3
    PythonPower's Avatar
    PythonPower is offline Programming Professional
    Join Date
    Feb 2009
    Posts
    228
    Rep Power
    13

    Re: Total number count

    You want something like:

    Code:
    arr = [1, 1, 10, 18, 34, 134, 345, 1001]
    
    def ones(n):
        s = 0
        while n > 0:
            if n%10 == 1: s += 1
            n //= 10
        return s
    
    t = 0
    for x in arr: t += ones(x)
    print(t)
    That's Python 3k code by the way.

  5. #4
    Lord Cat's Avatar
    Lord Cat is offline Newbie
    Join Date
    Feb 2009
    Location
    Somewhere but i just forgot
    Posts
    8
    Rep Power
    0

    Re: Total number count

    Something wrong with it when i put it in...I'm using Python 3.0 too

    Code:
    arr = [1, 1, 10, 18, 34, 134, 345, 1001]
    
    def ones(n):
        s = 0
        while n > 0:
            if n%10 == 1: s += 1
            n //= 10
        return s
    
    t = 0
    for x in arr: t += ones(x)
    print(t)
    SyntaxError: invalid  syntax (<pyshell#26, line 2>)
    There are 10 kinds of people in this world,
    Those that can read binary and those who can't

  6. #5
    PythonPower's Avatar
    PythonPower is offline Programming Professional
    Join Date
    Feb 2009
    Posts
    228
    Rep Power
    13

    Re: Total number count

    It sure works for me. Try creating a file and pasting the code in; then run it like:

    Code:
    python program.py
    Less likely to error than pasting it in the "live command-line".

  7. #6
    kiddies is offline Programmer
    Join Date
    May 2009
    Posts
    129
    Rep Power
    0

    Re: Total number count

    nice basic tutorial for python using a function sir....

  8. #7
    Hignar's Avatar
    Hignar is offline Programming Expert
    Join Date
    May 2009
    Posts
    419
    Blog Entries
    2
    Rep Power
    12

    Re: Total number count

    Quote Originally Posted by PythonPower View Post
    You want something like:

    Code:
    arr = [1, 1, 10, 18, 34, 134, 345, 1001]
    
    def ones(n):
        s = 0
        while n > 0:
            if n%10 == 1: s += 1
            n //= 10
        return s
    
    t = 0
    for x in arr: t += ones(x)
    print(t)
    That's Python 3k code by the way.
    This doesn't return what Lord Cat wants though. Your code only returns numbers that end in 1. If I understood the original post he wants to count all the 1s including the ones in 134 etc.

    Not sure off the top of my head how you could do this. Maybe convert each number to a string and check each digit in the number that way?

  9. #8
    Hignar's Avatar
    Hignar is offline Programming Expert
    Join Date
    May 2009
    Posts
    419
    Blog Entries
    2
    Rep Power
    12

    Re: Total number count

    I've been thinking about this and I couldn't come up with a way of testing the integer directly, so ended up converting each one to a string and testing each character of the string for equality to '1'.

    Code:
    arr = [1, 1, 10, 18, 34, 134, 345, 1001]
    strarr = []
    count = 1
    
    for x in arr:
       strarr.append(str(x)) 
    
    for x in strarr:
       for c in x:
          if c == '1':
             count += 1
    
    print count
    I'm new to python so this probably isn't the easiest or most efficient way of doing this but it gets the right answer.

  10. #9
    CallBackGuy is offline Newbie
    Join Date
    Mar 2009
    Posts
    29
    Rep Power
    0

    Re: Total number count

    Quote Originally Posted by Hignar View Post
    I've been thinking about this and I couldn't come up with a way of testing the integer directly, so ended up converting each one to a string and testing each character of the string for equality to '1'.

    Code:
    count = 1
    You may want to start at zero, otherwise it will output 8 instead of 7
    Here's mine it's very much the same as yours .-.
    Code:
    arr = [1, 1, 10, 18, 34, 134, 345, 1001]
    count = 0
    for strarr in str(arr):
        for x in strarr:
            if x == '1':
                count+=1
    print count
    3 lines anyone? xD
    Code:
    arr, count = [1, 1, 10, 18, 34, 134, 345, 1001], []
    [ ( lambda x: [ count.append(x) if x == '1' else '' for x in strarr ] )(strarr) for strarr in str( arr ) ]
    print len( count )

  11. #10
    Hignar's Avatar
    Hignar is offline Programming Expert
    Join Date
    May 2009
    Posts
    419
    Blog Entries
    2
    Rep Power
    12

    Re: Total number count

    Quote Originally Posted by CallBackGuy View Post
    You may want to start at zero, otherwise it will output 8 instead of 7
    Oops. Too many 1s floating about I guess.

    Didn't realise you could convert the arr to strarr like that.

    Ah, turns out you can't. What you've done is turn the array into one string ('[1, 1, 10, 18, 34, 134, 345, 1001]') and tested each character. It produces the same result, but I prefer my method as it leaves you with the ability to manipulate each string in the array if you want to. Admittedly, not a concern here though.

Closed Thread
Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 1
    Last Post: 06-03-2011, 02:58 PM
  2. Replies: 5
    Last Post: 10-01-2010, 06:35 AM
  3. C Program that counts total prime number
    By exe101 in forum C and C++
    Replies: 3
    Last Post: 04-07-2009, 08:19 AM
  4. Bash: Getting Number/Count of Files in Directory
    By Tor in forum Bash / Shell Scripting
    Replies: 0
    Last Post: 02-12-2009, 10:59 AM
  5. How to Count number of button has been Click?
    By roger in forum Visual Basic Programming
    Replies: 4
    Last Post: 06-01-2006, 12:49 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts