Python - 超越dicts

时间:2016-10-14 14:18:44

标签: python dictionary python-3.5

我使用的是Python 3.5,我有以下dics数组:

d1 = [{id=1, col="name", oldvalue="foo", newvalue="bar"}, {id=1, col="age", oldvalue="25", newvalue="26"}, {id=2, col="name", oldvalue="foo", newvalue="foobar"}, {id=3, col="age", oldvalue="25", newvalue="26"}]

d2 = [{id=1, col="name", oldvalue="foo", newvalue="bar"}, {id=1, col="age", oldvalue="25"]

d3 = [{id=3, col="age", oldvalue="25", newvalue="26"}]

正如您所看到的那样,它显示了对特定“行”的一些更改。

我想知道是否有办法返回更新的行总数,所以:

def counting_updates(d):
    # ...
    return total

print counting_updates(d1) # prints 3 because 3 rows were updated
print counting_updates(d2) # prints 1 because 1 row was updated
print counting_updates(d3) # prints 1 because 1 row was updated

1 个答案:

答案 0 :(得分:1)

如果您要查找唯一id的数量,那么

len({row['id'] for row in d})

{}是一种理解。