简体   繁体   English

将文件读入字典python

[英]reading file into dictionary python

This function is meant to read a file into a dictionary, using the birdnames a keys and the weights as values. 此功能旨在将鸟名,键和权重用作值,以将文件读入字典。 It is doing what I want but it isnt going through all the lines and im not sure why! 它正在做我想要的,但是并没有遍历所有行,而且我不确定为什么! Help a girl out? 帮助一个女孩吗? Here is my code: 这是我的代码:

def bird_weights(filename):
    bird_dict = {}
    f = open(filename, "r")
    for line in f:
        new_line = line.split(":")
        bird_type = new_line[0].capitalize()
        bird_weight = new_line[1].strip().split(' ')
        bw_list = [float(i) for i in bird_weight]
        bird_dict[bird_type] = bw_list
        if bird_type in bird_dict:
            bird_dict[bird_type].extend(bw_list)
        else:
            bird_dict[bird_type] = bw_list

    return bird_dict  

the .txt file is: .txt文件是:

bluebird:78.3 89.3 77.0
TANAGER: 111.9 107.65
BlueBird: 69.9
bluebirD: 91.9
tanager: 108.0 110.0

and the code is meant to produce 该代码旨在产生

{"Bluebird":[78.3, 89.3, 77.0, 69.9, 91.9],"Tanager": [111.9, 107.65, 108.0, 110.0]}

what i am getting is: 我得到的是:

{"Bluebird":[91.9, 91.9], "Tanager": [108.0, 110.0, 108.0, 110.0] }

I am not sure why 我不确定为什么

It's because python's dictionary can't have duplicate keys. 这是因为python的字典不能有重复的键。 You are using 'capitalize' method, which made some bird's names identical. 您正在使用“大写”方法,这使某些鸟的名字相同。

def bird_weights(filename):
    result = collections.defaultdict(list)

    with open(filename, 'r') as f:
        for line in f.readlines():
            bird_name, values = line.strip().split(':')

            # normalization
            bird_name = bird_name.strip().capitalize()
            values = map(lambda v: float(v.strip()), values.strip().split(' '))

            result[bird_name].extend(values)

    return result

Every time you see Bluebird , you're overwriting what was already there. 每次看到Bluebird ,您就会覆盖那里已经存在的内容。 Try something like: 尝试类似:

for line in f:
    ...
    if bird_type in bird_dict:
        bird_dict[bird_type].extend(bw_list)
    else:
        bird_dict[bird_type] = bw_list

to add to a pre-existing list for each bird_type . 为每个bird_type添加到预先存在的列表中。

You cannot have multiple keys of the same value in a Python dict. Python字典中不能有多个具有相同值的键。

You can add an integer to each instance such as: 可以为每个实例添加一个整数,例如:

keys={}
birds={}
with open(file) as f:
    for line in f:
        k,_,v=line.partition(':')
        k=k.capitalize()
        v=map(float, v.split())
        keys[k]=keys.setdefault(k, 0)+1
        birds.setdefault('{} {}'.format(k, keys[k]), []).extend(v)


{'Tanager 1': [111.9, 107.65], 
 'Tanager 2': [108.0, 110.0], 
 'Bluebird 3': [91.9], 
 'Bluebird 2': [69.9], 
 'Bluebird 1': [78.3, 89.3, 77.0]}

Or, use a list of lists kind of structure: 或者,使用列表列表的一种结构:

birds={}
with open(file) as f:
    for line in f:
        k,_,v=line.partition(':')
        k=k.capitalize()
        v=map(float, v.split())
        birds.setdefault(k, []).append(v)

{'Bluebird': [[78.3, 89.3, 77.0], [69.9], [91.9]], 
 'Tanager': [[111.9, 107.65], [108.0, 110.0]]}

Or, change append to extend for a flat list: 或者,更改appendextend为平面列表:

birds={}
with open(file) as f:
    for line in f:
        k,_,v=line.partition(':')
        k=k.capitalize()
        v=map(float, v.split())
        birds.setdefault(k, []).extend(v)

{'Bluebird': [78.3, 89.3, 77.0, 69.9, 91.9], 'Tanager': [111.9, 107.65, 108.0, 110.0]}

So i know there are already a lot of solutions to this, but i'll just post one more :) 所以我知道已经有很多解决方案,但是我将再发布一个:)

If you want to make your life a little bit easier and don't want to get confused by your code so easily, it sometimes helps to implement not the shortest but the most readable solution. 如果您想使自己的生活更轻松一点,并且不想被代码如此轻易地弄糊涂,那么有时它可以帮助实现最短但可读性最高的解决方案。 If you're at this time only half understanding what your doing you will have a hard time half a year in the future when trying to change something on this code snippet. 如果此时您只有一半的时间了解自己的工作,那么在尝试更改此代码段中的某些内容时,您将在半年后遇到困难。

So here is my fairly expressive solution and i think you'll be able to exactly understand what i did when you read through your bird_weights() function: 所以这是我相当有表现力的解决方案,我认为当您阅读bird_weights()函数时,您将能够完全理解我的所作所为:

class Bird(object):
    def __init__(self, name, weights):
        self.name = name
        self.weights = weights

    def __str__(self):
        return self.name + ':' + str(self.weights)

def get_float_list(weights):
    return [float(i.strip()) for i in weights.strip().split(' ')]

def print_birds(birdlist):
    print '{'
    for i in birdlist:
        print str(i) + ','
    print '}'

def bird_weights(f):
    birdlist = []
    for line in f:
        name, weights = line.split(':')
        birdy = Bird(name.capitalize(), get_float_list(weights))
        birdlist.append(birdy)
    print_birds(birdlist)

Happy flapping :) 快乐拍打:)

EDIT: Sorry forgot to mention that you should pass this function now a opened file object (or a list of strings as i did for testing) 编辑:对不起,忘了提及您现在应该通过此功能一个打开的文件对象(或像我所做的测试一样的字符串列表)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM