如何用python做一个运行和



我有一个列表Student,它具有以下结构:

[('abc', 50000), ('def', 34000),....]

这里每个元组的第一个元素是员工ID,第二部分是工资。现在我要做的是首先根据员工的数量形成不同的桶。所以桶里有- 0-5 employees, 0-10 employees, 0-15 employees等等。例如,如果我的列表中有32名员工数据,那么我的bucket将是- 0-5 employees, 0-10 employees, 0-15 employees, 0-20 employees, 0-25 employees, 0-30 employees,最后是0-32 employees。每一桶都是他们的工资总和。请注意,员工的数量可能会有所不同,而且他们不需要是5名员工的完美组合。但我希望他们在桶中5个员工的差异,直到最后的桶可能有小于5的差异。

我已经试过了:

count = 0
increment = 5
total_employees = 5
run_salary = 0
emp_bucket = []
for items in List1:
    count += 1
    if count <= total_employees:
        run_salary += items[1]
    else:
        emp_bucket.append(run_salary)
        total_employees += increment
        count = 0
        run_salary = 0

我知道这段代码是不正确的,因为当事情被重新初始化时,过程应该从第一个员工开始,而不是列表中的下一个员工。我当前的代码是从下一个员工开始的。我很难用累积的或运行的信息来构建这种类型的桶。我怎样才能形成这些桶呢?

试试这个:

>>> data = [('KgqZe', 4675), ('bFbad', 1279), ('oswIx', 2644), ('mEPlC', 2912), ('rnQGs', 3051), ('BTYHr', 3367), ('AgEqM', 2804), ('ovgNh', 4548), ('AlTAn', 4817), ('vOYtV', 3291), ('vbTxW', 4740), ('rzcRq', 3259), ('ZAJpv', 3800), ('IVGDY', 1499), ('fvCDx', 4432), ('btuUD', 3844), ('fWJUi', 3973), ('nptHC', 4854), ('dbAxH', 1467), ('egeDs', 4514), ('ArvtJ', 4798), ('PGtEh', 1924), ('VkrIb', 1637), ('dbIpm', 1612), ('HShOu', 2425), ('cWZOG', 4286), ('cMESU', 3374), ('fcBpX', 3926), ('VWhFW', 4546), ('FLLmu', 2609), ('XrLEf', 3829), ('xaWZh', 1543)]
>>> 
>>> for group in [data[:i+5] for i in range(0, len(data), 5)]:
...     print group
...     print sum(x[1] for x in group)
...
[('KgqZe', 4675), ('bFbad', 1279), ('oswIx', 2644), ('mEPlC', 2912), ('rnQGs', 3051)]
14561
[('KgqZe', 4675), ('bFbad', 1279), ('oswIx', 2644), ('mEPlC', 2912), ('rnQGs', 3051), ('BTYHr', 3367), ('AgEqM', 2804), ('ovgNh', 4548), ('AlTAn', 4817), ('vOYtV', 3291)]
33388
[('KgqZe', 4675), ('bFbad', 1279), ('oswIx', 2644), ('mEPlC', 2912), ('rnQGs', 3051), ('BTYHr', 3367), ('AgEqM', 2804), ('ovgNh', 4548), ('AlTAn', 4817), ('vOYtV', 3291), ('vbTxW', 4740), ('rzcRq', 3259), ('ZAJpv', 3800), ('IVGDY', 1499), ('fvCDx', 4432)]
51118
[('KgqZe', 4675), ('bFbad', 1279), ('oswIx', 2644), ('mEPlC', 2912), ('rnQGs', 3051), ('BTYHr', 3367), ('AgEqM', 2804), ('ovgNh', 4548), ('AlTAn', 4817), ('vOYtV', 3291), ('vbTxW', 4740), ('rzcRq', 3259), ('ZAJpv', 3800), ('IVGDY', 1499), ('fvCDx', 4432), ('btuUD', 3844), ('fWJUi', 3973), ('nptHC', 4854), ('dbAxH', 1467), ('egeDs', 4514)]
69770
[('KgqZe', 4675), ('bFbad', 1279), ('oswIx', 2644), ('mEPlC', 2912), ('rnQGs', 3051), ('BTYHr', 3367), ('AgEqM', 2804), ('ovgNh', 4548), ('AlTAn', 4817), ('vOYtV', 3291), ('vbTxW', 4740), ('rzcRq', 3259), ('ZAJpv', 3800), ('IVGDY', 1499), ('fvCDx', 4432), ('btuUD', 3844), ('fWJUi', 3973), ('nptHC', 4854), ('dbAxH', 1467), ('egeDs', 4514), ('ArvtJ', 4798), ('PGtEh', 1924), ('VkrIb', 1637), ('dbIpm', 1612), ('HShOu', 2425)]
82166
[('KgqZe', 4675), ('bFbad', 1279), ('oswIx', 2644), ('mEPlC', 2912), ('rnQGs', 3051), ('BTYHr', 3367), ('AgEqM', 2804), ('ovgNh', 4548), ('AlTAn', 4817), ('vOYtV', 3291), ('vbTxW', 4740), ('rzcRq', 3259), ('ZAJpv', 3800), ('IVGDY', 1499), ('fvCDx', 4432), ('btuUD', 3844), ('fWJUi', 3973), ('nptHC', 4854), ('dbAxH', 1467), ('egeDs', 4514), ('ArvtJ', 4798), ('PGtEh', 1924), ('VkrIb', 1637), ('dbIpm', 1612), ('HShOu', 2425), ('cWZOG', 4286), ('cMESU', 3374), ('fcBpX', 3926), ('VWhFW', 4546), ('FLLmu', 2609)]
100907
[('KgqZe', 4675), ('bFbad', 1279), ('oswIx', 2644), ('mEPlC', 2912), ('rnQGs', 3051), ('BTYHr', 3367), ('AgEqM', 2804), ('ovgNh', 4548), ('AlTAn', 4817), ('vOYtV', 3291), ('vbTxW', 4740), ('rzcRq', 3259), ('ZAJpv', 3800), ('IVGDY', 1499), ('fvCDx', 4432), ('btuUD', 3844), ('fWJUi', 3973), ('nptHC', 4854), ('dbAxH', 1467), ('egeDs', 4514), ('ArvtJ', 4798), ('PGtEh', 1924), ('VkrIb', 1637), ('dbIpm', 1612), ('HShOu', 2425), ('cWZOG', 4286), ('cMESU', 3374), ('fcBpX', 3926), ('VWhFW', 4546), ('FLLmu', 2609), ('XrLEf', 3829), ('xaWZh', 1543)]
106279

这将数据分组为递增的5块,并打印组加上所有人的工资之和。

(注意:我使用random库来生成数据,因此它看起来很奇怪)

编辑

要打印范围,只需更改print语句:

>>> for group in [data[:i+5] for i in range(0, len(data), 5)]:
...    print 'Group from 0 to', len(group)
...    print 'Sum:', sum(x[1] for x in group)
...
Group from 0 to 5
Sum: 14561
Group from 0 to 10
Sum: 33388
Group from 0 to 15
Sum: 51118
Group from 0 to 20
Sum: 69770
Group from 0 to 25
Sum: 82166
Group from 0 to 30
Sum: 100907
Group from 0 to 32
Sum: 106279

与Ben的回答相似:

 # function to sum a list of (string, int) tuples
 fsum = lambda x: sum(i[1] for i in x)
 buckets = [fsum(salaries[:i]) for i in range(5, len(salaries), 5)]

相关内容

  • 没有找到相关文章

最新更新