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.
#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


Messages In This Thread
RE: Using function argument in lists comprehension. - by snippsat - Apr-23-2019, 09:59 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  mutable argument in function definition akbarza 1 492 Dec-15-2023, 02:00 PM
Last Post: deanhystad
  Using list comprehension with 'yield' in function tester_V 5 1,264 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 1,107 Dec-25-2022, 03:00 PM
Last Post: askfriends
  i want to use type= as a function/method keyword argument Skaperen 9 1,901 Nov-06-2022, 04:28 AM
Last Post: Skaperen
  Use ranking function for different lists klatlap 6 2,017 Feb-15-2022, 11:31 PM
Last Post: klatlap
  Regex - Pass Flags as a function argument? muzikman 6 3,619 Sep-06-2021, 03:43 PM
Last Post: muzikman
  Pop function for lists jamesaarr 8 2,661 Aug-26-2021, 06:40 PM
Last Post: ndc85430
  How to invoke a function with return statement in list comprehension? maiya 4 2,867 Jul-17-2021, 04:30 PM
Last Post: maiya
  How to use a tuple as an argument of a function zarox 5 3,651 Nov-14-2020, 08:02 PM
Last Post: buran
  calling a function and argument in an input phillup7 3 2,628 Oct-25-2020, 02:12 PM
Last Post: jefsummers

Forum Jump:

User Panel Messages

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