列表的几种特殊用法

一. 去重并保持原来元素的顺序

def dedupe(items):
    h = []
    for item in items:
        if item not in h:
#            yield item
            h.append(item)
    return h

#a = [1, 5, 2, 1, 9, 1, 5, 10]
a = [ {'x':1, 'y':2}, {'x':1, 'y':3}, {'x':1, 'y':2}, {'x':2, 'y':4}]
b = dedupe(a)
print(b)

猜你喜欢

转载自www.cnblogs.com/regit/p/9373083.html
今日推荐