按列

时间:2015-11-10 17:00:34

标签: python python-3.x

我有一个文本文件:

example_table.txt

EmployeeID,EmployeeName,EmployeeService,EmployeeJob,EmployeeNum 12039,Bob,Morning,Taxi Driver,03489293489 98734,Jacob,Evening,Bus Driver,02084928349 48023,Jenny,Night,Register Attendant,02932389045 23490,Andrew,Morning,Teacher,03941826384 34792,Nautilus,Night,Nurse,02678439985 58023,Ulysses,Evening,Watchman,0983748932

我制作了一个将其变成3D列表的函数:

#NOTE - Take x to be open("example_table.txt","r+")
#NOTE - Take y to be [i.split(",") for i in x.readlines()]
def splitter(lis, char):
    x = []
    for i in range(len(lis[0].split(char))):
        x.append(lis[i].split(char))
    return x

如果我致电splitter(y,","),它会给我以下列表:

[
['EmployeeID,EmployeeName,EmployeeService,EmployeeJob,EmployeeNum'],
['12039,Bob,Morning,Taxi Driver,03489293489'], 
['98734,Jacob,Evening,Bus Driver,02084928349'], 
['48023,Jenny,Night,Register Attendant,02932389045'], 
['23490,Andrew,Morning,Teacher,03941826384'], 
['34792,Nautilus,Night,Nurse,02678439985'], 
['58023,Ulysses,Evening,Watchman,0983748932']
]

我想像这样迭代:

#Take p = splitter(y,",")
p[0][1 to amount of rows] # as in p[0][2], p[0][3], p[0][4]
p[1][1 to amount of rows] # same as above, only 0 is 1
#Until...                 # et cetera, et cetera
p[4][1 to amount of rows] # Final iteration

所以,

FOR every_row in p:
    FOR every_column in p:
        ITERATE every_row through every_column /*ONCE!!*/

我还想将这些结果存储在字典中。

v = {
    'EmployeeID':['12309','98734',...,'58023'],
    '...':[...],
    'EmployeeNum':['03489293489',...]
    }

我也希望这适用于任何数量的列和行。有什么帮助吗?

BUZZYSIN

2 个答案:

答案 0 :(得分:1)

你可以做一个简单的列表comp,用.split('.')将字符串拆分成列表:

>>> l =[
['EmployeeID,EmployeeName,EmployeeService,EmployeeJob,EmployeeNum'],
['12039,Bob,Morning,Taxi Driver,03489293489'],
['98734,Jacob,Evening,Bus Driver,02084928349'],
['48023,Jenny,Night,Register Attendant,02932389045'],
['23490,Andrew,Morning,Teacher,03941826384'],
['34792,Nautilus,Night,Nurse,02678439985'],
['58023,Ulysses,Evening,Watchman,0983748932']
]
>>> l = [x[0].split(',') for x in l]
>>> l
[['EmployeeID', 'EmployeeName', 'EmployeeService', 'EmployeeJob', 'EmployeeNum'], ['12039', 'Bob', 'Morning', 'Taxi Driver', '03489293489'], ['98734', 'Jacob', 'Evening', 'Bus Driver', '02084928349'], ['48023', 'Jenny', 'Night', 'Register Attendant', '02932389045'], ['23490', 'Andrew', 'Morning', 'Teacher', '03941826384'], ['34792', 'Nautilus', 'Night', 'Nurse', '02678439985'], ['58023', 'Ulysses', 'Evening', 'Watchman', '0983748932']]

然后,您可以重新整理列表,以更好地满足您的需求:

>>> keys = l[0]
>>> keys
['EmployeeID', 'EmployeeName', 'EmployeeService', 'EmployeeJob', 'EmployeeNum']
>>> values = [[] for _ in range(len(keys))]
>>> for li in l[1:]: #exclude the keys
    for index,item in enumerate(li):
        values[index].append(item)
>>> values
[['12039', '98734', '48023', '23490', '34792', '58023'], ['Bob', 'Jacob', 'Jenny', 'Andrew', 'Nautilus', 'Ulysses'], ['Morning', 'Evening', 'Night', 'Morning', 'Night', 'Evening'], ['Taxi Driver', 'Bus Driver', 'Register Attendant', 'Teacher', 'Nurse', 'Watchman'], ['03489293489', '02084928349', '02932389045', '03941826384', '02678439985', '0983748932']]

并且使用这些,它是一个简单的dict comp:

>>> d = {key:val for key,val in zip(keys,values)}
>>> d
{'EmployeeNum': ['03489293489', '02084928349', '02932389045', '03941826384', '02678439985', '0983748932'], 'EmployeeID': ['12039', '98734', '48023', '23490', '34792', '58023'], 'EmployeeService': ['Morning', 'Evening', 'Night', 'Morning', 'Night', 'Evening'], 'EmployeeJob': ['Taxi Driver', 'Bus Driver', 'Register Attendant', 'Teacher', 'Nurse', 'Watchman'], 'EmployeeName': ['Bob', 'Jacob', 'Jenny', 'Andrew', 'Nautilus', 'Ulysses']}

答案 1 :(得分:0)

您可以执行以下操作从文件中创建字典:

with open('example_table.txt') as employee_file:
    headers, *rows = [r.rstrip('\n').split(',') for r in employee_file]
employees = dict(zip(headers, zip(*rows)))

headers, *rows = something是一个Python 3-ism,是headers, rows = something[0], something[1:]的缩写。

zip(*rows)将采用2乘2的列表(列表列表)并转置它。

所以如果rows是:

[['12039', 'Bob', 'Morning', 'Taxi Driver', '03489293489'],
 ['98734', 'Jacob', 'Evening', 'Bus Driver', '02084928349'],
 ['48023', 'Jenny', 'Night', 'Register Attendant', '02932389045'],
 ['23490', 'Andrew', 'Morning', 'Teacher', '03941826384'],
 ['34792', 'Nautilus', 'Night', 'Nurse', '02678439985'],
 ['58023', 'Ulysses', 'Evening', 'Watchman', '0983748932']]

然后list(zip(*rows))会返回:

[('12039', '98734', '48023', '23490', '34792', '58023'),
 ('Bob', 'Jacob', 'Jenny', 'Andrew', 'Nautilus', 'Ulysses'),
 ('Morning', 'Evening', 'Night', 'Morning', 'Night', 'Evening'),
 ('Taxi Driver', 'Bus Driver', 'Register Attendant', 'Teacher', 'Nurse', 'Watchman'),
 ('03489293489', '02084928349', '02932389045', '03941826384', '02678439985', '0983748932')]

dict构造函数将接受一系列键值对并从中生成一个字典,因此将标题拉到相应的值列表(或实际上是这种情况下的元组)将创建我们的字典:

{'EmployeeJob': ('Taxi Driver', 'Bus Driver', 'Register Attendant', 'Teacher', 'Nurse', 'Watchman'),
 'EmployeeService': ('Morning', 'Evening', 'Night', 'Morning', 'Night', 'Evening'),
 'EmployeeNum': ('03489293489', '02084928349', '02932389045', '03941826384', '02678439985', '0983748932'),
 'EmployeeName': ('Bob', 'Jacob', 'Jenny', 'Andrew', 'Nautilus', 'Ulysses'),
 'EmployeeID': ('12039', '98734', '48023', '23490', '34792', '58023')}

如果您需要将值设为列表而不是元组,则可以在最后一行使用map将每个元组转换为列表:

employees = dict(zip(headers, map(list, zip(*rows))))

所以最终结果如下:

>>> with open('example_table.txt') as employee_file:
...     headers, *rows = [r.rstrip('\n').split(',') for r in employee_file]
...
>>> employees = dict(zip(headers, map(list, zip(*rows))))
>>> employees
{'EmployeeJob': ['Taxi Driver', 'Bus Driver', 'Register Attendant', 'Teacher', 'Nurse', 'Watchman'], 'EmployeeNum': ['03489293489', '02084928349', '02932389045', '03941826384', '02678439985', '0983748932'], 'EmployeeID': ['12039', '98734', '48023', '23490', '34792', '58023'], 'EmployeeName': ['Bob', 'Jacob', 'Jenny', 'Andrew', 'Nautilus', 'Ulysses'], 'EmployeeService': ['Morning', 'Evening', 'Night', 'Morning', 'Night', 'Evening']}