[EMAIL PROTECTED] wrote:
d = {}
for line in [l[:-1] for l in file('test.txt', 'rU') if len(l)1]:
k,v = line.split()
d.setdefault(k,[]).append(v)
Try that with a test.txt where the last line has no newline.
Peter
--
http://mail.python.org/mailman/listinfo/python-list
limodou wrote:
On 4 Oct 2006 13:11:15 -0700, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote:
limodou wrote:
here is my program
d = {}
for line in file('test.txt'):
line = line.strip()
if line:
k, v = line.strip().split()
d.setdefault(k, []).append(v)
[EMAIL PROTECTED] wrote:
for line in (l.rstrip(\n) for l in file(test.txt, rU) if l[0] !=
\n):
k, v = line.split()
d.setdefault(k, []).append(v)
Note that this snippet will produce the same output with or without the
rstrip() method call.
Peter
--
Peter Otten wrote:
[EMAIL PROTECTED] wrote:
for line in (l.rstrip(\n) for l in file(test.txt, rU) if l[0] !=
\n):
\001\001\001k,\001v\001=\001line.split()
\001\001\001d.setdefault(k,\001[]).append(v)
Note that this snippet will produce the same output with or without the
rstrip()
- Python 2.5 introduced a dictionary type with automatic
creation of values,
ala Perl:
===
from collections import defaultdict
d = defaultdict(list)
for line in fl:
k, v = line.strip().split()
d[k].append(v)
for k,v in
-Original Message-
From:
[EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED]
rg] On Behalf Of Giovanni Bajo
Sent: 04 October 2006 15:17
To: python-list@python.org
Subject: Re: dictionary of list from a file
[EMAIL PROTECTED] wrote:
while(IN){
@info=split
On 4 Oct 2006 06:09:21 -0700, [EMAIL PROTECTED]
[EMAIL PROTECTED] wrote:
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:
You may wish to consider
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
On 4 Oct 2006 06:09:21 -0700, [EMAIL PROTECTED]
[EMAIL PROTECTED] wrote:
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
[EMAIL PROTECTED] wrote:
snip perl example code
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
On Wed, 04 Oct 2006 06:09:21 -0700, [EMAIL PROTECTED] let this
slip:
b={}
a=[]
for line in fl.readlines():
info=lines.split()
b[info[0]] = a.append(info[1])
append does not return a value. you'll want something like
d = {}
for line in fl:
key, value =
[EMAIL PROTECTED] wrote:
while(IN){
@info=split(/ +/,$_);
push (@{$tmp{$info[0]}},$info[1]);
}
and then
foreach $key (keys %tmp){
print $key - @{$tmp{$key}}\n;
}
Python 2.5 introduced a dictionary type with automatic creation of values,
ala Perl:
[EMAIL PROTECTED] wrote in message
news:[EMAIL PROTECTED]
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
limodou wrote:
here is my program
d = {}
for line in file('test.txt'):
line = line.strip()
if line:
k, v = line.strip().split()
d.setdefault(k, []).append(v)
print d
Minor nits: you call strip twice, when you don't need to. just omit
the second call.
Also, I'm
Thus spoke Paul McGuire (on 2006-10-04 17:34):
[EMAIL PROTECTED] wrote in message
this is my first post. my programming background is perlish
I'll see your perlish line noise, and raise you this obfuscapython: :)
data = \
2 1 2 3 4
7 7 8 9 10
5 1 3 5 7 9
2 6 8 10.split('\n') #
On 4 Oct 2006 13:11:15 -0700, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote:
limodou wrote:
here is my program
d = {}
for line in file('test.txt'):
line = line.strip()
if line:
k, v = line.strip().split()
d.setdefault(k, []).append(v)
print d
Minor nits:
16 matches
Mail list logo