Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python ';拆分';将列表分成几个数组_Python_Arrays_List - Fatal编程技术网

Python ';拆分';将列表分成几个数组

Python ';拆分';将列表分成几个数组,python,arrays,list,Python,Arrays,List,我正在尝试完成一个项目,该项目将显示.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.

我正在尝试完成一个项目,该项目将显示.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行?

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

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


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

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

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


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

是的,在每13行中,您就可以获得一名员工的信息

但是,您可以使用列表字典,而不是使用12个不同的列表,这样您就不必担心员工的数量

您可以在指向每个员工的行数上使用参数

您可以执行以下操作:

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()
访问字典条目的一些方法:

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
整个过程如下:

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()

不知何故,我很抱歉绕圈子(:

是的,每到第十三行,你就会有一名员工的信息

但是,您可以使用列表字典,而不是使用12个不同的列表,这样您就不必担心员工的数量

您可以在指向每个员工的行数上使用参数

您可以执行以下操作:

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()
访问字典条目的一些方法:

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
整个过程如下:

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()
对不起,不知何故,我有点绕圈子了(:

这里是选项。 不好,但仍然是野蛮的选择

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%奖金”

这是一个选择。 不好,但仍然是野蛮的选择

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%奖金”


(模)操作符呢?你可以用它每13步增加一次“输出”数组索引。只需做一个while循环并解析这13个点。那么(模)操作符呢?你可以用它每13步增加一次“输出”数组索引。只需做一个while循环并解析这13个点?open(“file.txt”,“rt”)!谢谢,我添加了
r
标志,这是输入文件的可选标志,但我没有获得
t
标志,这是干什么用的?这似乎是最好的解决方案!谢谢@Rubens。我仍然无法让它工作,你确定列表中会包含str和数字吗?似乎每次我尝试在销售中运行该程序时都会出错sData.txtYou's welcome!尝试以更通用的方式制作内容,以便您可以更轻松地考虑使用容器。只是不要将内容的大小限制太多(:问候!是的,列表确实存储了数字,您收到的错误是什么?@Rubens“显式优于隐式…”“t”表示文本模式,“b”对于二进制文件,我们以文本模式读取-因此,对于每个将阅读代码的人,我们都这样说(即使这不是必需的)。打开(“file.txt”,“rt”)!谢谢,我添加了
r
标志,这是输入文件的可选标志,但我没有获得
t
标志,这是干什么用的?这似乎是最好的解决方案!谢谢@Rubens。我仍然无法让它工作,你确定列表中会包含str和数字吗?似乎每次我尝试在销售中运行该程序时都会出错sData.txtYou's welcome!尝试以更通用的方式制作内容,以便您可以更轻松地考虑使用容器。只是不要将内容的大小限制太多(:问候!是的,列表确实存储了数字,您收到的错误是什么?@Rubens“显式优于隐式…”“t”表示文本模式,“b”对于二进制。我们以文本模式阅读-所以我们对所有阅读代码的人都这样说(即使这不是必要的)。这太完美了!!从这里我想我可以按照需要使用数据。再次感谢你们!!你们真是帮了大忙here@mmmaceo这不好(而且有点难看)因为有很多抛出的异常。在大数据上,它可能运行得很慢。但对于10-100人来说,我认为这没关系。@mmmaceo补充道