473,378 Members | 1,426 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

about dictionary

I have have the following code:
a=[3,5,8,0]
b={}

How I can i assign each item in a as the key in the dictionary b
simultaneously?
that is,
b={3:[],5:[],8:[],0:[]}
Thanks!
Nov 22 '05 #1
20 1931
b = dict([(x,dict()) for x in a])
Shi Mu wrote:
I have have the following code:
a=[3,5,8,0]
b={}

How I can i assign each item in a as the key in the dictionary b
simultaneously?
that is,
b={3:[],5:[],8:[],0:[]}
Thanks!


Nov 22 '05 #2
b = dict([(x,dict()) for x in a])
Shi Mu wrote:
I have have the following code:
a=[3,5,8,0]
b={}

How I can i assign each item in a as the key in the dictionary b
simultaneously?
that is,
b={3:[],5:[],8:[],0:[]}
Thanks!


Nov 22 '05 #3
On 20 Nov 2005 02:59:30 -0800, bo****@gmail.com <bo****@gmail.com> wrote:
b = dict([(x,dict()) for x in a])
Shi Mu wrote:
I have have the following code:
>> a=[3,5,8,0]
>> b={}
>>

How I can i assign each item in a as the key in the dictionary b
simultaneously?
that is,
b={3:[],5:[],8:[],0:[]}
Thanks!

Thanks!!!
Nov 22 '05 #4
On 20 Nov 2005 02:59:30 -0800, bo****@gmail.com <bo****@gmail.com> wrote:
b = dict([(x,dict()) for x in a])
Shi Mu wrote:
I have have the following code:
>> a=[3,5,8,0]
>> b={}
>>

How I can i assign each item in a as the key in the dictionary b
simultaneously?
that is,
b={3:[],5:[],8:[],0:[]}
Thanks!

Thanks!!!
Nov 22 '05 #5
Shi Mu <sa************@gmail.com> wrote:
I have have the following code:
a=[3,5,8,0]
b={}

How I can i assign each item in a as the key in the dictionary b
simultaneously?
that is,
b={3:[],5:[],8:[],0:[]}


Explicit:

a = [3, 5, 8, 0]
b = {}
for key in a:
b[key] = []

Quicker:

a = [3, 5, 8, 0]
b = dict( [(k, []) for k in a] )

What behaviour do you expect when there are repeated values in the
list 'a'?

--
\ "Faith may be defined briefly as an illogical belief in the |
`\ occurrence of the improbable." -- Henry L. Mencken |
_o__) |
Ben Finney
Nov 22 '05 #6
Shi Mu <sa************@gmail.com> wrote:
I have have the following code:
a=[3,5,8,0]
b={}

How I can i assign each item in a as the key in the dictionary b
simultaneously?
that is,
b={3:[],5:[],8:[],0:[]}


Explicit:

a = [3, 5, 8, 0]
b = {}
for key in a:
b[key] = []

Quicker:

a = [3, 5, 8, 0]
b = dict( [(k, []) for k in a] )

What behaviour do you expect when there are repeated values in the
list 'a'?

--
\ "Faith may be defined briefly as an illogical belief in the |
`\ occurrence of the improbable." -- Henry L. Mencken |
_o__) |
Ben Finney
Nov 22 '05 #7

Uzytkownik "Shi Mu" <sa************@gmail.com> napisal w wiadomosci
news:ma**************************************@pyth on.org...
I have have the following code:
a=[3,5,8,0]
b={}

How I can i assign each item in a as the key in the dictionary b
simultaneously?
that is,
b={3:[],5:[],8:[],0:[]}
Thanks!
Other solution:
b.fromkeys(a,[])

cheers,

Przemek
Nov 22 '05 #8

Uzytkownik "Shi Mu" <sa************@gmail.com> napisal w wiadomosci
news:ma**************************************@pyth on.org...
I have have the following code:
a=[3,5,8,0]
b={}

How I can i assign each item in a as the key in the dictionary b
simultaneously?
that is,
b={3:[],5:[],8:[],0:[]}
Thanks!
Other solution:
b.fromkeys(a,[])

cheers,

Przemek
Nov 22 '05 #9
przemek drochomirecki wrote:
Uzytkownik "Shi Mu" <sa************@gmail.com> napisal w wiadomosci
news:ma**************************************@pyth on.org...
I have have the following code:
a=[3,5,8,0]
b={}
How I can i assign each item in a as the key in the dictionary b
simultaneously?
that is,
b={3:[],5:[],8:[],0:[]}
Thanks!
Other solution:
b.fromkeys(a,[])


Be warned that all keys share the same value.
a = [3, 5, 8, 0]
b = dict.fromkeys(a, [])
b[3].append(42)
b

{8: [42], 0: [42], 3: [42], 5: [42]}

Probably not what you want...

Peter

Nov 22 '05 #10
przemek drochomirecki wrote:
Uzytkownik "Shi Mu" <sa************@gmail.com> napisal w wiadomosci
news:ma**************************************@pyth on.org...
I have have the following code:
a=[3,5,8,0]
b={}
How I can i assign each item in a as the key in the dictionary b
simultaneously?
that is,
b={3:[],5:[],8:[],0:[]}
Thanks!
Other solution:
b.fromkeys(a,[])


Be warned that all keys share the same value.
a = [3, 5, 8, 0]
b = dict.fromkeys(a, [])
b[3].append(42)
b

{8: [42], 0: [42], 3: [42], 5: [42]}

Probably not what you want...

Peter

Nov 22 '05 #11
On 11/20/05, Peter Otten <__*******@web.de> wrote:
przemek drochomirecki wrote:
Uzytkownik "Shi Mu" <sa************@gmail.com> napisal w wiadomosci
news:ma**************************************@pyth on.org...
I have have the following code:
> a=[3,5,8,0]
> b={}
>

How I can i assign each item in a as the key in the dictionary b
simultaneously?
that is,
b={3:[],5:[],8:[],0:[]}
Thanks!
Other solution:
b.fromkeys(a,[])


Be warned that all keys share the same value.
a = [3, 5, 8, 0]
b = dict.fromkeys(a, [])
b[3].append(42)
b

{8: [42], 0: [42], 3: [42], 5: [42]}

Probably not what you want...

Peter

how to do with it?
Nov 22 '05 #12
On 11/20/05, Peter Otten <__*******@web.de> wrote:
przemek drochomirecki wrote:
Uzytkownik "Shi Mu" <sa************@gmail.com> napisal w wiadomosci
news:ma**************************************@pyth on.org...
I have have the following code:
> a=[3,5,8,0]
> b={}
>

How I can i assign each item in a as the key in the dictionary b
simultaneously?
that is,
b={3:[],5:[],8:[],0:[]}
Thanks!
Other solution:
b.fromkeys(a,[])


Be warned that all keys share the same value.
a = [3, 5, 8, 0]
b = dict.fromkeys(a, [])
b[3].append(42)
b

{8: [42], 0: [42], 3: [42], 5: [42]}

Probably not what you want...

Peter

how to do with it?
Nov 22 '05 #13
Shi Mu wrote:
how to do with it?


Use Ben Finney's, not Przemek's approach if the values are mutables that you
plan to modify. If that's what you are asking.

Peter
Nov 22 '05 #14
Shi Mu wrote:
how to do with it?


Use Ben Finney's, not Przemek's approach if the values are mutables that you
plan to modify. If that's what you are asking.

Peter
Nov 22 '05 #15

Uzytkownik "Peter Otten" <__*******@web.de> napisal w wiadomosci
news:dl*************@news.t-online.com...
Shi Mu wrote:
how to do with it?
Use Ben Finney's, not Przemek's approach if the values are mutables that

you plan to modify. If that's what you are asking.

Peter


Maybe he just want to use dictionary as a set.
He can use SET container instead.

Przemek
Nov 22 '05 #16

Uzytkownik "Peter Otten" <__*******@web.de> napisal w wiadomosci
news:dl*************@news.t-online.com...
Shi Mu wrote:
how to do with it?
Use Ben Finney's, not Przemek's approach if the values are mutables that

you plan to modify. If that's what you are asking.

Peter


Maybe he just want to use dictionary as a set.
He can use SET container instead.

Przemek
Nov 22 '05 #17
On 11/20/05, przemek drochomirecki <dr*****@wytnij.hyene.com> wrote:

Uzytkownik "Peter Otten" <__*******@web.de> napisal w wiadomosci
news:dl*************@news.t-online.com...
Shi Mu wrote:
how to do with it?


Use Ben Finney's, not Przemek's approach if the values are mutables that

you
plan to modify. If that's what you are asking.

Peter


Maybe he just want to use dictionary as a set.
He can use SET container instead.

Przemek

How to use SET?
Nov 22 '05 #18
On 11/20/05, przemek drochomirecki <dr*****@wytnij.hyene.com> wrote:

Uzytkownik "Peter Otten" <__*******@web.de> napisal w wiadomosci
news:dl*************@news.t-online.com...
Shi Mu wrote:
how to do with it?


Use Ben Finney's, not Przemek's approach if the values are mutables that

you
plan to modify. If that's what you are asking.

Peter


Maybe he just want to use dictionary as a set.
He can use SET container instead.

Przemek

How to use SET?
Nov 22 '05 #19

Uzytkownik "Shi Mu" <sa************@gmail.com> napisal w wiadomosci
news:ma**************************************@pyth on.org...
On 11/20/05, przemek drochomirecki <dr*****@wytnij.hyene.com> wrote:

Uzytkownik "Peter Otten" <__*******@web.de> napisal w wiadomosci
news:dl*************@news.t-online.com...
Shi Mu wrote:
how to do with it?


Use Ben Finney's, not Przemek's approach if the values are mutables that

you
plan to modify. If that's what you are asking.

Peter


Maybe he just want to use dictionary as a set.
He can use SET container instead.

Przemek

How to use SET?
http://docs.python.org/lib/types-set.html
a [3, 5, 8, 8] b = set(a)
b set([8, 3, 5])


cheers,
przemek
Nov 22 '05 #20

Uzytkownik "Shi Mu" <sa************@gmail.com> napisal w wiadomosci
news:ma**************************************@pyth on.org...
On 11/20/05, przemek drochomirecki <dr*****@wytnij.hyene.com> wrote:

Uzytkownik "Peter Otten" <__*******@web.de> napisal w wiadomosci
news:dl*************@news.t-online.com...
Shi Mu wrote:
how to do with it?


Use Ben Finney's, not Przemek's approach if the values are mutables that

you
plan to modify. If that's what you are asking.

Peter


Maybe he just want to use dictionary as a set.
He can use SET container instead.

Przemek

How to use SET?
http://docs.python.org/lib/types-set.html
a [3, 5, 8, 8] b = set(a)
b set([8, 3, 5])


cheers,
przemek
Nov 22 '05 #21

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: John Mudd | last post by:
I must be missing something here. It's clearly faster to lookup an item directly in a dictionary than to scan through a list. So when I have a large lookup table I always load it in the form of a...
8
by: Rodd Snook | last post by:
I have an application which makes extensive use of the Scripting.Dictionary object. I'm not doing anything silly like putting them outside the page scope -- just creating quite a few of them and...
2
by: Shi Mu | last post by:
d is a dictionary. >>> d {0: , ], 1: , , ], 2: , , ], 3: , ]} for the value under each key, if the possible connection is in the dictionary, for example, under key 0. 1 and 2 were found to...
90
by: Christoph Zwerschke | last post by:
Ok, the answer is easy: For historical reasons - built-in sets exist only since Python 2.4. Anyway, I was thinking about whether it would be possible and desirable to change the old behavior in...
5
by: Andrew Robinson | last post by:
I need to serialize a dictionary so I can encode the contents. I have the following working but the size seems large. I am guessing that I am serializing the entire object not just the data. Is...
3
by: wildThought | last post by:
If I have an object that contains a generic dictionary inside of it, how do I get access to its properties such as count?
37
by: Hilton | last post by:
Hi, for (int i = 0; i < list.Count; i++) has a hidden performance hit; i.e. list.Count gets evaluated each time, so we write something like: int listCount = list.Count; for (int i = 0; i <...
6
by: GiJeet | last post by:
hello, I'm trying to use a dictionary as a class member. I want to use a property to get/set the key/value of the dictionary but I'm confused as how to use a dictionary as a property. Since there...
8
by: Bob Altman | last post by:
Hi all, I'm trying to do something that should be really easy, but I can't think of an obvious way to do it. I have a dictionary whose value is a "value type" (as opposed to a reference type --...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.