为什么我在此列表推导语句中收到无效语法

时间:2016-03-22 23:06:49

标签: python-3.x list-comprehension

所以这些是我正在使用的两个数据集。我想按部门编号获取元素并返回平均工资。我理解如何编写列表理解语句,但我似乎无法理解为什么我在第二个for循环上接收错误,它在print语句之前声明了无效语法。

数据表

s_emp = (('ID', 'LAST_NAME', 'FIRST_NAME', 'USERID', 'START_DATE',     'COMMENTS', 'TITLE',
          'SALARY', 'COMMISSION', 'DEPT_ID', 'MANAGER_ID'),
         (1, 'Martin', 'Carmen', 'martincu', '3-Mar-90', '', 'President',  4500, '', 50, ''),
         (10, 'Jackson', 'Marta', 'jacksomt', '27-Feb-91', '', 'Warehouse      Manager', 1507, '', 45, 2),
         (11, 'Henderson', 'Colin', 'hendercs', '14-May-90', '', 'Sales     Representative', 1400, 10, 31, 3),
         (12, 'Gilson', 'Sam', 'gilsonsj', '18-Jan-92', '', 'Sales Representative', 1490, 12.5, 32, 3),
         (13, 'Sanders', 'Jason', 'sanderjk', '18-Feb-91', '', 'Sales Representative', 1515, 10, 33, 3),
         (14, 'Dameron', 'Andre', 'dameroap', '9-Oct-91', '', 'Sales Representative', 1450, 17.5, 35, 3),
         (15, 'Hardwick', 'Elaine', 'hardwiem', '7-Feb-92', '', 'Stock Clerk', 1400, '', 41, 6),
         (16, 'Brown', 'George', 'browngw', '8-Mar-90', '', 'Store Clerk', 940, '', 41, 6),
         (17, 'Washington', 'Thomas', 'washintl', '9-Feb-91', '', 'Store Clerk', 1200, '', 42, 7),
         (18, 'Patterson', 'Donald', 'patterdv', '6-Aug-91', '', 'Store Clerk', 795, '', 42, 7),
         (19, 'Bell', 'Alexander', 'bellag', '26-May-91', '', 'Store Clerk', 850, '', 43, 8),
         (2, 'Smith', 'Doris', 'smithdj', '8-Mar-90', '', 'VP, Operations', 2450, '', 41, 1),
         (20, 'Gantos', 'Eddie', 'gantosej', '30-Nov-90', '', 'Store Clerk', 800, '', 44, 9),
         (21, 'Stephenoson', 'Blaine', 'stephebs', '17-Mar-91', '', 'Store Clerk', 860, '', 45, 10),
         (22, 'Chester', 'Eddie', 'chesteek', '30-Nov-90', '', 'Store Clerk', 800, '', 44, 9),
         (23, 'Pearl', 'Roger', 'pearlrg', '17-Oct-90', '', 'Store Clerk', 795, '', 34, 9),
         (24, 'Dancer', 'Bonnie', 'dancerbw', '17-Mar-91', '', 'Store Clerk', 860, '', 45, 7),
         (25, 'Schmitt', 'Sandra', 'schmitss', '9-May-91', '', 'Store Clerk', 1100, '', 45, 8),
         (3, 'Norton', 'Michael', 'nortonma', '17-Jun-91', '', 'VP, Sales', 2400, '', 31, 1),
         (4, 'Quenin', 'Mark', 'quentiml', '7-Apr-90', '', 'VP, Finance', 2450, '', 10, 1),
         (5, 'Roper', 'Joseph', 'roperjm', '4-Mar-90', '', 'VP, Administration', 2550, '', 50, 1),
         (6, 'Brown', 'Molly', 'brownmr', '18-Jan-91', '', 'Warehouse Manager', 1600, '', 41, 2),
         (7, 'Hawkins', 'Roberta', 'hawkinrt', '14-May-90', '', 'Warehouse Manager', 1650, '', 42, 2),
         (8, 'Burns', 'Ben', 'burnsba', '7-Apr-90', '', 'Warehouse Manager', 1500, '', 43, 2),
         (9, 'Catskill', 'Antoinette', 'catskiaw', '9-Feb-92', '', 'Warehouse Manager', 1700, '', 44, 2))

s_dept = (('ID', 'Name', 'REGION_ID'),
          (10, 'Finance', 1),
          (31, 'Sales', 1),
          (32, 'Sales', 2),
          (33, 'Sales', 3),
          (34, 'Sales', 4),
          (35, 'Sales', 5),
          (41, 'Operations', 1),
          (42, 'Operations', 2),
          (43, 'Operations', 3),
          (44, 'Operations', 4),
          (45, 'Operations', 5),
          (50, 'Administration', 1))

我的列表理解陈述

print ("Select dept_id, avg(salary) from s_emp by dept_id and order by  department ID: "
for department in {i[9] for  i in s_emp[1::]} :print(department, (lambda 1: round(sum(1) / len(1), 2))(map(float, [ e[7] for e in s_emp[1::] if e[9] == department ])))

1 个答案:

答案 0 :(得分:0)

打印语句之前:上的语法无效"是因为你在第一个print语句的末尾离开了右括号。它应该是:

print ("Select dept_id, avg(salary) from s_emp by dept_id and order by department ID: ")

但是,您会发现lambda表达式中存在语法错误:lambda 1: - 您传递的参数不能是文字,在这种情况下您可以使用传递一个整数。此外,sum(1)len(1)会抛出异常,但我不确定您在此尝试做什么...

替代方法

假设您想按部门名称(而不是部门编号)对元素进行分组并返回平均工资,那么当您在6个月内回复时,以下内容可能更具可读性:

from itertools import groupby

# Dict of {'dept_id': 'Department name'}. Used as a lookup table.
dept_lookup = {row[0]: row[1] for row in s_dept if row[1] != 'Name'}

# List of (salary, Department name) pairs
salaries = [(row[7], dept_lookup[row[9]])
            for row in s_emp
            if row[7] != 'SALARY']

# Sort by "Department name" for groupby function
salaries.sort(key=lambda row: row[1])

for dept_name, group in groupby(salaries, lambda row: row[1]):  # row[1] => Dept name
    total_salaries, n = 0, 0
    for row in group:
        total_salaries += row[0]  # row[0] => Salary
        n += 1
    print("{}: ${:.2f}".format(dept_name, total_salaries / n))

结果:

Administration: $3525.00
Finance: $2450.00
Operations: $1250.75
Sales: $1508.33