我正试图找到一种方法,使用Python脚本从列表中的多个项目中创建字典。有问题的列表看起来像这样,仅举几例:
['331416', 'Macromedaeus', 'distinguendus', '|', '|', 'scientific', 'name','|']
['331417', 'Physalopteroidea', '|', '|', 'scientific', 'name', '|']
['331418', 'Dracunculus', 'insignis', '|', '|', 'scientific', 'name', '|']
['331419', 'Bejaria', 'sprucei', '|', '|', 'scientific', 'name', '|']
['331420', 'Paecilomyces', 'sp.', 'JCM', '12545', '|', '|', 'scientific', 'name', '|']
这是我找麻烦的地方,因为我不知道如何去做这件事。第一项是一个ID,第二项是有机体属名,有时有一个物种名称作为第三项给出,有时没有,如第二个清单的情况。我需要使用ID号作为关键字以及有机体属和物种名称(如果给出)作为值来创建字典。
我该怎么做呢?我目前正在使用Python。 2.7.8。
答案 0 :(得分:1)
input = [
['331416', 'Macromedaeus', 'distinguendus', '|', '|', 'scientific', 'name','|'],
['331417', 'Physalopteroidea', '|', '|', 'scientific', 'name', '|'],
['331418', 'Dracunculus', 'insignis', '|', '|', 'scientific', 'name', '|'],
['331419', 'Bejaria', 'sprucei', '|', '|', 'scientific', 'name', '|'],
['331420', 'Paecilomyces', 'sp.', 'JCM', '12545', '|', '|', 'scientific', 'name', '|']
]
taxonomy = {}
for r in input:
taxonomy[r[0]] = {}
taxonomy[r[0]]['genus'] = r[1]
if r[2] != '|':
taxonomy[r[0]]['specie'] = " ".join(r[2:r.index("|")])
在taxonomy
{
'331418': {'genus': 'Dracunculus', 'specie': 'insignis'},
'331419': {'genus': 'Bejaria', 'specie': 'sprucei'},
'331420': {'genus': 'Paecilomyces', 'specie': 'sp. JCM 12545'},
'331416': {'genus': 'Macromedaeus', 'specie': 'distinguendus'},
'331417': {'genus': 'Physalopteroidea'}
}
答案 1 :(得分:1)
if you want a dictionary with keys to be ID#s and values to be a simple list (instead of a dictionary) use a defaultdict, which allows you to have list values.
import re # import regular expressions
from collections import defaultdict # use default dictionary
# your lists
combinedlist = [
['331416', 'Macromedaeus', 'distinguendus', '|', '|', 'scientific', 'name','|'],
['331417', 'Physalopteroidea', '|', '|', 'scientific', 'name', '|'],
['331418', 'Dracunculus', 'insignis', '|', '|', 'scientific', 'name', '|'],
['331419', 'Bejaria', 'sprucei', '|', '|', 'scientific', 'name', '|'],
['331420', 'Paecilomyces', 'sp.', 'JCM', '12545', '|', '|', 'scientific', 'name', '|']
]
# make a regular expression pattern for an id number that is exactly 6 digits
# this is flexible-- if you wanted an id number between 4 and 6 digits, use \d{4,6}
id_num = re.compile("\d{6}")
# your default dictionary which has lists as values
d = defaultdict(list)
# iterate through your combined list
for list in combinedlist:
n = len(list)
new_entry = []
# for all the entries of each list
for i in range(1, n):
new_entry.append(list[i])
d[list[0]] = new_entry
# print
for key in d.keys():
print 'key: ',key, '\n value:',d[key]
here's output
key: 331418
value: ['Dracunculus', 'insignis', '|', '|', 'scientific', 'name', '|']
key: 331419
value: ['Bejaria', 'sprucei', '|', '|', 'scientific', 'name', '|']
key: 331420
value: ['Paecilomyces', 'sp.', 'JCM', '12545', '|', '|', 'scientific', 'name', '|']
key: 331416
value: ['Macromedaeus', 'distinguendus', '|', '|', 'scientific', 'name', '|']
key: 331417
value: ['Physalopteroidea', '|', '|', 'scientific', 'name', '|']
答案 2 :(得分:-1)
鉴于其中一个列表的行为如下:
<ion-view view-title="cards">
<ion-content ng-controller="Cards">
<h1> Cards view </h1>
<ion-list>
<ion-item ng-repeat="cards in cards">
{{card.title}}
</ion-item>
</ion-list>
</ion-content>
</ion-view>
将ID作为键,并使列表的其余部分成为值。
然后,根据这些列表的存储位置,您可以循环并将所有列表添加到字典中。