我有一个程序,我有一个名单和类的列表。我按字母顺序排列。现在我试图检查名称是否重复,将类添加到一个单一的名称。
我试着写一些像
这样的代码如果名称已在列表中,则浏览名称,将类添加到一个名称。所以一个例子就是,而不是
['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']
答案 0 :(得分: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']]
答案 1 :(得分: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']]
答案 2 :(得分: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个以上条目的情况。