tlorin
tlorin

Reputation: 1150

Skip variable number of iterations in Python for loop

I have a list and a for loop such as these:

mylist = ['foo','foo','foo','bar,'bar','hello']
for item in mylist:
    cp = mylist.count(item)
    print("You "+item+" are present in "+str(cp)+" copy(ies)")

Output:

You foo are present in 3 copy(ies)
You foo are present in 3 copy(ies)
You foo are present in 3 copy(ies)
You bar are present in 2 copy(ies)
You bar are present in 2 copy(ies)
You dude are present in 1 copy(ies)

Expected output:

You foo are present in 3 copy(ies)
You bar are present in 2 copy(ies)
You dude are present in 1 copy(ies)

The idea is thus to skip a variable number of iterations within the for loop, using something like this script (not working):

for item in mylist:
    cp = mylist.count(item)
    print("You "+item+" are present in "+str(cp)+" copy(ies)")
    continue(cp)

The script would thus "jump" cp elements in the for loop at every round and start doing again what it is asked at the item item + cp.

I know that you can use continue to skip multiple iterations (such as in this post) but I cannot figure out how to use continue to skip a variable number of iterations.

Thanks for your answer! :)


Edit: similar items are always next to each other.

Upvotes: 2

Views: 401

Answers (7)

Tom Dalton
Tom Dalton

Reputation: 6190

You could use a Counter:

from collections import Counter

mylist = ['foo','foo','foo','bar','bar','hello']
c = Counter(mylist)
for item, cp in c.items():
    print("You "+item+" are present in "+str(cp)+" copy(ies)")

Upvotes: 5

Maximilian Peters
Maximilian Peters

Reputation: 31709

You can get the unique elements of a list using set

mylist = ['foo','foo','foo','bar','bar','hello']
uniq_list = set(my_list)
for item in uniq_list:
    cp = mylist.count(item)
    print("You "+item+" are present in "+str(cp)+" copy(ies)")

Output:

You bar are present in 2 copy(ies)

You hello are present in 1 copy(ies)

You foo are present in 3 copy(ies)

Upvotes: 2

Padraic Cunningham
Padraic Cunningham

Reputation: 180502

Since the elements are consecutive, you could use a groupby which would group the consecutive strings, it is just a matter of summing the length of each group to get the count:

from itertools import groupby
mylist = ['foo','foo','foo','bar','bar','hello']

for k,v in groupby(mylist):
    print("You {} are present in {} copy(ies)".format(k, sum(1 for _ in v)))

Output:

You foo are present in 3 copy(ies)
You bar are present in 2 copy(ies)
You hello are present in 1 copy(ies)

The most efficient way to generally get counts would be to use the dictionary logic like a Counter provided in the other answers, if you want to keep order you could use an OrderedDict to do the counting:

from collections import OrderedDict
mylist = ['foo','foo','foo','bar','bar','hello']
od = OrderedDict()
for ele in mylist:
    od.setdefault(ele, 0)
    od[ele] += 1

for tup in od.items():
    print("You {} are present in {} copy(ies)".format(*tup))

Which would output the same:

You foo are present in 3 copy(ies)
You bar are present in 2 copy(ies)
You hello are present in 1 copy(ies)

Both the groupby and the dict logic are O(n), using your list.count is quadratic.

Upvotes: 3

Rafa
Rafa

Reputation: 3043

set and list counts would do the trick:

mylist = ['foo','foo','foo','bar','bar','hello']
for item in set(mylist):
    print("You "+item+" are present in "+str(mylist.count(item))+" copy(ies)")

output:

You foo are present in 3 copy(ies)
You bar are present in 2 copy(ies)
You hello are present in 1 copy(ies)

Upvotes: 1

sumit
sumit

Reputation: 3780

You can also do it this way

mylist = ['foo','foo','foo','bar','bar','hello']
prev = None
for item in mylist:
    if item != prev:
        cp = mylist.count(item)
        print("You "+item+" are present in "+str(cp)+" copy(ies)")
        prev = item

hope it may help !

Upvotes: 2

Sebastian Hoffmann
Sebastian Hoffmann

Reputation: 2914

mylist = ['foo','foo','foo','bar','bar','hello']
last = None
for item in mylist:
    if item is last:
        continue

    last = item
    cp = mylist.count(item)
    print("You "+item+" are present in "+str(cp)+" copy(ies)")

This assumes that the list is ordered so that identical objects are next to each other.

Upvotes: 1

Aswin Murugesh
Aswin Murugesh

Reputation: 11080

You can use collections.Counter for your job:

>>> from collections import Counter
>>> Counter(['foo', 'foo', 'bar'])
Counter({'foo': 2, 'bar': 1})

Thus,

count_dict = Counter(mylist)
for item in count_dict:
    print("You "+item+" are present in "+str(count_dict[item[)+" copy(ies)")

Upvotes: 3

Related Questions