将一个d列表转换为2d列表(写入错误)

时间:2022-10-29 18:10:46

so im doing input/output and i was trying to get the logic of my program down, being that given names and courses, i would write to the file the names in alphabetical order, followed by the courses they took. i accomplished that and wrote everything to a one d list in the progress. now im trying to wrtie to a text file "name, class, .....(if more than one class)" but since i have made it into a 1d list the program writes item by item and not names and classes groped together. ex, i want the new file to read

所以我在做输入/输出,我试着把我的程序的逻辑写下来,因为给定的名字和课程,我会把名字按字母顺序写在文件上,然后是他们选的课程。我完成了这个任务,并把所有的东西都写进了一个一维列表。现在我正试着去写一个文本文件:“姓名,班级,……”(如果不止一个类)“但是由于我已经把它变成了一个一维列表,程序就会逐项写入,而不是名字和类一起摸索。ex,我要读取新文件。

Ashley,MATH 1426,PHYS 1443
Jonathan,IE 3312
Joseph,IE 3312
Nang,MATH 1426
Ram,IE 3312
Randal,IE 3301,MATH 2325,PHYS 1443
Sol,IE 3301

how do i do this if i have a one d list. i was thinking of writing something like

如果我有一个一维列表,我该怎么做呢?我想写一些类似的东西

while name, remains the same, write the classes, when name changes print newline....., write name and classes

problem with this is its a oned list and im not sure how to detect a name change. Is there anyway to convert this into a 2dlist each sublist containing one name and its classes? here is my original 2d list in its unorginzed form,

问题是这是一个名单,我不确定如何检测一个名字的变化。有没有办法把它转换成一个2dlist每个子列表包含一个名字和它的类?这是我原始的2d列表,在它的未组织形式中,

[['Adam', 'PHYS 1443'], ['Ashley', 'IE 3312'], ['Ashley', 'PHYS 1443'], ['August', 'PHYS 1444'], ['Baron', 'PHYS 1443'], ['Christopher', 'IE 3301'], ['Christopher', 'CSE 1320'], ['Christopher', 'PHYS 1443'], ['Dylan', 'CSE 1310'], ['Henry', 'PHYS 1444'], ['James', 'IE 3301'], ['James', 'PHYS 1443'], ['Jonathan', 'IE 3312'], ['Krishna', 'CSE 1310'], ['Luis', 'CSE 1310'], ['Michael', 'IE 3301'], ['Nang', 'PHYS 1443'], ['Pramod', 'PHYS 1444'], ['Pramod', 'PHYS 1443'], ['Saroj', 'IE 3301'], ['Saroj', 'MATH 1426'], ['Sol', 'CSE 1310'], ['Timothy', 'MATH 2325'], ['Timothy', 'IE 3301']]

to orginize i wrote the following code, appending it to a list which was a mistake

为了实现orginize,我编写了以下代码,并将其附加到一个错误的列表中

d = []
size = len(c)
two = []
d.append(c[0][0])
d.append(c[0][1])
i = 1
while i < size  :
    # if current name = previous name, add classes
    if c[i][0]==c[i-1][0] :
        d.append(c[i][1])  
    # if current name != previous name, add name and classes
    if c[i][0]!= c[i-1][0] :
        d.append(c[i][0])
        d.append(c[i][1])
    i = i + 1

output was

输出是

['Adam', 'PHYS 1443', 'Ashley', 'IE 3312', 'PHYS 1443', 'August', 'PHYS 1444', 'Baron', 'PHYS 1443', 'Christopher', 'IE 3301', 'CSE 1320', 'PHYS 1443', 'Dylan', 'CSE 1310', 'Henry', 'PHYS 1444', 'James', 'IE 3301', 'PHYS 1443', 'Jonathan', 'IE 3312', 'Krishna', 'CSE 1310', 'Luis', 'CSE 1310', 'Michael', 'IE 3301', 'Nang', 'PHYS 1443', 'Pramod', 'PHYS 1444', 'PHYS 1443', 'Saroj', 'IE 3301', 'MATH 1426', 'Sol', 'CSE 1310', 'Timothy', 'MATH 2325', 'IE 3301']

is there any easy fix to this?

有什么简单的解决办法吗?

3 个解决方案

#1


2  

import itertools
print [[key, [cls[1] for cls in list(group)]]
        for key, group in itertools.groupby(data, key=lambda x: x[0])]

Output

输出

[['Adam', ['PHYS 1443']],
 ['Ashley', ['IE 3312', 'PHYS 1443']],
 ['August', ['PHYS 1444']],
 ['Baron', ['PHYS 1443']],
 ['Christopher', ['IE 3301', 'CSE 1320', 'PHYS 1443']],
 ['Dylan', ['CSE 1310']],
 ['Henry', ['PHYS 1444']],
 ['James', ['IE 3301', 'PHYS 1443']],
 ['Jonathan', ['IE 3312']],
 ['Krishna', ['CSE 1310']],
 ['Luis', ['CSE 1310']],
 ['Michael', ['IE 3301']],
 ['Nang', ['PHYS 1443']],
 ['Pramod', ['PHYS 1444', 'PHYS 1443']],
 ['Saroj', ['IE 3301', 'MATH 1426']],
 ['Sol', ['CSE 1310']],
 ['Timothy', ['MATH 2325', 'IE 3301']]]

#2


0  

You can use itertools.groupby() like @thefourtheye or a defaultdict to organize by names :

可以使用itertools.groupby(),如@thefourtheye或defaultdict来按名称组织:

from collections import defaultdict
>>> onedlist = zip(["ADAM","JOHN"]*5,range(10))
[('ADAM', 0), ('JOHN', 1), ('ADAM', 2), ('JOHN', 3), ('ADAM', 4), ('JOHN', 5), ('ADAM', 6), ('JOHN', 7), ('ADAM', 8), ('JOHN', 9)]
>>> my_dict = defaultdict(list)
>>> for name,val in onedlist: my_dict[name].append(val)
>>> print my_dict["ADAM"]
[1,2,3,4,5]

#3


0  

One alternative is to convert it to a dictionary instead:

一种替代方法是将它转换成字典:

d = {}
for k,v in l: #l is your list
    if d[k]:
        d[k].append(v)
    else:
        d[k] = v

Output:

输出:

{'Krishna': ['CSE 1310'], 'Dylan': ['CSE 1310'], 'Ashley': ['IE 3312', 'PHYS 1443'], 'Baron': ['PHYS 1443'], 'Timothy': ['MATH 2325', 'IE 3301'], 'James': ['IE 3301', 'PHYS 1443'], 'Pramod': ['PHYS 1444', 'PHYS 1443'], 'Michael': ['IE 3301'], 'Sol': ['CSE 1310'], 'Nang': ['PHYS 1443'], 'Christopher': ['IE 3301', 'CSE 1320', 'PHYS 1443'], 'Adam': ['PHYS 1443'], 'Henry': ['PHYS 1444'], 'Saroj': ['IE 3301', 'MATH 1426'], 'Luis': ['CSE 1310'], 'Jonathan': ['IE 3312'], 'August': ['PHYS 1444']}

And then, printing it:

然后,打印:

>>> for k,v in d.items(): #iterates through d
    print(k, end=' ')
    for i in v:
        print (i, end=" ")
    print()

Output:

输出:

Krishna CSE 1310 
Dylan CSE 1310 
Ashley IE 3312 PHYS 1443 
Baron PHYS 1443 
Timothy MATH 2325 IE 3301 
James IE 3301 PHYS 1443 
Henry PHYS 1444 
Michael IE 3301 
Sol CSE 1310 
Nang PHYS 1443 
August PHYS 1444 
Christopher IE 3301 CSE 1320 PHYS 1443 
Adam PHYS 1443 
Pramod PHYS 1444 PHYS 1443 
Luis CSE 1310 
Saroj IE 3301 MATH 142

Storing it in a dictionary is also much faster and efficient than storing it in a multi-dimensional array. Take a look at this:

将其存储在字典中比将其存储在多维数组中要快得多。看看这个:

>>> timeit("""[['Adam', ['PHYS 1443']],
 ['Ashley', ['IE 3312', 'PHYS 1443']],
 ['August', ['PHYS 1444']],
 ['Baron', ['PHYS 1443']],
 ['Christopher', ['IE 3301', 'CSE 1320', 'PHYS 1443']],
 ['Dylan', ['CSE 1310']],
 ['Henry', ['PHYS 1444']],
 ['James', ['IE 3301', 'PHYS 1443']],
 ['Jonathan', ['IE 3312']],
 ['Krishna', ['CSE 1310']],
 ['Luis', ['CSE 1310']],
 ['Michael', ['IE 3301']],
 ['Nang', ['PHYS 1443']],
 ['Pramod', ['PHYS 1444', 'PHYS 1443']],
 ['Saroj', ['IE 3301', 'MATH 1426']],
 ['Sol', ['CSE 1310']],
 ['Timothy', ['MATH 2325', 'IE 3301']]]""")
5.6225116937032311
>>> timeit("""{'Krishna': ['CSE 1310'], 'Dylan': ['CSE 1310'], 'Ashley': ['IE 3312', 'PHYS 1443'], 'Baron': ['PHYS 1443'], 'Timothy': ['MATH 2325', 'IE 3301'], 'James': ['IE 3301', 'PHYS 1443'], 'Pramod': ['PHYS 1444', 'PHYS 1443'], 'Michael': ['IE 3301'], 'Sol': ['CSE 1310'], 'Nang': ['PHYS 1443'], 'Christopher': ['IE 3301', 'CSE 1320', 'PHYS 1443'], 'Adam': ['PHYS 1443'], 'Henry': ['PHYS 1444'], 'Saroj': ['IE 3301', 'MATH 1426'], 'Luis': ['CSE 1310'], 'Jonathan': ['IE 3312'], 'August': ['PHYS 1444']}
""")
3.4978408664244967

#1


2  

import itertools
print [[key, [cls[1] for cls in list(group)]]
        for key, group in itertools.groupby(data, key=lambda x: x[0])]

Output

输出

[['Adam', ['PHYS 1443']],
 ['Ashley', ['IE 3312', 'PHYS 1443']],
 ['August', ['PHYS 1444']],
 ['Baron', ['PHYS 1443']],
 ['Christopher', ['IE 3301', 'CSE 1320', 'PHYS 1443']],
 ['Dylan', ['CSE 1310']],
 ['Henry', ['PHYS 1444']],
 ['James', ['IE 3301', 'PHYS 1443']],
 ['Jonathan', ['IE 3312']],
 ['Krishna', ['CSE 1310']],
 ['Luis', ['CSE 1310']],
 ['Michael', ['IE 3301']],
 ['Nang', ['PHYS 1443']],
 ['Pramod', ['PHYS 1444', 'PHYS 1443']],
 ['Saroj', ['IE 3301', 'MATH 1426']],
 ['Sol', ['CSE 1310']],
 ['Timothy', ['MATH 2325', 'IE 3301']]]

#2


0  

You can use itertools.groupby() like @thefourtheye or a defaultdict to organize by names :

可以使用itertools.groupby(),如@thefourtheye或defaultdict来按名称组织:

from collections import defaultdict
>>> onedlist = zip(["ADAM","JOHN"]*5,range(10))
[('ADAM', 0), ('JOHN', 1), ('ADAM', 2), ('JOHN', 3), ('ADAM', 4), ('JOHN', 5), ('ADAM', 6), ('JOHN', 7), ('ADAM', 8), ('JOHN', 9)]
>>> my_dict = defaultdict(list)
>>> for name,val in onedlist: my_dict[name].append(val)
>>> print my_dict["ADAM"]
[1,2,3,4,5]

#3


0  

One alternative is to convert it to a dictionary instead:

一种替代方法是将它转换成字典:

d = {}
for k,v in l: #l is your list
    if d[k]:
        d[k].append(v)
    else:
        d[k] = v

Output:

输出:

{'Krishna': ['CSE 1310'], 'Dylan': ['CSE 1310'], 'Ashley': ['IE 3312', 'PHYS 1443'], 'Baron': ['PHYS 1443'], 'Timothy': ['MATH 2325', 'IE 3301'], 'James': ['IE 3301', 'PHYS 1443'], 'Pramod': ['PHYS 1444', 'PHYS 1443'], 'Michael': ['IE 3301'], 'Sol': ['CSE 1310'], 'Nang': ['PHYS 1443'], 'Christopher': ['IE 3301', 'CSE 1320', 'PHYS 1443'], 'Adam': ['PHYS 1443'], 'Henry': ['PHYS 1444'], 'Saroj': ['IE 3301', 'MATH 1426'], 'Luis': ['CSE 1310'], 'Jonathan': ['IE 3312'], 'August': ['PHYS 1444']}

And then, printing it:

然后,打印:

>>> for k,v in d.items(): #iterates through d
    print(k, end=' ')
    for i in v:
        print (i, end=" ")
    print()

Output:

输出:

Krishna CSE 1310 
Dylan CSE 1310 
Ashley IE 3312 PHYS 1443 
Baron PHYS 1443 
Timothy MATH 2325 IE 3301 
James IE 3301 PHYS 1443 
Henry PHYS 1444 
Michael IE 3301 
Sol CSE 1310 
Nang PHYS 1443 
August PHYS 1444 
Christopher IE 3301 CSE 1320 PHYS 1443 
Adam PHYS 1443 
Pramod PHYS 1444 PHYS 1443 
Luis CSE 1310 
Saroj IE 3301 MATH 142

Storing it in a dictionary is also much faster and efficient than storing it in a multi-dimensional array. Take a look at this:

将其存储在字典中比将其存储在多维数组中要快得多。看看这个:

>>> timeit("""[['Adam', ['PHYS 1443']],
 ['Ashley', ['IE 3312', 'PHYS 1443']],
 ['August', ['PHYS 1444']],
 ['Baron', ['PHYS 1443']],
 ['Christopher', ['IE 3301', 'CSE 1320', 'PHYS 1443']],
 ['Dylan', ['CSE 1310']],
 ['Henry', ['PHYS 1444']],
 ['James', ['IE 3301', 'PHYS 1443']],
 ['Jonathan', ['IE 3312']],
 ['Krishna', ['CSE 1310']],
 ['Luis', ['CSE 1310']],
 ['Michael', ['IE 3301']],
 ['Nang', ['PHYS 1443']],
 ['Pramod', ['PHYS 1444', 'PHYS 1443']],
 ['Saroj', ['IE 3301', 'MATH 1426']],
 ['Sol', ['CSE 1310']],
 ['Timothy', ['MATH 2325', 'IE 3301']]]""")
5.6225116937032311
>>> timeit("""{'Krishna': ['CSE 1310'], 'Dylan': ['CSE 1310'], 'Ashley': ['IE 3312', 'PHYS 1443'], 'Baron': ['PHYS 1443'], 'Timothy': ['MATH 2325', 'IE 3301'], 'James': ['IE 3301', 'PHYS 1443'], 'Pramod': ['PHYS 1444', 'PHYS 1443'], 'Michael': ['IE 3301'], 'Sol': ['CSE 1310'], 'Nang': ['PHYS 1443'], 'Christopher': ['IE 3301', 'CSE 1320', 'PHYS 1443'], 'Adam': ['PHYS 1443'], 'Henry': ['PHYS 1444'], 'Saroj': ['IE 3301', 'MATH 1426'], 'Luis': ['CSE 1310'], 'Jonathan': ['IE 3312'], 'August': ['PHYS 1444']}
""")
3.4978408664244967