1

我有一个程序,其中有一个名称和类的列表。我有按字母顺序排列的列表。现在我试图检查名称是否重复,将类添加到一个名称中。

我正在尝试编写一些代码,例如

如果名称已经在列表中,则遍历名称,将类添加到一个名称中。所以一个例子是,而不是

['Anita','phys 1443'], ['Anita','IE 3312']

我会的

['Anita','PHYS 1443','IE 3312']

在不使用任何内置函数的情况下,我将如何以逻辑方式执行此操作?我试着比较像

if list[i][0] == list[i+1][0]

附加list[i+1][1]到一个空列表。虽然这几乎奏效了,但它会在某些时候搞砸。这是我的尝试

size = len(c)
i = 0
c = [['Anita', 'PHYS 1443'], ['Anita', 'IE 3312'], ['Beihuang', 'PHYS 1443'], ['Chiao-Lin', 'MATH 1426'], ['Chiao-Lin', 'IE 3312'], ['Christopher', 'CSE 1310'], ['Dylan', 'CSE 1320'], ['Edmund', 'PHYS 1443'], ['Ian', 'IE 3301'], ['Ian', 'CSE 1320'], ['Ian', 'PHYS 1443'], ['Isis', 'PHYS 1443'], ['Jonathan', 'MATH 2325'], ['Krishna', 'MATH 2325'], ['Michael', 'IE 3301'], ['Nang', 'MATH 2325'], ['Ram', 'CSE 1320'], ['Taesu', 'CSE 1320'], ["Tre'Shaun", 'IE 3312'], ["Tre'Shaun", 'MATH 2325'], ["Tre'Shaun", 'CSE 1310']]
## Check if any names repeat
d.append(c[0][0])
while i < size - 1 :
    if c[i][0] == c[i+1][0] :

        d.append(c[i][1])
        d.append(c[i+1][1])
    else :
        d.append(c[i+1][0])
        d.append(c[i+1][1])
    i = i + 1
print d

输出是。

['Anita', 'PHYS 1443', 'IE 3312', 'Beihuang', 'PHYS 1443', 'Chiao-Lin', 'MATH 1426', 'MATH 1426', 'IE 3312', 'Christopher', 'CSE 1310', 'Dylan', 'CSE 1320', 'Edmund', 'PHYS 1443', 'Ian', 'IE 3301', 'IE 3301', 'CSE 1320', 'CSE 1320', 'PHYS 1443', 'Isis', 'PHYS 1443', 'Jonathan', 'MATH 2325', 'Krishna', 'MATH 2325', 'Michael', 'IE 3301', 'Nang', 'MATH 2325', 'Ram', 'CSE 1320', 'Taesu', 'CSE 1320', "Tre'Shaun", 'IE 3312', 'IE 3312', 'MATH 2325', 'MATH 2325', 'CSE 1310']
4

3 回答 3

1

您可以使用集合的defaultdict. 您可以使用defaultdict创建一个字典,其中每个键默认映射到一个空列表,因此您可以为列表中的每个人累积值。

In [1]: c = [['Anita', 'PHYS 1443'], ['Anita', 'IE 3312'], ['Beihuang', 'PHYS 1443'], ['Chiao-Lin', 'MATH 1426'], ['Chiao-Lin', 'IE 3312'], ['Christopher', 'CSE 1310'], ['Dylan', 'CSE 1320'], ['Edmund', 'PHYS 1443'], ['Ian', 'IE 3301'], ['Ian', 'CSE 1320'], ['Ian', 'PHYS 1443'], ['Isis', 'PHYS 1443'], ['Jonathan', 'MATH 2325'], ['Krishna', 'MATH 2325'], ['Michael', 'IE 3301'], ['Nang', 'MATH 2325'], ['Ram', 'CSE 1320'], ['Taesu', 'CSE 1320'], ["Tre'Shaun", 'IE 3312'], ["Tre'Shaun", 'MATH 2325'], ["Tre'Shaun", 'CSE 1310']]
In [2]: from collections import defaultdict
In [3]: result = defaultdict(list)
In [4]: for a in c:
            result[a[0]].append( a[1] )
In [5]: result
Out[1]: defaultdict(<type 'list'>, {'Krishna': ['MATH 2325'], 'Dylan': ['CSE 1320'], 'Isis': ['PHYS 1443'], 'Jonathan': ['MATH 2325'], 'Ram': ['CSE 1320'], 'Michael': ['IE 3301'], 'Nang': ['MATH 2325'], 'Ian': ['IE 3301', 'CSE 1320', 'PHYS 1443'], 'Christopher': ['CSE 1310'], 'Edmund': ['PHYS 1443'], "Tre'Shaun": ['IE 3312', 'MATH 2325', 'CSE 1310'], 'Beihuang': ['PHYS 1443'], 'Chiao-Lin': ['MATH 1426', 'IE 3312'], 'Anita': ['PHYS 1443', 'IE 3312'], 'Taesu': ['CSE 1320']})

然后,您可以将此字典转换回列表:

In [6]: list_result = [ [k] + v for k, v in result.items() ]
In [7]: list_result
Out[2]: [['Krishna', 'MATH 2325'],
 ['Dylan', 'CSE 1320'],
 ['Isis', 'PHYS 1443'],
 ['Jonathan', 'MATH 2325'],
 ['Ram', 'CSE 1320'],
 ['Michael', 'IE 3301'],
 ['Nang', 'MATH 2325'],
 ['Ian', 'IE 3301', 'CSE 1320', 'PHYS 1443'],
 ['Christopher', 'CSE 1310'],
 ['Edmund', 'PHYS 1443'],
 ["Tre'Shaun", 'IE 3312', 'MATH 2325', 'CSE 1310'],
 ['Beihuang', 'PHYS 1443'],
 ['Chiao-Lin', 'MATH 1426', 'IE 3312'],
 ['Anita', 'PHYS 1443', 'IE 3312'],
 ['Taesu', 'CSE 1320']]
于 2013-10-29T15:58:25.300 回答
1

你可能想要这样的东西:

d = []
vi = 0
c = [['Anita', 'PHYS 1443'], ['Anita', 'IE 3312'], ['Beihuang', 'PHYS 1443'], ['Chiao-Lin', 'MATH 1426'], ['Chiao-Lin', 'IE 3312'], ['Christopher', 'CSE 1310'], ['Dylan', 'CSE 1320'], ['Edmund', 'PHYS 1443'], ['Ian', 'IE 3301'], ['Ian', 'CSE 1320'], ['Ian', 'PHYS 1443'], ['Isis', 'PHYS 1443'], ['Jonathan', 'MATH 2325'], ['Krishna', 'MATH 2325'], ['Michael', 'IE 3301'], ['Nang', 'MATH 2325'], ['Ram', 'CSE 1320'], ['Taesu', 'CSE 1320'], ["Tre'Shaun", 'IE 3312'], ["Tre'Shaun", 'MATH 2325'], ["Tre'Shaun", 'CSE 1310']]
size = len(c)
## Check if any names repeat
while i < size - 1:
    if c[i][0] == c[i+1][0] :
        temp = c[i]
        temp.append(c[i+1][1])
        d.append(temp)
    else :
        d.append(c[i+1])
    i = i + 1
print d

输出

>>> [['Anita', 'PHYS 1443', 'IE 3312'], ['Beihuang', 'PHYS 1443'],...,
    ["Tre'Shaun", 'MATH 2325', 'CSE 1310']]
于 2013-10-29T15:52:08.770 回答
0

我希望这可以纠正您正在寻找的逻辑:

c = [['Anita', 'PHYS 1443'], ['Anita', 'IE 3312'], ['Anita', 'CS169'], ['Beihuang', 'PHYS 1443'], ['Chiao-Lin', 'MATH 1426'], ['Chiao-Lin', 'IE 3312'], ['Christopher', 'CSE 1310'], ['Dylan', 'CSE 1320'], ['Edmund', 'PHYS 1443'], ['Ian', 'IE 3301'], ['Ian', 'CSE 1320'], ['Ian', 'PHYS 1443'], ['Isis', 'PHYS 1443'], ['Jonathan', 'MATH 2325'], ['Krishna', 'MATH 2325'], ['Michael', 'IE 3301'], ['Nang', 'MATH 2325'], ['Ram', 'CSE 1320'], ['Taesu', 'CSE 1320'], ["Tre'Shaun", 'IE 3312'], ["Tre'Shaun", 'MATH 2325'], ["Tre'Shaun", 'CSE 1310']]

i = 0
d = list()
size = len(c)
## Check if any names repeat
while i < (size - 1):
    if(c[i][0] == c[i+1][0]):
        #append the next course in itself. 
        c[i].append(c[i+1][1])
        #remove the next entry, since we have included the course in that author
        c.remove(c[i+1])
        #decrease the size of iteration by 1
        size -= 1
    else:
        #when next entry is not with same name, append it in d
        d.append(c[i])
        #and move on
        i += 1

print(d)

结果:

[['Anita', 'PHYS 1443', 'IE 3312', 'CS169'], ['Beihuang', 'PHYS 1443'], ['Chiao-Lin', 'MATH 1426', 'IE 3312'], ['Christopher', 'CSE 1310'], ['Dylan', 'CSE 1320'], ['Edmund', 'PHYS 1443'], ['Ian', 'IE 3301', 'CSE 1320'], ['Ian', 'PHYS 1443'], ['Isis', 'PHYS 1443'], ['Jonathan', 'MATH 2325'], ['Krishna', 'MATH 2325'], ['Michael', 'IE 3301'], ['Nang', 'MATH 2325'], ['Ram', 'CSE 1320'], ['Taesu', 'CSE 1320'], ["Tre'Shaun", 'IE 3312', 'MATH 2325']]

编辑:添加以处理存在超过 2 个具有相同作者姓名的条目时的情况。

于 2013-10-29T16:00:03.423 回答