2013-05-20 81 views
0

我有這樣一個CSV文件蟒蛇3 CSV數據結構問題

Category Subcategory 
----------------------- 
cat   panther 
cat   tiger 
dog   wolf 
dog   heyena 
cat   lion 
dog   beagle 

我試着去編寫一個腳本,輸出這樣的事情(順序並不重要):到目前爲止

animals = [ 
       [['cat'], ['panther', 'tiger', 'lion']], 
       [['dog'], ['wolf', 'heyena', 'beagle']] 
      ] 

我我能夠製作唯一類別的列表以及唯一子類別的列表。

for p in infile: 
    if(p[0] not in catlist): 
     catlist.append(p[0]) 
    if(p[1] not in subcatlist) : 
     subcatlist.append(p[1]) 

但我無法寫,說的邏輯「如果分類‘貓’是動物[],但‘豹’是不是‘貓’,追加吧。」

我玩過拉鍊()和字典()一些,但我幾乎只是在這裏徘徊。相當新的python。使用Python 3.

+0

哪裏是你的代碼的其餘部分?你得到了什麼輸出? –

+0

你真的想要嵌套列表嗎?口授將更舒適的使用。 –

+0

我把它貼了一下,現在全部搞砸了。我想我基本上尋找一種更好的方式來處理二維數組,或者如果只是一些總體上更好的方法來解決這類問題。 – jason

回答

4

如果要將鍵映射到某些值,則使用字典要容易得多。特別方便建設他們是defaultdict

假設你的infile拆分上空白輸入線,下面的應該有所幫助:

from collections import defaultdict 

animals = defaultdict(list) 

for p in infile: 
    animals[p[0]].append(p[1]) 
+0

這似乎正是我所需要的,謝謝。 – jason

+1

注意,並非索引'p [0]'/'p [1]',可讀性更強的是使用解包併爲'infile中的key和value'做準備:'''[keys] .append(value)'' 。 –

2

你可能會考慮使用一組和字典。使用類別名稱作爲字典的關鍵字。因此,對於每個p in infile,animals[p[0]].add(p[1]),假設p0,p1是類型和物種。

這樣做的好處是,如果'Panther'多次​​出現爲'Cat',則不必檢查它是否已經存在於'Cat'列表中,因爲集合類型將確保您擁有一組獨特的元素。

>>> from collections import defaultdict 
>>> animals = defaultdict(set) 
>>> animals['Cat'].add('Panther') 
>>> animals 
defaultdict(<class 'set'>, {'Cat': {'Panther'}}) 
>>> animals['Cat'].add('Lion') 
>>> animals 
defaultdict(<class 'set'>, {'Cat': {'Lion', 'Panther'}}) 
>>> animals['Cat'].add('Panther') 
>>> animals 
defaultdict(<class 'set'>, {'Cat': {'Lion', 'Panther'}}) 

相比,使用列表:

>>> moreanimals = defaultdict(list) 
>>> moreanimals['Cat'].append('Panther') 
>>> moreanimals 
defaultdict(<class 'list'>, {'Cat': ['Panther']}) 
>>> moreanimals['Cat'].append('Panther') 
>>> moreanimals 
defaultdict(<class 'list'>, {'Cat': ['Panther', 'Panther']}) 
+0

+1價值元素的獨特性 – msw