Skip to main content
 首页 » 编程设计

python之如何使用列表中的数字作为 Python 字典的键

2024年10月24日14lhb25

我的文本文件是一个很大的数据列表(大的意思是我不能手动格式化它)仅由数字组成,格式如下:

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