繁体   English   中英

将文件读入字典python

[英]reading file into dictionary python

此功能旨在将鸟名,键和权重用作值,以将文件读入字典。 它正在做我想要的,但是并没有遍历所有行,而且我不确定为什么! 帮助一个女孩吗? 这是我的代码:

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  

.txt文件是:

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

该代码旨在产生

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

我得到的是:

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

我不确定为什么

这是因为python的字典不能有重复的键。 您正在使用“大写”方法,这使某些鸟的名字相同。

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

每次看到Bluebird ,您就会覆盖那里已经存在的内容。 尝试类似:

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

为每个bird_type添加到预先存在的列表中。

Python字典中不能有多个具有相同值的键。

可以为每个实例添加一个整数,例如:

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]}

或者,使用列表列表的一种结构:

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]]}

或者,更改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]}

所以我知道已经有很多解决方案,但是我将再发布一个:)

如果您想使自己的生活更轻松一点,并且不想被代码如此轻易地弄糊涂,那么有时它可以帮助实现最短但可读性最高的解决方案。 如果此时您只有一半的时间了解自己的工作,那么在尝试更改此代码段中的某些内容时,您将在半年后遇到困难。

所以这是我相当有表现力的解决方案,我认为当您阅读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)

快乐拍打:)

编辑:对不起,忘了提及您现在应该通过此功能一个打开的文件对象(或像我所做的测试一样的字符串列表)

暂无
暂无

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

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