如何在 python 中求和 运行?
How to do a running sum in python?
我有一个列表 Student
,其结构如下:
[('abc', 50000), ('def', 34000),....]
这里每个元组的第一个元素是员工ID,第二部分是薪水。现在我要做的是首先根据员工人数形成不同的桶。所以桶会有 - 0-5 employees
、0-10 employees
、0-15 employees
等等。例如,如果我在列表中说有 32 名员工数据,那么我的存储桶将是 - 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
库生成数据,因此看起来很奇怪)
编辑
要改为打印范围,只需更改打印语句:
>>> 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
类似于本的回答:
# 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)]
我有一个列表 Student
,其结构如下:
[('abc', 50000), ('def', 34000),....]
这里每个元组的第一个元素是员工ID,第二部分是薪水。现在我要做的是首先根据员工人数形成不同的桶。所以桶会有 - 0-5 employees
、0-10 employees
、0-15 employees
等等。例如,如果我在列表中说有 32 名员工数据,那么我的存储桶将是 - 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
库生成数据,因此看起来很奇怪)
编辑
要改为打印范围,只需更改打印语句:
>>> 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
类似于本的回答:
# 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)]