473,473 Members | 1,713 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 1934
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.