Python Forum
Using function argument in lists comprehension.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using function argument in lists comprehension.
#1
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?
Reply
#2
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']
Reply
#3
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)
Reply
#4
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.
Reply
#5
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
Reply
#6
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Appending lists into lists from function is not working for me BoredBannana 2 1,201 Oct-25-2024, 05:18 PM
Last Post: deanhystad
  mutable argument in function definition akbarza 1 1,236 Dec-15-2023, 02:00 PM
Last Post: deanhystad
  Using list comprehension with 'yield' in function tester_V 5 3,627 Apr-02-2023, 06:31 PM
Last Post: tester_V
Information How to take url in telegram bot user input and put it as an argument in a function? askfriends 0 2,373 Dec-25-2022, 03:00 PM
Last Post: askfriends
  i want to use type= as a function/method keyword argument Skaperen 9 3,679 Nov-06-2022, 04:28 AM
Last Post: Skaperen
  Use ranking function for different lists klatlap 6 3,227 Feb-15-2022, 11:31 PM
Last Post: klatlap
  Regex - Pass Flags as a function argument? muzikman 6 6,072 Sep-06-2021, 03:43 PM
Last Post: muzikman
  Pop function for lists jamesaarr 8 3,990 Aug-26-2021, 06:40 PM
Last Post: ndc85430
  How to invoke a function with return statement in list comprehension? maiya 4 4,062 Jul-17-2021, 04:30 PM
Last Post: maiya
  How to use a tuple as an argument of a function zarox 5 9,169 Nov-14-2020, 08:02 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020