1

我正在尝试完成一个项目,该项目将显示 .txt 文件中包含的特定列表中的年总销售额。

该列表的格式如下:

-lastname, firstname (string)
-45.7 (float)
-456.4 (float)
-345.5 (float)
-lastname2, firstname2 (string)
-3354.7 (float)
-54.6 (float)
-56.2 (float)
-lastname3, firstname3 (string)
-76.6 (float)
-34.2 (float)
-48.2 (float)

等等....实际上,7 个不同的“员工”后跟 12 组“数字”(一年中的月份)....但是这个例子应该足以让我了解我想要做什么。

我需要输出每个“员工”的特定信息 - 员工姓名 - 总和(列表中 12 个数字的总和)

所以我的逻辑是让我得出这个结论,但我不知道从哪里开始:

创建 7 个不同的数组来存储每个“员工”数据。使用这种逻辑,我需要将主列表拆分为独立的数组,以便我可以使用它们。

如何做到这一点?而且,如果我没有预定义的员工人数(但定义的格式 ::“姓名”后跟 12 个月的数字)......我怎样才能做到这一点?

我敢肯定,一旦我知道如何将列表“拆分”为不同的部分——每 13 行?

4

3 回答 3

3

Yes, at every thirteenth line you'd have the information of an employee.

However, instead of using twelve different lists, you can use a dictionary of lists, so that you wouldn't have to worry about the number of employees.

And you can either use a parameter on the number of lines directed to each employee.

You could do the following:

infile = open("file.txt", "rt")

employee = dict()
name = infile.readline().strip()
while name:

    employee[name] = list()

    for i in xrange(1, 12):
        val = float(infile.readline().strip())
        employee[name].append(val)

    name = infile.readline().strip()

Some ways to access dictionary entries:

for name, months in employee.items():
    print name
    print months

for name in employee.keys():
    print name
    print employee[name]

for months in employee.values():
    print months

for name, months in (employee.keys(), employee.values()):
    print name
    print months

The entire process goes as follows:

infile = open("file.txt", "rt")

employee = dict()
name = infile.readline().strip()
while name:

    val = 0.0
    for i in xrange(1, 12):
        val += float(infile.readline().strip())
    employee[name] = val

    print ">>> Employee:", name, " -- salary:", str(employee[name])
    name = infile.readline().strip()

Sorry for being round the bush, somehow (:

于 2012-12-01T23:34:38.483 回答
1

这是选项。不好,但仍然是粗鲁的选择。

summed = 0
with open("file.txt", "rt") as f:
    print f.readline() # We print first line (first man) 
    for line in f:
        # then we suppose every line is float.
        try:                             
            # convert to float
            value = float(line.strip())  
            # add to sum
            summed += value              
        # If it does not convert, then it is next person
        except ValueError: 
            # print sum for previous person
            print summed
            # print new name
            print line
            # reset sum
            summed = 0
    # on end of file there is no errors, so we print lst result
    print summed

由于您需要更大的灵活性,因此还有另一种选择:

    data = {} # dict: list of all values for person by person name
    with open("file.txt", "rt") as f:
        data_key = f.readline() # We remember first line (first man)
        data[data_key] = [] # empty list of values
        for line in f:
            # then we suppose every line is float.
            try:                             
                # convert to float
                value = float(line.strip())  
                # add to data
                data[data_key].append(value)
            # If it does not convert, then it is next person
            except ValueError: 
                # next person's name
                data_key = line
                # new list
                data[data_key] = []

问:假设我想为总销售额超过 7000(12 个月)的员工打印“2% 奖金”

for employee, stats in data.iteritems():
    if sum(stats) > 7000:
        print employee + " done 7000 in total sales! need 2% bonus"
于 2012-12-02T00:24:00.703 回答
0

我不会创建 7 个不同的数组。我会创建某种数据结构,以一种数据类型保存一名员工的所有相关信息(这是 python,但您当然也可以在 python 中创建数据结构)。

然后,当您处理每个员工的数据时,您所要做的就是遍历一组员工数据元素。这样,跟踪数据的索引就容易多了(甚至可能不需要!)。

如果您想以某种方式对数据进行排序,这将特别有用。这样,您只需对一个数组而不是 7 个数组进行排序。

于 2012-12-01T23:29:43.613 回答