1

所以我在做输入/输出,我试图让我的程序的逻辑下来,因为给定的名称和课程,我会按字母顺序将名称写入文件,然后是他们学习的课程。我完成了这一点,并将所有内容都写到了一个正在处理的列表中。现在我试图写入一个文本文件"name, class, .....(if more than one class)" ,但由于我已经将它变成一个一维列表,程序逐项写入,而不是名称和类一起摸索。例如,我希望读取新文件

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

如果我有一个 d 列表,我该怎么做。我正在考虑写类似的东西

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

问题在于它是一个单一的列表,我不确定如何检测名称更改。无论如何将其转换为 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']]

为了组织我写了下面的代码,把它附加到一个错误的列表中

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

输出是

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

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

4

3 回答 3

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

输出

[['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']]]
于 2013-10-30T01:12:42.130 回答
0

您可以使用 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]
于 2013-10-30T01:19:34.613 回答
0

一种替代方法是将其转换为字典:

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

输出:

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

然后,打印它:

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

输出:

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

将其存储在字典中也比将其存储在多维数组中更快、更有效。看看这个:

>>> 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
于 2013-10-30T01:23:49.023 回答