Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > save dictionary to a file without brackets.

Reply
Thread Tools

save dictionary to a file without brackets.

 
 
giuseppe.amatulli@gmail.com
Guest
Posts: n/a
 
      08-09-2012
Hi,
I have a dict() unique
like this
{(4, 5): 1, (5, 4): 1, (4, 4): 2, (2, 3): 1, (4, 3): 2}
and i want to print to a file without the brackets comas and semicolon in order to obtain something like this?
4 5 1
5 4 1
4 4 2
2 3 1
4 3 2
Any ideas?
Thanks in advance
Giuseppe
 
Reply With Quote
 
 
 
 
Roman Vashkevich
Guest
Posts: n/a
 
      08-09-2012
for key in dict:
print key[0], key[1], dict[key]

10.08.2012, Χ 0:11, http://www.velocityreviews.com/forums/(E-Mail Removed) ΞΑΠΙΣΑΜ(Α):

> Hi,
> I have a dict() unique
> like this
> {(4, 5): 1, (5, 4): 1, (4, 4): 2, (2, 3): 1, (4, 3): 2}
> and i want to print to a file without the brackets comas and semicolon in order to obtain something like this?
> 4 5 1
> 5 4 1
> 4 4 2
> 2 3 1
> 4 3 2
> Any ideas?
> Thanks in advance
> Giuseppe
> --
> http://mail.python.org/mailman/listinfo/python-list


 
Reply With Quote
 
 
 
 
Tim Chase
Guest
Posts: n/a
 
      08-09-2012
On 08/09/12 15:22, Roman Vashkevich wrote:
>> {(4, 5): 1, (5, 4): 1, (4, 4): 2, (2, 3): 1, (4, 3): 2}
>> and i want to print to a file without the brackets comas and semicolon in order to obtain something like this?
>> 4 5 1
>> 5 4 1
>> 4 4 2
>> 2 3 1
>> 4 3 2

>
> for key in dict:
> print key[0], key[1], dict[key]


This might read more cleanly with tuple unpacking:

for (edge1, edge2), cost in d.iteritems(): # or .items()
print edge1, edge2, cost

(I'm making the assumption that this is a edge/cost graph...use
appropriate names according to what they actually mean)

-tkc



 
Reply With Quote
 
Gelonida N
Guest
Posts: n/a
 
      08-09-2012
On 08/09/2012 10:11 PM, (E-Mail Removed) wrote:
> Hi,
> I have a dict() unique
> like this
> {(4, 5): 1, (5, 4): 1, (4, 4): 2, (2, 3): 1, (4, 3): 2}
> and i want to print to a file without the brackets comas and semicolon in order to obtain something like this?
> 4 5 1
> 5 4 1
> 4 4 2
> 2 3 1
> 4 3 2
> Any ideas?
> Thanks in advance
> Giuseppe
>

Boring explicit solution:

d = {(4, 5): 1, (5, 4): 1, (4, 4): 2, (2, 3): 1, (4, 3): 2}
for key, val in d.items():
v1,v2 = key
fout.write("%d %d %d\n" % (v1, v2, val))


 
Reply With Quote
 
Giuseppe Amatulli
Guest
Posts: n/a
 
      08-09-2012
thanks for the fast replies
my testing were very closed to yours but i did not know how

On 9 August 2012 15:25, Oscar Benjamin <(E-Mail Removed)> wrote:
>
> On Aug 9, 2012 9:17 PM, <(E-Mail Removed)> wrote:
>>
>> Hi,
>> I have a dict() unique
>> like this
>> {(4, 5): 1, (5, 4): 1, (4, 4): 2, (2, 3): 1, (4, 3): 2}
>> and i want to print to a file without the brackets comas and semicolon in
>> order to obtain something like this?
>> 4 5 1
>> 5 4 1
>> 4 4 2
>> 2 3 1
>> 4 3 2
>> Any ideas?
>> Thanks in advance

>
> How's this?
>
> from __future__ import print_function
>
> output = open("out.txt", "w")
>
> for (a, b), c in d.items():
> print(a, b, c, file=output)
>
> output.close()
>
> Oscar.
>> --
>> http://mail.python.org/mailman/listinfo/python-list




--
Giuseppe Amatulli
Web: www.spatial-ecology.net
 
Reply With Quote
 
Giuseppe Amatulli
Guest
Posts: n/a
 
      08-09-2012
thanks for the fast replies
my testing were very closed to yours but i did not know how to print
the the number after the semicolon!
thanks!


On 9 August 2012 15:25, Oscar Benjamin <(E-Mail Removed)> wrote:
>
> On Aug 9, 2012 9:17 PM, <(E-Mail Removed)> wrote:
>>
>> Hi,
>> I have a dict() unique
>> like this
>> {(4, 5): 1, (5, 4): 1, (4, 4): 2, (2, 3): 1, (4, 3): 2}
>> and i want to print to a file without the brackets comas and semicolon in
>> order to obtain something like this?
>> 4 5 1
>> 5 4 1
>> 4 4 2
>> 2 3 1
>> 4 3 2
>> Any ideas?
>> Thanks in advance

>
> How's this?
>
> from __future__ import print_function
>
> output = open("out.txt", "w")
>
> for (a, b), c in d.items():
> print(a, b, c, file=output)
>
> output.close()
>
> Oscar.
>> --
>> http://mail.python.org/mailman/listinfo/python-list




--
Giuseppe Amatulli
Web: www.spatial-ecology.net
 
Reply With Quote
 
Roman Vashkevich
Guest
Posts: n/a
 
      08-09-2012
dict.items() is a list - linear access time whereas with 'for key in dict:' access time is constant: http://python.net/~goodger/projects/...ere-possible-1

10.08.2012, Χ 0:35, Tim Chase ΞΑΠΙΣΑΜ(Α):

> On 08/09/12 15:22, Roman Vashkevich wrote:
>>> {(4, 5): 1, (5, 4): 1, (4, 4): 2, (2, 3): 1, (4, 3): 2}
>>> and i want to print to a file without the brackets comas and semicolon in order to obtain something like this?
>>> 4 5 1
>>> 5 4 1
>>> 4 4 2
>>> 2 3 1
>>> 4 3 2

>>
>> for key in dict:
>> print key[0], key[1], dict[key]

>
> This might read more cleanly with tuple unpacking:
>
> for (edge1, edge2), cost in d.iteritems(): # or .items()
> print edge1, edge2, cost
>
> (I'm making the assumption that this is a edge/cost graph...use
> appropriate names according to what they actually mean)
>
> -tkc
>
>
>


 
Reply With Quote
 
Mark Lawrence
Guest
Posts: n/a
 
      08-09-2012
On 09/08/2012 21:41, Roman Vashkevich wrote:
> dict.items() is a list - linear access time whereas with 'for key in dict:' access time is constant: http://python.net/~goodger/projects/...ere-possible-1
>
> 10.08.2012, Χ 0:35, Tim Chase ΞΑΠΙΣΑΜ(Α):
>
>> On 08/09/12 15:22, Roman Vashkevich wrote:
>>>> {(4, 5): 1, (5, 4): 1, (4, 4): 2, (2, 3): 1, (4, 3): 2}
>>>> and i want to print to a file without the brackets comas and semicolon in order to obtain something like this?
>>>> 4 5 1
>>>> 5 4 1
>>>> 4 4 2
>>>> 2 3 1
>>>> 4 3 2
>>>
>>> for key in dict:
>>> print key[0], key[1], dict[key]

>>
>> This might read more cleanly with tuple unpacking:
>>
>> for (edge1, edge2), cost in d.iteritems(): # or .items()
>> print edge1, edge2, cost
>>
>> (I'm making the assumption that this is a edge/cost graph...use
>> appropriate names according to what they actually mean)
>>
>> -tkc
>>
>>
>>

>


I'm impressed, the OP gives a dict with five entries and already we're
optimising, a cunning plan if ever there was one. Hum, I think I'll
start on the blast proof ferro-concrete bunker tonight just in case
WWIII starts tomorrow.

--
Cheers.

Mark Lawrence.

 
Reply With Quote
 
Tim Chase
Guest
Posts: n/a
 
      08-09-2012
On 08/09/12 15:41, Roman Vashkevich wrote:
> 10.08.2012, Χ 0:35, Tim Chase ΞΑΠΙΣΑΜ(Α):
>> On 08/09/12 15:22, Roman Vashkevich wrote:
>>>> {(4, 5): 1, (5, 4): 1, (4, 4): 2, (2, 3): 1, (4, 3): 2}
>>>> and i want to print to a file without the brackets comas and semicolon in order to obtain something like this?
>>>> 4 5 1
>>>> 5 4 1
>>>> 4 4 2
>>>> 2 3 1
>>>> 4 3 2
>>>
>>> for key in dict:
>>> print key[0], key[1], dict[key]

>>
>> This might read more cleanly with tuple unpacking:
>>
>> for (edge1, edge2), cost in d.iteritems(): # or .items()
>> print edge1, edge2, cost
>>
>> (I'm making the assumption that this is a edge/cost graph...use
>> appropriate names according to what they actually mean)

>
> dict.items() is a list - linear access time whereas with 'for
> key in dict:' access time is constant:
> http://python.net/~goodger/projects/...ere-possible-1


That link doesn't actually discuss dict.{iter}items()

Both are O(N) because you have to touch each item in the dict--you
can't iterate over N entries in less than O(N) time. For small
data-sets, building the list and then iterating over it may be
faster faster; for larger data-sets, the cost of building the list
overshadows the (minor) overhead of a generator. Either way, the
iterate-and-fetch-the-associated-value of .items() & .iteritems()
can (should?) be optimized in Python's internals to the point I
wouldn't think twice about using the more readable version.

-tkc


 
Reply With Quote
 
Roman Vashkevich
Guest
Posts: n/a
 
      08-09-2012
Actually, they are different.
Put a dict.{iter}items() in an O(k^N) algorithm and make it a hundred thousand entries, and you will feel the difference.
Dict uses hashing to get a value from the dict and this is why it's O(1).

10.08.2012, Χ 1:21, Tim Chase ΞΑΠΙΣΑΜ(Α):

> On 08/09/12 15:41, Roman Vashkevich wrote:
>> 10.08.2012, Χ 0:35, Tim Chase ΞΑΠΙΣΑΜ(Α):
>>> On 08/09/12 15:22, Roman Vashkevich wrote:
>>>>> {(4, 5): 1, (5, 4): 1, (4, 4): 2, (2, 3): 1, (4, 3): 2}
>>>>> and i want to print to a file without the brackets comas and semicolon in order to obtain something like this?
>>>>> 4 5 1
>>>>> 5 4 1
>>>>> 4 4 2
>>>>> 2 3 1
>>>>> 4 3 2
>>>>
>>>> for key in dict:
>>>> print key[0], key[1], dict[key]
>>>
>>> This might read more cleanly with tuple unpacking:
>>>
>>> for (edge1, edge2), cost in d.iteritems(): # or .items()
>>> print edge1, edge2, cost
>>>
>>> (I'm making the assumption that this is a edge/cost graph...use
>>> appropriate names according to what they actually mean)

>>
>> dict.items() is a list - linear access time whereas with 'for
>> key in dict:' access time is constant:
>> http://python.net/~goodger/projects/...ere-possible-1

>
> That link doesn't actually discuss dict.{iter}items()
>
> Both are O(N) because you have to touch each item in the dict--you
> can't iterate over N entries in less than O(N) time. For small
> data-sets, building the list and then iterating over it may be
> faster faster; for larger data-sets, the cost of building the list
> overshadows the (minor) overhead of a generator. Either way, the
> iterate-and-fetch-the-associated-value of .items() & .iteritems()
> can (should?) be optimized in Python's internals to the point I
> wouldn't think twice about using the more readable version.
>
> -tkc
>
>


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Performance ordered dictionary vs normal dictionary Navkirat Singh Python 6 07-29-2010 10:18 AM
creating a dictionary from a dictionary with regex james_027 Python 1 08-22-2007 07:39 AM
how to automatically "Save " a page after certain intervals without clicking "Save Page As..." subhadip Java 0 03-28-2007 04:15 PM
[DICTIONARY] - Copy dictionary entries to attributes Ilias Lazaridis Python 6 02-21-2006 11:27 AM
save a dictionary in a file Luis P. Mendes Python 10 11-16-2004 09:40 AM



Advertisments