gpt4 book ai didi

python - 按列迭代 3D 列表

转载 作者:太空宇宙 更新时间:2023-11-04 05:41:58 25 4
gpt4 key购买 nike

我有一个文本文件:

example_table.txt

EmployeeID、EmployeeName、EmployeeService、EmployeeJob、EmployeeNum
12039,鲍勃,早上,出租车司机,03489293489
98734,Jacob,Evening,Bus Driver,02084928349
48023,Jenny,Night,登记服务员,02932389045
23490,Andrew,Morning,老师,03941826384
34792,鹦鹉螺,夜,护士,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

最佳答案

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

>>> 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']}

关于python - 按列迭代 3D 列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33635460/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com