2013-10-29 71 views
1

我有一個程序,其中有一個名稱和類的列表。我按字母順序排列名單。現在即時通訊嘗試檢查名稱是否重複,將類添加到單個名稱。檢查二維列表中的重複字符串

我嘗試寫這樣

一些代碼經過的名字,如果名稱已在列表中的類添加到一個名稱。這樣的例子是,而不必

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

我只想有

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

我將如何去在logival的方式這樣做,而沒有使用任何的內置函數?我試圖比較指數,如

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

list[i+1][1]附加到emptylist。雖然這幾乎奏效,但它會在某些方面出現問題。這是我的嘗試

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

什麼是期望的輸出? – Christian

回答

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']] 
+0

關閉但不正確。有些名字重複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']] 
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']] 

編輯:添加處理案件時存在多於兩個具有相同作者姓名的條目。

+0

好吧好吧,我看看你在那裏做什麼。當一個人有超過3個與他們相關的課程時,問題就會發生。例如伊恩有三個班級,所以他在你的輸出中出現了兩次。而tre shaun只有他的名字後面有2個班級,即使他拿了3個。我開始想改變我的方法,因爲我似乎永遠無法得到它的權利 –

+0

是的,有一些問題,你追加的東西:) – kiddorails

+0

@Zach:我已經更新了一些我之前使用過的縮進陷阱。這應該如預期現在 – kiddorails