我的文本文件是一个很大的数据列表(从大的意义上讲我无法手工格式化),仅由数字组成,其格式如下:

 1 5555 6666
 2 5555 6666
 1 7755 6666
 3 8888 6666


我想将前两列用作键,将剩余的第三列用作它们的值。

这是我的代码:

import string
def load (filename):
    with open ('filename', 'r'):
        dict = {}
        for line in file.readline():
            key, site, value = dict([line.strip().split('\t')for line in file
            dict[key[0]]+[site[1]]= value[2]
        return dict


但是我的代码失败。

我想要的输出是这样的:

{('1', '5555'): '6666', ('2', '5555'): '6666', ('1', '7755'): '6666', ('3', '8888'): '6666'}

是否可以实现我的输出?我在正确的轨道上吗?如果没有,我在哪里出错了,我该如何解决?

谢谢

最佳答案

您可以使用csv模块读取通过传递的任何定界符分割元素的内容,然后解压缩并将元组中的前两个元素用作键,最后一个用作值:

import csv

with open("in.csv") as f:
    d = {}
    r = csv.reader(f, delimiter=" ") # pass whatever your delimiter is
    for row in r: # first row  1 5555 6666 -> ["1", "5555", "6666"]
        a, b, v = row # a,b,c = "1", "5555", "6666"
        d[(a, b)] = v # create a tuple from the first two elements of the row
print(d)
{('3', '8888'): '6666', ('1', '5555'): '6666', ('1', '7755'): '6666', ('2', '5555'): '6666'}


如果要订购的数据,请使用OrderedDict:

import csv
from collections import OrderedDict
with open("in.csv") as f:
    d = OrderedDict()
    r = csv.reader(f, delimiter=" ")
    for row in r:
        a, b, v = row
        d[(a, b)] = v
print(d)


如果您有机会按键可以重复,那么您需要将值存储在列表或某些容器中:

import csv
from collections import OrderedDict
with open("in.csv") as f:
    d = OrderedDict()
    r = csv.reader(f, delimiter=" ")
    for row in r:
        a, b, v = row
        d.setdefault((a,b),[]).append(v)
print(d)


您自己的代码有多个错误:

def load(filename):
    with open(filename, 'r') as f: # as f and pass variable filename not a string
        d = {} # don't shadow the python dict
        for line in f: # iterate over the file object
            key, site, value = line.split() # unpack
            d[(key, site)] = value # already unpacked so just use the variables
        return d


然后调用传递文件名的函数:

print(load("in.csv"))
{('1', '5555'): '6666', ('3', '8888'): '6666', ('2', '5555'): '6666', ('1', '7755'): '66`66'}

关于python - 如何在Python中使用列表中的数字作为字典的键?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30959052/

10-11 22:03
查看更多