开发者

How to get multiple values from a for loop?

I have a list of records from a column, the list is named as dates. I am trying to get different dates out of the list. The list have many repetitive dates, such as 1/1/2010,1/1/2010, …. but there are different dates too. But if i use:

for date in dates: ....

it's repeating the loop for every single date(no matter if it is the same or not), not different dates. How could I tell it to do:

for differentdate in dates:... 

The开发者_C百科 language is Python!!


for date in set(dates):

set() makes a collection out of the unique elements in another collection. Note: this may not preserve the order of the original list, so if you require that order to be preserved, go with @GregHewgill's answer.


You can use the itertools module to group by the dates. For example:

>>> import itertools
>>> a = ["aaa", "bbb", "bbb", "ccc"]
>>> for k, g in itertools.groupby(a):
...   print(k)
... 
aaa
bbb
ccc

This preserves the original order of the elements in a (which could be important for you). Inside the loop, g is a generator that produces a sequence containing each element with that key. See the documentation for itertools.groupby for more information.


Either of the following:

def uniqueItems(seq, key=None, reverse=False):
    "Returns a list of unique items in (customizable) order"
    seq = list(set(seq))
    seq.sort(key=key, reverse=reverse)

def uniqueItems(seq):
    "Generator - return unique items in original order of first occurrence"
    seen = set()
    for item in seq:
        if item not in seq:
            yield item
            seen.add(item)

can be used as

for date in uniqueItems(dates):
    # do something with date
    pass


If preserving order was important, the following generator function derived from a comment by Alex Martelli about the Remove duplicates from a sequence ActiveState recipe would work (and should also be relatively fast based to these bench-marks which included the original dictionary-based, non-generator Martelli exemplar):

dates = ["1/1/2010", "1/3/2010", "1/3/2010", "1/7/2010"]

def unique(seq, idfun=lambda x: x):
    seen = set()
    for item in seq:
        marker = idfun(item)
        if marker not in seen:
            seen.add(marker)
            yield item

for date in unique(dates):
    print date

# 1/1/2010
# 1/3/2010
# 1/7/2010

Another nice feature is that it's fairly flexible and can be adapted to other data structures by providing a custom idfun to use to retrieve the datum to be compared.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜