文件列表的字典 [英] dictionary of list from a file

查看:79
本文介绍了文件列表的字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

这是我的第一篇文章。我的编程背景是perlish脚本

现在我正在学习python。我需要从文件中创建一个列表

的字典。通常在Perl我用来做:


while(< IN>){

@ info = split(/ + /,$ _);

push(@ {$ tmp {$ info [0]}},$ info [1]);

}


然后

foreach $ key(键%tmp){

print" $ key - @ {$ tmp {$ key}} \ n" ;;

}

i获得


2 -1 2 3 4

7 -7 8 9 10


在python中我试过:

b = {}

a = []

for line in fl。 readlines():

info = lines.split()

b [info [0]] = a.append(info [1])

>
然后

for b in b:

print i,b [i]

i get

2无

无无


数据文件是:

2 1

2 2

2 3

2 4

7 7

7 8

7 9

7 10


任何帮助??

提前致谢

最好的问候

Andrea

Hi guys,
this is my first post. my "programming" background is perlish scripting
and now I am learning python. I need to create a dictionary of list
from a file. Normally in perl I use to do like:

while(<IN>){
@info=split(/ +/,$_);
push (@{$tmp{$info[0]}},$info[1]);
}

and then
foreach $key (keys %tmp){
print "$key -@{$tmp{$key}}\n";
}
i get

2 -1 2 3 4
7 -7 8 9 10

in python I tried:
b={}
a=[]
for line in fl.readlines():
info=lines.split()
b[info[0]] = a.append(info[1])

and then
for i in b:
print i,b[i]
i get
2 None
7 None

data file is:
2 1
2 2
2 3
2 4
7 7
7 8
7 9
7 10

Any help??
Thanks in advance
Best Regards

Andrea

推荐答案

_ );

push(@ {
_);
push (@{


tmp {


info [0]}},
info[0]}},


这篇关于文件列表的字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆