【发布时间】:2018-11-21 03:21:34
【问题描述】:
所以我需要编写一个代码来检查一个口袋妖怪是否有一个类型,如果有,它会将该口袋妖怪名称添加到字典中,其中包含所有其他具有相同类型的口袋妖怪。这方面的所有信息都存储在一个 csv 文件中。缩进看起来也很奇怪,但它在我的实际文件中正确缩进。
import sqlite3
import csv
SEP = ','
def get_pokemon_stats():
"""Reads the data contained in a pokemon data file and parses it into
several data structures.
Args: None
Returns:
-a dict where:
-each key is a pokemon type (str). Note that type_1 and type_2
entries are all considered types. There should be no special
treatment for the type NA; it is considered a type as well.
-each value is a list of all pokemon names (strs) that fall into
the corresponding type
"""
type_list = []
poketype = []
pokewith_type = []
DATA_FILENAME = 'pokemon.csv'
with open('pokemon.csv', 'r') as csv_file:
csv_file.readline()
for line in csv_file:
list_of_values = line.strip().split(SEP)
type_list.extend(list_of_values[6:8])
for i in range(len(type_list)):
if type_list[i] not in poketype:
poketype.append(type_list[i])
poketypelist = (list_of_values[1], list_of_values[6:8])
for i in range(len(poketypelist) - 1):
if type_list[i] in poketype:
pokemon_by_type[ type_list[i]] = poketypelist[i]
我的问题:
我不知道如何让 python 识别列表中的口袋妖怪是否具有类型以及是否将其添加到字典中。
一个例子;如果bulbasaur 是毒草,那么在字典中bulbasaur 应该出现在毒药和草键旁边。
我的 CSV 文件如下所示:
它有口袋妖怪的名字,然后是一堆其他的东西,然后第三列和第四列就是这两种类型。
【问题讨论】:
-
你有什么问题?
-
那么 csv 文件是什么样的?
-
我不知道如何让 python 识别列表中的口袋妖怪是否具有类型以及是否将其添加到字典中。一个例子;如果bulbasaur 是毒草,那么在字典中bulbasaur 应该出现在毒药和草键旁边。 @SpencerWieczorek
-
它有口袋妖怪的名字,然后是一堆其他的东西,然后第三列和第四列就是这两种类型。 @滑块
-
最好把数据样本放在你的问题上,用pandas读取csv会更容易。
标签: python