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
Total number count
Started by Lord Cat, Feb 14 2009 12:05 PM
11 replies to this topic
#1
Posted 14 February 2009 - 12:05 PM
There are 10 kinds of people in this world,
Those that can read binary and those who can't
Those that can read binary and those who can't
|
|
|
#2
Posted 14 February 2009 - 03:28 PM
#3
Posted 15 February 2009 - 01:06 PM
You want something like:
That's Python 3k code by the way.
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
Posted 15 February 2009 - 01:28 PM
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
Those that can read binary and those who can't
#5
Posted 16 February 2009 - 01:36 AM
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". ;)
python program.py
Less likely to error than pasting it in the "live command-line". ;)
#6
Posted 29 May 2009 - 07:48 AM
nice basic tutorial for python using a function sir....
#7
Posted 31 May 2009 - 08:33 AM
PythonPower said:
You want something like:
That's Python 3k code by the way.
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
Posted 31 May 2009 - 10:17 PM
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.
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
Posted 02 June 2009 - 05:02 AM
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 count3 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
Posted 02 June 2009 - 05:20 AM
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
Posted 07 June 2009 - 05:26 AM
str([1, 1, 10, 18, 34, 134, 345, 1001]).count('1')
#12
Posted 11 June 2009 - 03:58 AM
wow, nice void. haha.
i love findin faster ways to do things.
thx :D
i love findin faster ways to do things.
thx :D
Edited by Hot_Milo23, 11 June 2009 - 03:58 AM.
spelling/grammar


Sign In
Create Account

Back to top









