-1

我有一个列表字典:

d = {'a': ['Adam', 'Book', 4], 'b': ['Bill', 'TV', 6, 'Jill', 'Sports', 1, 'Bill', 'Computer', 5], 'c': ['Bill', 'Sports', 3], 'd': ['Quin', 'Computer', 3, 'Adam', 'TV', 3], 'e': ['Quin', 'TV', 2, 'Quin', 'Book', 5], 'f': ['Adam', 'Computer', 7]}

每个列表代表该人使用给定对象的次数。例如,对于列表“a”,它显示每周Adam读取一次。Book 4在“b”中,每周Bill观看一次,“ ”每周播放一次。TV 6Jillsports

我想在每个列表中找到一个人做某事的总次数。

在这种情况下,输出将是这样的:

a: Adam 4
b: Bill 11, Jill 1
c: Bill 3
d: Quin 3, Adam 3
e: Quin 7
f: Adam 7

不必是那种确切的格式,但类似的东西。

目前,我已经尝试使用 Counter 但它只计算人名,而不是与之关联的数字。我尝试只返回名称,然后返回数字,但这最终不适用于名称超过 1 个的列表,因为有些列表甚至有 6 个总名称。

任何帮助表示赞赏!谢谢!

4

3 回答 3

3

If you really want to do this with your data structure, you could do something like:

from collections import defaultdict

for k, lst in d.iteritems():
    counts = defaultdict(int)
    for i in range(0, len(lst), 3):
        counts[lst[i]] += lst[i + 2]
    print k, ", ".join(["%s %d" % (n, c) for n, c in counts.items()])

However, a better idea would be to store the data in a sensible data structure. Storing information in lists with rules like "Each list comes in groups of threes, where the first of each three is the name etc" makes working with the data clumsy and counterintuitive. Instead, what if you stored the data as:

d = {'a': {'Adam': {'Book': 4}},
     'b': {'Bill': {'TV': 6, 'Computer': 5}, 'Jill': {'Sports': 1}},
     'c': {'Bill': {'Sports': 3}},
     'd': {'Quin': {'Computer': 3}, 'Adam': {'TV': 3}},
     'e': {'Quin': {'TV': 2, 'Book': 5}},
     'f': {'Adam': {'Computer': 7}}}

Then, you could answer the question like this:

for k, v in d.items():
    print k, ", ".join(["%s %d" % (n, sum(a.values())) for n, a in v.items()])

This would have many other benefits as well: You could find out how many times Adam read a book in "a" just by doing d["a"]["Adam"]["Book"] (or, if you're not sure that "Adam" or "Book" are there, d["a"].get("Adam", {}).get("Book", 0)). Other calculations based on this list would also be possible and useful.

于 2012-08-21T16:12:02.320 回答
2

I think that a defaultdict and list slicing would be useful here ...

a = ['Adam', 'TV', 4, 'Adam', 'Bike', 4 ]
print a[::3] # ['Adam', 'Adam' ]
print a[2::3] # [4, 4]

from collections import defaultdict
for key,value in d.items():
    c = defaultdict(int)
    for k,v in zip(value[::3],value[2::3]):
        c[k] += v
    print key, c

A Counter would work too, but it's not introduced until python2.7 whereas defaultdict is python2.5 compatible and provides the same functionality in this instance.

Although as others have stated, this seems to be a very strange data structure that you're using ...

于 2012-08-21T16:10:41.290 回答
0

This produces the desired result:

import collections
for k, v in sorted(d.iteritems()):
        tmp=collections.defaultdict(int)
        for i in xrange(0, len(v), 3):
                tmp[v[i]]+=v[i+2]
        print "{}: {}".format(k,
                ", ".join("%s %s" % (a, b)
                        for a, b in sorted(tmp.iteritems())))

Output:

a: Adam 4
b: Bill 11, Jill 1
c: Bill 3
d: Adam 3, Quin 3
e: Quin 7
f: Adam 7
于 2012-08-21T16:13:04.150 回答