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! 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!
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!
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!!!
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!!!
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
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
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
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
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
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
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?
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?
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
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
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
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
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?
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?
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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...
|
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...
|
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...
|
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...
| |
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?
|
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 <...
|
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...
|
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 --...
|
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,...
|
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,...
| |
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...
|
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,...
|
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...
|
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...
|
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...
|
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 ...
| |
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...
| |