473,386 Members | 1,694 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,386 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 1933
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 --...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.