我如何将列表分成较小的列表

时间:2016-04-22 21:44:44

标签: python

我的列表格式如下:

gymnastics_school,participant_name,all-around_points_earned

我需要按学校划分,但保留分数。

import collections

def main():
    names = ["gymnastics_school", "participant_name", "all_around_points_earned"]
    Data = collections.namedtuple("Data", names)    
    data = []

    with open('state_meet.txt','r') as f:   
        for line in f:
            line = line.strip()
            items = line.split(',')
            items[2] = float(items[2])
            data.append(Data(*items))

这些是他们如何设置的示例:

Lanier City Gymnastics,Ben W.,55.301
Lanier City Gymnastics,Alex W.,54.801
Lanier City Gymnastics,Sky T.,51.2
Lanier City Gymnastics,William G.,47.3
Carrollton Boys,Cameron M.,61.6
Carrollton Boys,Zachary W.,58.7
Carrollton Boys,Samuel B.,58.6
La Fayette Boys,Nate S.,63
La Fayette Boys,Kaden C.,62
La Fayette Boys,Cohan S.,59.1
La Fayette Boys,Cooper J.,56.101
La Fayette Boys,Avi F.,53.401
La Fayette Boys,Frederic T.,53.201
Columbus,Noah B.,50.3
Savannah Metro,Levi B.,52.801
Savannah Metro,Taylan T.,52
Savannah Metro,Jacob S.,51.5
SAAB Gymnastics,Dawson B.,58.1
SAAB Gymnastics,Dean S.,57.901
SAAB Gymnastics,William L.,57.101
SAAB Gymnastics,Lex L.,52.501
Suwanee Gymnastics,Colin K.,57.3
Suwanee Gymnastics,Matthew B.,53.201

处理后应该如下:

Lanier City Gymnastics:participants(4)

因为它拥有列表

Carrollton Boys(3)

因为它拥有列表

La Fayette Boys(6)

1 个答案:

答案 0 :(得分:0)

我建议将它们放在dictionaries中:

data = {}

with open('state_meet.txt','r') as f:   
    for line in f:
        line = line.strip()
        items = line.split(',')
        items[2] = float(items[2])
        if items[0] in data:
            data[items[0]].append(items[1:])
        else:
            data[items[0]] = [items[1:]]

然后可以通过以下方式访问学校:

>>> data['Lanier City Gymnastics']
[['Ben W.',55.301],['Alex W.',54.801],['Sky T'.,51.2],['William G.',47.3]

修改: 假设您首先需要将整个数据集作为列表,那么您希望将其划分为较小的列表,您可以从列表中生成字典:

data = []

with open('state_meet.txt','r') as f:   
    for line in f:
        line = line.strip()
        items = line.split(',')
        items[2] = float(items[2])
        data.append(items)

#perform median or other operation on your data

nested_data = {}        
for items in data:
    if items[0] in data:
        data[items[0]].append(items[1:])
    else:
        data[items[0]] = [items[1:]]

    nested_data[item[0]] 

当您需要获取列表的子集时,可以使用切片:

mylist[start:stop:step] 

其中startstopstep是可选的(有关更全面的介绍,请参阅link