Posts: 31
Threads: 12
Joined: Apr 2018
Apr-23-2019, 07:29 PM
(This post was last modified: Apr-23-2019, 07:38 PM by Yoriz.)
def nb_dig(n,d):
r = [n**2 for n in range(1,n)]
digz = [d for d in r]
print(r,digz)
nb_dig(10,1) Output: >>>[1, 4, 9, 16, 25, 36, 49, 64, 81] [1, 4, 9, 16, 25, 36, 49, 64, 81]
So, I want 'd' to be a string value, telling me how many times certain number occurs in the results, but instead of it I get some bs. Why the damn 'd' in digz is not imported from the function parameters?
Posts: 2,168
Threads: 35
Joined: Sep 2016
Apr-23-2019, 07:47 PM
(This post was last modified: Apr-23-2019, 07:52 PM by Yoriz.)
d is not a string value, you pass in the number 1 as d
digz = [d for d in r] is not counting how many times d is in r, its making a new copy of r, d is added to a list for each d that is assigned from each item in r(the d that was passed in is overwriten).
to count how many times d is in r use count and if you want it to be a string convert it using str
def nb_dig(n,d):
r = [n**2 for n in range(1,n)]
# digz = [d for d in r]
digz = str(r.count(d))
print(r,digz)
nb_dig(10,1) Output: [1, 4, 9, 16, 25, 36, 49, 64, 81] 1
Unless you want this, im not really sure what you want
def nb_dig(n,d):
r = [n**2 for n in range(1,n)]
digz = [str(d) for _ in r if _ == d]
# digz = str(r.count(d))
print(r,digz)
nb_dig(10,1) Output: [1, 4, 9, 16, 25, 36, 49, 64, 81] ['1']
Posts: 12,034
Threads: 486
Joined: Sep 2016
It works as written:
>>> def nb_dig(n,d):
... print(f'n: {n}, d: {d}')
... r = [n**2 for n in range(1,n)]
... print(f'r: {r}')
... digz = [d for d in r]
... print(f'digz: {digz}')
...
>>> nb_dig(10,1)
n: 10, d: 1
r: [1, 4, 9, 16, 25, 36, 49, 64, 81]
digz: [1, 4, 9, 16, 25, 36, 49, 64, 81]
>>> this statement: digz = [d for d in r] overwrites original d (one passed in)
Posts: 31
Threads: 12
Joined: Apr 2018
It should count all the paremeter given digits in the string and Im still doing something wrong somehow.
def nb_dig(n,d):
r = [n**2 for n in range(1,n)]
digz = [r.count(d)]
print(r,digz)
nb_dig(10,1)
>>>[1, 4, 9, 16, 25, 36, 49, 64, 81] [1] And the result(1) is wrong cause the '1' occurs 3 times in that particular string.
Posts: 2,168
Threads: 35
Joined: Sep 2016
Apr-23-2019, 09:23 PM
(This post was last modified: Apr-23-2019, 09:32 PM by Yoriz.)
I don't understand, there is no string, there is a list of numbers of which the number 1 only appears once.
Edit: The closest i can get to guessing what your after is the following
def nb_dig(n,d):
r = [n**2 for n in range(1,n)]
count = 0
for item in r:
if str(d) in str(item):
count +=1
print(r,count)
nb_dig(10,1) Output: [1, 4, 9, 16, 25, 36, 49, 64, 81] 3
Posts: 7,320
Threads: 123
Joined: Sep 2016
Apr-23-2019, 09:59 PM
(This post was last modified: Apr-23-2019, 09:59 PM by snippsat.)
Can also make it a string,and then count.
As string count() method counts occurrences of substring,and list count() method counts whole values in list.
>>> lst = [1, 4, 9, 16, 25, 36, 49, 64, 81]
>>> s = ''.join(str(i) for i in lst)
>>> s
'149162536496481'
>>> s.count('1')
3
>>> help(s.count)
Help on method_descriptor:
count(...)
S.count(sub[, start[, end]]) -> int
Return the number of non-overlapping occurrences of substring sub in
string S[start:end]. Optional arguments start and end are
interpreted as in slice notation.
|