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 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!
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: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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
|
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...
|
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...
|
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: 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: 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...
|
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...
| |