Python Forum

Full Version: Using function argument in lists comprehension.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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?
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 Huh
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']
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)
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.
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
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.