【问题标题】:Iterating through a list, to find item遍历列表以查找项目
【发布时间】: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


【解决方案1】:

数据.csv

name,stuff,stuff,type1,type2
bulbasaur,who knows,who knows,poison,grass
fakeasaur,who knows,who knows,poison,grass
pikachu,who knows,who knows,lightning,rat

口袋妖怪.py

#!/bin/python

the_big_d = {}

with open('data.csv', 'r') as csv_file:
    csv_file.readline()
    for line in csv_file:
        l = line[0:-1].split(',')
        if l[3] not in the_big_d:
            the_big_d[l[3]] = [l[0]]
        else:
            the_big_d[l[3]].append(l[0])
        if l[4] not in the_big_d:
            the_big_d[l[4]] = [l[0]]
        else:
            the_big_d[l[4]].append(l[0])

print(the_big_d)

输出:

{'poison': ['bulbasaur', 'fakeasaur'], 'grass': ['bulbasaur', 'fakeasaur'], 'lightning': ['pikachu'], 'rat': ['pikachu']}

【讨论】:

    【解决方案2】:

    我假设第 2 列有口袋妖怪的名称,第 6 列和第 7 列是它的类型。而且由于你不能使用 pandas,

    我不知道你到底在问什么,但这就是我认为你正在寻找的东西

    def get_pokemon_stats():
        """
        Reads a csv file and returns a Dict of pokemons grouped under their type 
        """
    
        type_list = [] 
        pokemon_by_type_dict = {}
    
    
        DATA_FILENAME = 'pokemon.csv' 
    
        with open('pokemon.csv', 'r') as csv_file:
            csv_file.readline()
    
            for line in csv_file:
    
                list_of_values = line.split(",")
    
                for i in list_of_values[6:8]:
                    if i not in type_list:
                        type_list.append(i)
                        pokemon_by_type_dict[i] = []     # Each "Type" key is a list of pokemons, change it if you want to
    
                    pokemon_by_type_dict[i].append(list_of_values[1])
    
    
        return pokemon_by_type_dict         # Returns the pokemon dict
    

    这是您要查找的代码吗?

    【讨论】:

      猜你喜欢
      • 2012-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-17
      • 1970-01-01
      相关资源
      最近更新 更多