Sarah
Sarah

Reputation: 23

Counting digits in a list of list of strings and returning them as a list of integers

New here. Please take a look at my docstring to see what I'm trying to do:

def count(data):
    """ (list of list of str) -> list of int

    Return the numbers of occurrences of strings that end in digits 0, 1, or 2
    for each string.

    >>> data = [['N', 'OW1'], ['Y', 'EH1', 'S'], ['AW1', 'OW1']]
    >>> count(data)
    [1, 1, 2]
    """

    num_list = []
    num = 0

    for sublist in phonemes:
        for item in sublist:
            if item[-1].isdigit():
                num += 1
        num_list.append(num)
    return num_list 

I don't know how to go about creating a number for each data sublist. Is that even the right approach? Any help would be great.

Upvotes: 1

Views: 84

Answers (5)

Avinash Raj
Avinash Raj

Reputation: 174696

Through list_comprehension.

>>> data = [['N', 'OW1'], ['Y', 'EH1', 'S'], ['AW1', 'OW1']]
>>> [len([j for j in i if j[-1] in "012"]) for i in data]
[1, 1, 2]

Upvotes: 1

TigerhawkT3
TigerhawkT3

Reputation: 49318

>>> data = [['N', 'OW1'], ['Y', 'EH1', 'S'], ['AW1', 'OW1']]
>>> the_lengths = [i for i in map(int, [len([i for i in thelist if i[-1].isdigit()]) for thelist in data])]
>>> the_lengths
[1, 1, 2]

Upvotes: 0

Kozyarchuk
Kozyarchuk

Reputation: 21837

Try this.

def count(data):
    """ (list of list of str) -> list of int

    Return the numbers of occurrences of strings that end in digits 0, 1, or 2
    for each string.

    >>> data = [['N', 'OW1'], ['Y', 'EH1', 'S'], ['AW1', 'OW1']]
    >>> count(data)
    [1, 1, 2]
    """

    return [ len([item for item  in sublist if item[-1].isdigit()]) for sublist in data]

Upvotes: 2

ProfOak
ProfOak

Reputation: 551

I think you have the right idea but you're not resetting num to 0 after you finish a sublist. What you're doing right now is counting a total amount of numbers 0,1,2 in all the sublists. What you want is a count for each sublist.

You also have to append num after you go through the whole sublist. So you need to put that out of the inner for loop body.

Revised:

def count(data):
    """ (list of list of str) -> list of int

    Return the numbers of occurrences of strings that end in digits 0, 1, or 2
    for each string.

    >>> data = [['N', 'OW1'], ['Y', 'EH1', 'S'], ['AW1', 'OW1']]
    >>> count(data)
    [1, 1, 2]
    """

    num_list = []
    num = 0

    for sublist in data:
        for item in sublist:
            if item[-1] in "012":
                num += 1

        # item in sublist body is over
        # num is now total occurrences of 0,1,2 in sublist
        num_list.append(num)
        num = 0
    return num_list 

print count([['N', 'OW1'], ['Y', 'EH1', 'S'], ['AW1', 'OW1']])

Upvotes: 0

pzp
pzp

Reputation: 6597

My solution:

def count(data):
    nums = []
    for sub in data:
        occur = 0
        for i in sub:
            if i.endswith(('0', '1', '2')):
                occur += 1
        nums.append(occur)
    return nums

Upvotes: 0

Related Questions