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
You want something like:
That's Python 3k code by the way.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)
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
It sure works for me. Try creating a file and pasting the code in; then run it like:
Less likely to error than pasting it in the "live command-line".Code:python program.py![]()
nice basic tutorial for python using a function sir....
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?
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'.
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.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
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 .-.
3 lines anyone? xDCode: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
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 )
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.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks