如何将整个元素列表添加到一个列表中?

时间:2013-08-22 04:27:24

标签: python list

这是我的代码。我必须在这里添加一个完整的列表:( http://pastebin.com/u5S0rF9D)到列表中,我该怎么做?这是我导入到python中的excel文件(https://docs.google.com/spreadsheet/ccc?key=0Atza5UMAhSHRdHJMWGZqZlRrZWpySnU1SHhKOXFlN2c#gid=0) 我将什么“追加”到maleList?

import random

gender = raw_input("Enter your character gender (Male/Female): ")
start = raw_input("Enter Please enter starting letter of name(A to B): ")

import csv
readerFileHandle = open("Book1.csv", "rb")
malenames = csv.reader(readerFileHandle)
for row in malenames:
    y = []
    for x in row:
        if x[-1] == '\xa0':
            y.append(x[:-2])
        else:
            y.append(x)
    for z in y:       
        print z
maleList = []
maleList.append()
print maleList 
readerFileHandle.close()

2 个答案:

答案 0 :(得分:2)

如果你想将它们全部放在一个列表中,比如粘贴框,只需直接附加它们

malenames = csv.reader(readerFileHandle)
maleList = []
for row in malenames:
    for x in row:
        maleList.append(x.rstrip('\xa0'))

你可以把它写成列表理解

malenames = csv.reader(readerFileHandle)
maleList = [x.rstrip('\xa0') for row in malenames for x in row]

编辑:看起来有一些组合'\xc2\xa0'附加到某些条目的末尾。所以应该x.rstrip('\xc2\xa0')正确清理它们

答案 1 :(得分:0)

如果你有一个= [1,2]和b = [3,4],并希望将列表b附加到列表a,那么

>>> a=[1,2]
>>> b=[3,4]
>>> a.extend(b)
>>> a
[1, 2, 3, 4]