Python Forum
Function to count words in a list up to and including Sam
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Function to count words in a list up to and including Sam
#14
One alternative approach (assumes that if name not in the list then number of words in list should be returned): if the case is to count consecutive items then there is possibility to do that without setting counter outside for loop. Just using built-in function enumerate will suffice:

def countwhile_inclusive(iterable, predicate):
    for queue_no, item in enumerate(iterable, start=1):
        if item == predicate:
            break
    return queue_no
However, we test all our code, right Wink. By doing so we will find that if iterable is empty we get error (queu_no will be never assigned). So we probably have to deal with it. For that we need to decide what should we return if empty list is passed as argument. There are many possibilities but I point out two of them: raise error or return 0 (in human language it can be described as: 'how many blue marbles do you have?'; 'I don't know what blue marble is (return error); 'I don't have blue marbles (return 0)')

So we can write:

def countwhile_inclusive(iterable, predicate):
    if len(iterable) == 0:        # if list is empty
        return 0                  # or raise ValueError?
    for queue_no, item in enumerate(iterable, start=1):
        if item == predicate:
            break
    return queue_no
According to Python wiki getting list length is pretty cheap O(1)

There is also built-in module itertools which have takewhile which can be used to solve this type of problems as well.

EDIT: this can also be solved using else branch of for loop:

def countwhile_inclusive(iterable, predicate):
    for queue_no, item in enumerate(iterable, start=1):
        if item == predicate:
            return queue_no
    else:                      # no break
        return len(iterable)
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Messages In This Thread
RE: Function to count words in a list up to and including Sam - by perfringo - Feb-18-2021, 10:22 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Row Count and coloumn count Yegor123 4 1,336 Oct-18-2022, 03:52 AM
Last Post: Yegor123
  For Word, Count in List (Counts.Items()) new_coder_231013 6 2,621 Jul-21-2022, 02:51 PM
Last Post: new_coder_231013
  How to get unique entries in a list and the count of occurrence james2009 5 2,995 May-08-2022, 04:34 AM
Last Post: ndc85430
  Including data files in a package ChrisOfBristol 4 2,557 Oct-27-2021, 04:14 PM
Last Post: ChrisOfBristol
  Not including a constructor __init__ in the class definition... bytecrunch 3 11,933 Sep-02-2021, 04:40 AM
Last Post: deanhystad
  count item in list korenron 8 3,491 Aug-18-2021, 06:40 AM
Last Post: naughtyCat
  Generate a string of words for multiple lists of words in txt files in order. AnicraftPlayz 2 2,819 Aug-11-2021, 03:45 PM
Last Post: jamesaarr
  list.count does not appear to function Oldman45 7 3,946 Mar-16-2021, 04:25 PM
Last Post: Oldman45
  how to create pythonic codes including for loop and if statement? aupres 1 1,928 Jan-02-2021, 06:10 AM
Last Post: Gribouillis
  How to use the count function from an Excel file using Python? jpy 2 4,469 Dec-21-2020, 12:30 AM
Last Post: jpy

Forum Jump:

User Panel Messages

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