生成元组列表并使用它

时间:2014-03-18 01:32:30

标签: python list input tuples output

我想打开一个文件,取出文件中的所有行,然后将每一行转换为列表中的元组,然后我将用它来生成输出。

我目前的代码如下:

def file(name):
    fn = open(name)
    for line in fn:
        line_list = line.strip().split(";")
        main_list.append(line_list)
        return 

当我打印列表时,它只显示其中的一行文件,文件如下所示:

De Aza, Alejandro; CWS; 153; 607; 84; 160; 27; 4; 17
Hunter, Torii; DET; 144; 606; 90; 184; 37; 5; 17
Hamilton, Josh; LAA; 151; 576; 73; 144; 32; 5; 21
Choo, Shin-Soo; CIN; 154; 569; 107; 162; 34; 2; 21
Upton, Justin; ATL; 149; 558; 94; 147; 27; 2; 27
Cabrera, Miguel; DET; 148; 555; 103; 193; 26; 1; 44
Posey, Buster; SF; 148; 520; 61; 153; 34; 1; 15
Suzuki, Ichiro; NYY; 150; 520; 57; 136; 15; 3; 7
Holliday, Matt; STL; 141; 520; 103; 156; 31; 1; 22
Headley, Chase; SD; 141; 520; 59; 130; 35; 2; 13
Cabrera, Asdrubal; CLE; 136; 508; 66; 123; 35; 2; 14
Pierzynski, A.J.; TEX; 134; 503; 48; 137; 24; 1; 17
Hoes, L.J.; HOU; 46; 167; 24; 48; 7; 2; 1
Young Jr., Eric; COL; 57; 165; 22; 40; 9; 3; 1
Hairston, Scott; CHC; 52; 99; 13; 17; 2; 0; 8
Arnaud, Travis; NYM; 31; 99; 4; 20; 3; 0; 1
Ankiel, Rick; NYM; 20; 66; 7; 12; 4; 1; 2
Ankiel, Rick; HOU; 25; 62; 6; 12; 3; 0; 5
den Dekker, Matt; NYM; 27; 58; 7; 12; 1; 0; 1
Sanchez, Angel; CWS; 1; 2; 0; 0; 0; 0; 0

它只输出第一行:

[['De Aza, Alejandro', ' CWS', ' 153', ' 607', ' 84', ' 160', ' 27', ' 4', ' 17']]

为什么会这样?

2 个答案:

答案 0 :(得分:1)

因为您在处理完第一行后立即返回。取消return语句,就像这样

for line in fn:
    line_list = line.strip().split(";")
    main_list.append(line_list)
return main_list

这可以通过列表理解更好地解决,比如

def read_file(name):
    with open(name) as in_fil:
        return [[item.strip() for item in l.rstrip().split(";")] for l in in_fil]

使用该功能,它会产生

[['De Aza, Alejandro', 'CWS', '153', '607', '84', '160', '27', '4', '17'],
 ['Hunter, Torii', 'DET', '144', '606', '90', '184', '37', '5', '17'],
 ['Hamilton, Josh', 'LAA', '151', '576', '73', '144', '32', '5', '21'],
 ['Choo, Shin-Soo', 'CIN', '154', '569', '107', '162', '34', '2', '21'],
 ['Upton, Justin', 'ATL', '149', '558', '94', '147', '27', '2', '27'],
 ['Cabrera, Miguel', 'DET', '148', '555', '103', '193', '26', '1', '44'],
 ['Posey, Buster', 'SF', '148', '520', '61', '153', '34', '1', '15'],
 ['Suzuki, Ichiro', 'NYY', '150', '520', '57', '136', '15', '3', '7'],
 ['Holliday, Matt', 'STL', '141', '520', '103', '156', '31', '1', '22'],
 ['Headley, Chase', 'SD', '141', '520', '59', '130', '35', '2', '13'],
 ['Cabrera, Asdrubal', 'CLE', '136', '508', '66', '123', '35', '2', '14'],
 ['Pierzynski, A.J.', 'TEX', '134', '503', '48', '137', '24', '1', '17'],
 ['Hoes, L.J.', 'HOU', '46', '167', '24', '48', '7', '2', '1'],
 ['Young Jr., Eric', 'COL', '57', '165', '22', '40', '9', '3', '1'],
 ['Hairston, Scott', 'CHC', '52', '99', '13', '17', '2', '0', '8'],
 ['Arnaud, Travis', 'NYM', '31', '99', '4', '20', '3', '0', '1'],
 ['Ankiel, Rick', 'NYM', '20', '66', '7', '12', '4', '1', '2'],
 ['Ankiel, Rick', 'HOU', '25', '62', '6', '12', '3', '0', '5'],
 ['den Dekker, Matt', 'NYM', '27', '58', '7', '12', '1', '0', '1'],
 ['Sanchez, Angel', 'CWS', '1', '2', '0', '0', '0', '0', '0']]

答案 1 :(得分:0)

在你的代码中,你在内部循环中有一个return语句:

def file(name):
    fn = open(name)
    for line in fn:
        line_list = line.strip().split(";")
        main_list.append(line_list)
        return # Here

尝试解决这个问题,看看程序的行为方式如何。

为了更清楚地了解发生了什么,您可以尝试添加一些打印语句来查看流程:

def file(name):
    fn = open(name)
    for line in fn:
        print('a')
        line_list = line.strip().split(";")
        main_list.append(line_list)
        print('b')
        return # Here
        print('c')