Jump to content

Total number count

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
11 replies to this topic

#1
Lord Cat

Lord Cat

    Newbie

  • Members
  • Pip
  • 8 posts
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
deer dance

deer dance

    Learning Programmer

  • Members
  • PipPipPip
  • 40 posts
What version are you using?
Ameteur programmer
HTML Expert
Official Lunatic

#3
PythonPower

PythonPower

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 230 posts
You want something like:

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.

#4
Lord Cat

Lord Cat

    Newbie

  • Members
  • Pip
  • 8 posts
Something wrong with it when i put it in...I'm using Python 3.0 too

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

#5
PythonPower

PythonPower

    Programming Professional

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

python program.py

Less likely to error than pasting it in the "live command-line". ;)

#6
kiddies

kiddies

    Programmer

  • Members
  • PipPipPipPip
  • 130 posts
nice basic tutorial for python using a function sir....

#7
Hignar

Hignar

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 420 posts

PythonPower said:

You want something like:


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?

#8
Hignar

Hignar

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 420 posts
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'.


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.

#9
CallBackGuy

CallBackGuy

    Newbie

  • Members
  • PipPip
  • 29 posts

Hignar said:

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'.


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 .-.
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

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 )



#10
Hignar

Hignar

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 420 posts

CallBackGuy said:

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.

#11
v0id

v0id

    Retired

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,936 posts
str([1, 1, 10, 18, 34, 134, 345, 1001]).count('1')


#12
Hot_Milo23

Hot_Milo23

    Programmer

  • Members
  • PipPipPipPip
  • 120 posts
wow, nice void. haha.
i love findin faster ways to do things.
thx :D

Edited by Hot_Milo23, 11 June 2009 - 03:58 AM.
spelling/grammar