Re: dictionaries an matrices

2020-09-16 Thread joseph pareti
you can use the following: (change the matrices as it suits your app): import numpy as np def set_params(w, b): params = {"w0": w[0], "w1": w[1] , "w2": w[2], "w3": w[3], "w4": w[4], "b": b} return params w = np.random.randn((5)) b = 1 params = set_params(w, b) for i in range(5)

Re: Dictionaries with variable default.

2014-11-04 Thread Cousin Stanley
> So How should I call this: > > class ...dict(dict): > def __init__(self, fun): > self.fun = fun > > def __missing__(self, key): > return self.fun(key) I don't know how you should, but I tried the following which seems to work class KeyPlusOne( dict ) : def __

Re: Dictionaries with variable default.

2014-11-04 Thread Antoon Pardon
Op 03-11-14 om 12:09 schreef Chris Angelico: > On Mon, Nov 3, 2014 at 10:04 PM, Antoon Pardon > wrote: >> Is it possible to have a default dictionary where the default is dependant >> on the key? >> >> I was hoping something like this might work: > m = defaultdict(lambda key: key+1) >> But it

Re: Dictionaries with variable default.

2014-11-03 Thread Chris Angelico
On Mon, Nov 3, 2014 at 10:04 PM, Antoon Pardon wrote: > Is it possible to have a default dictionary where the default is dependant > on the key? > > I was hoping something like this might work: m = defaultdict(lambda key: key+1) > > But it obviously doesn't: m[3] > > Traceback (most rece

Re: Dictionaries

2014-03-20 Thread ishish
Thanks Peter for the fast response, but the prob is solved. My colleague just verbally slapped me because a dict SHOULDN'T have duplicate keys... I change it around to duplicate values and it works fine I think I need coffee now... lots of it. -- https://mail.python.org/mailman/listinfo/pyt

Re: Dictionaries

2014-03-20 Thread John Gordon
In ishish writes: > The script [...] only creates batch1.csv. If the script only creates batch1.csv, that means Batch2 and Batch3 must be empty. > for k, v in myDict.items(): > if Batch1.has_key(k): > if k in Batch2.has_key(k): > Batch3[k] = v >

Re: Dictionaries

2014-03-20 Thread Peter Otten
ishish wrote: > This might sound weird, but is there a limit how many dictionaries a > can create/use in a single script? No. > My reason for asking is I split a 2-column-csv (phone#, ref#) file into > a dict and am trying to put duplicated phone numbers with different ref > numbers into new

Re: Dictionaries

2014-02-08 Thread worthingtonclinton
greatly appreciated guys. thanks! -- https://mail.python.org/mailman/listinfo/python-list

Re: Dictionaries

2014-02-08 Thread Frank Millman
wrote in message news:891d3696-4e4e-44cc-a491-6b8fef47f...@googlegroups.com... > why in a for loop can i access values for a dict that i did not address in > the for loop. > > example: > > a = {blah:blah} > b = {blah:blah} > > for x in a: > >print a[x] > >#here's what i don't understand

Re: Dictionaries

2014-02-08 Thread Gary Herron
On 02/08/2014 11:07 PM, worthingtonclin...@gmail.com wrote: why in a for loop can i access values for a dict that i did not address in the for loop. example: a = {blah:blah} b = {blah:blah} for x in a: print a[x] #here's what i don't understand print b[x]

Re: Dictionaries with tuples or tuples of tuples

2013-02-18 Thread Mitya Sirenef
On 02/18/2013 10:14 PM, Dave Angel wrote: On 02/18/2013 09:54 PM, Mitya Sirenef wrote: >> On 02/18/2013 09:17 PM, Jon Reyes wrote: >>> Thanks Dave and Mitya for enlightening me about dictionaries. I'm >>> still confused about this though: >> > >> > " so that if two >> > key objects are equal, t

Re: Dictionaries with tuples or tuples of tuples

2013-02-18 Thread Dave Angel
On 02/18/2013 09:54 PM, Mitya Sirenef wrote: On 02/18/2013 09:17 PM, Jon Reyes wrote: Thanks Dave and Mitya for enlightening me about dictionaries. I'm still confused about this though: > > " so that if two > key objects are equal, they stay equal, and if they differ, they stay > different

Re: Dictionaries with tuples or tuples of tuples

2013-02-18 Thread Mitya Sirenef
On 02/18/2013 09:17 PM, Jon Reyes wrote: Thanks Dave and Mitya for enlightening me about dictionaries. I'm still confused about this though: > > " so that if two > key objects are equal, they stay equal, and if they differ, they stay > different. " > > What does this mean? I won't be comparing

Re: Dictionaries with tuples or tuples of tuples

2013-02-18 Thread Jon Reyes
Oh, I see, thanks! I was thinking I'll study 2.7 and once I'm comfortable with Python as a language I'll move to 3. Heck, I don't even know how to create a simple main method. -- http://mail.python.org/mailman/listinfo/python-list

Re: Dictionaries with tuples or tuples of tuples

2013-02-18 Thread Mark Lawrence
On 19/02/2013 01:38, Jon Reyes wrote: Hi Mark. Well, doesn't iteritems() work the same? It's iteritems for Python 2, items for Python 3. -- Cheers. Mark Lawrence -- http://mail.python.org/mailman/listinfo/python-list

Re: Dictionaries with tuples or tuples of tuples

2013-02-18 Thread Jon Reyes
Thanks Dave and Mitya for enlightening me about dictionaries. I'm still confused about this though: " so that if two key objects are equal, they stay equal, and if they differ, they stay different. " What does this mean? I won't be comparing key objects with one another. Also, when I had two

Re: Dictionaries with tuples or tuples of tuples

2013-02-18 Thread Dave Angel
On 02/18/2013 08:38 PM, Jon Reyes wrote: Hi Mark. Well, doesn't iteritems() work the same? or am I missing something? By the way I'm sure I read the dictionaries part of Python but I'm unsure if it would take int's as a key for dictionaries. I've been weaned on Java where the keys of hashmaps

Re: Dictionaries with tuples or tuples of tuples

2013-02-18 Thread Mitya Sirenef
On 02/18/2013 08:38 PM, Jon Reyes wrote: Hi Mark. Well, doesn't iteritems() work the same? or am I missing something? By the way I'm sure I read the dictionaries part of Python but I'm unsure if it would take int's as a key for dictionaries. I've been weaned on Java where the keys of hashmaps

Re: Dictionaries with tuples or tuples of tuples

2013-02-18 Thread Jon Reyes
Hi Mark. Well, doesn't iteritems() work the same? or am I missing something? By the way I'm sure I read the dictionaries part of Python but I'm unsure if it would take int's as a key for dictionaries. I've been weaned on Java where the keys of hashmaps are always Strings. PS: Just checked, wow

Re: Dictionaries with tuples or tuples of tuples

2013-02-18 Thread Mark Lawrence
On 19/02/2013 00:52, Jon Reyes wrote: So I have a dictionary and the key is a number. The values are either a single tuple or a tuple of tuples. Is there a better way to go about accessing the values of the dictionary? All the tuples contain four elements. So say: col = {"1": (0,1,2,3): "2": (

Re: Dictionaries with tuples or tuples of tuples

2013-02-18 Thread Roy Smith
In article , Jon Reyes wrote: > So I have a dictionary and the key is a number. > [...] > col = {"1": (0,1,2,3): "2": ((0,1,2,3),(2,3,4,5))} The keys here are strings, not numbers. But that's a detail. Somewhat more importantly, that's a syntax error (one of the colons should be a comma).

Re: Dictionaries with tuples or tuples of tuples

2013-02-18 Thread Jon Reyes
Sorry if I didn't check the code before I posted it, I just mocked it up in Google's editor. That's what Mitya suggested too, yep, I guess I just need to make it uniform to get rid of the extra checking. Thanks man! -- http://mail.python.org/mailman/listinfo/python-list

Re: Dictionaries with tuples or tuples of tuples

2013-02-18 Thread Jon Reyes
Wow, why didn't I think of that. Thanks! I'll try it now. By the way I think I don't need to wrap the single tuples in runtime because I'm declaring that dictionary anyway beforehand and I could just do it right there. I won't be adding elements to the tuple. -- http://mail.python.org/mailman/l

Re: Dictionaries with tuples or tuples of tuples

2013-02-18 Thread Mitya Sirenef
On 02/18/2013 07:52 PM, Jon Reyes wrote: So I have a dictionary and the key is a number. The values are either a single tuple or a tuple of tuples. Is there a better way to go about accessing the values of the dictionary? All the tuples contain four elements. > > So say: > col = {"1": (0,1,2,3

Re: Dictionaries and incrementing keys

2011-06-25 Thread Raymond Hettinger
On Jun 14, 12:57 pm, Steve Crook wrote: > Today I spotted an alternative: > > dict[key] = dict.get(key, 0) + 1 > > Whilst certainly more compact, I'd be interested in views on how > pythonesque this method is. It is very pythonesque in the it was the traditional one way to do it (also one of the

Re: Dictionaries and incrementing keys

2011-06-14 Thread Asen Bozhilov
Steve Crook wrote: > Whilst certainly more compact, I'd be interested in views on how > pythonesque this method is. Instead of calling function you could use: d = {} d[key] = (key in d and d[key]) + 1 Regards. -- http://mail.python.org/mailman/listinfo/python-list

Re: Dictionaries and incrementing keys

2011-06-14 Thread Steven D'Aprano
On Tue, 14 Jun 2011 12:53:11 +, Steve Crook wrote: > On Tue, 14 Jun 2011 05:37:45 -0700 (PDT), AlienBaby wrote in Message-Id: > <078c5e9a-8fad-4d4c-b081-f69d0f575...@v11g2000prk.googlegroups.com>: > >> How do those methods compare to the one I normally use; >> >> try: >> dict[key]+=1 >> exce

Re: Dictionaries and incrementing keys

2011-06-14 Thread Steven D'Aprano
On Tue, 14 Jun 2011 10:57:44 +, Steve Crook wrote: > Hi all, > > I've always done key creation/incrementation using: > > if key in dict: > dict[key] += 1 > else: > dict[key] = 1 > > Today I spotted an alternative: > > dict[key] = dict.get(key, 0) + 1 > > Whilst certainly more comp

Re: Dictionaries and incrementing keys

2011-06-14 Thread Steve Crook
On Tue, 14 Jun 2011 13:16:47 +0200, Peter Otten wrote in Message-Id: : > Your way is usually faster than > >> dict[key] = dict.get(key, 0) + 1 Thanks Peter, ran it through Timeit and you're right. It's probably also easier to read the conditional version, even if it is longer. > You may also c

Re: Dictionaries and incrementing keys

2011-06-14 Thread Steve Crook
On Tue, 14 Jun 2011 05:37:45 -0700 (PDT), AlienBaby wrote in Message-Id: <078c5e9a-8fad-4d4c-b081-f69d0f575...@v11g2000prk.googlegroups.com>: > How do those methods compare to the one I normally use; > > try: > dict[key]+=1 > except: > dict[key]=1 This is a lot slower in percentage terms. You

Re: Dictionaries and incrementing keys

2011-06-14 Thread AlienBaby
On Jun 14, 12:16 pm, Peter Otten <__pete...@web.de> wrote: > Steve Crook wrote: > > I've always done key creation/incrementation using: > > > if key in dict: > >     dict[key] += 1 > > else: > >     dict[key] = 1 > > Your way is usually faster than > > > dict[key] = dict.get(key, 0) + 1 > > > Whils

Re: Dictionaries and incrementing keys

2011-06-14 Thread Peter Otten
Steve Crook wrote: > I've always done key creation/incrementation using: > > if key in dict: > dict[key] += 1 > else: > dict[key] = 1 Your way is usually faster than > dict[key] = dict.get(key, 0) + 1 > > Whilst certainly more compact, I'd be interested in views on how > pythonesque t

Re: Dictionaries inside out

2010-11-26 Thread Ben Finney
Ben Finney writes: > code_by_desc = dict( > (desc, code) for (code, desc) in codes_to_messages.items()) Bah, I fumbled an edit. Try this:: code_by_desc = dict( (desc, code) for (code, desc) in desc_by_code.items()) -- \“The reason we come up with new versions

Re: Dictionaries inside out

2010-11-26 Thread Ben Finney
(Replying to Greg, though his original message doesn't appear at Gmane.) > Greg Lindstrom writes: > > > I am working on a project where I'm using dictionaries to hold the > > translations to codes (i.e., {'1':'Cheddar','2':'Ice > > Hockey','IL':'Thermostat Broken'}).  The bulk of the application

Re: Dictionaries inside out

2010-11-26 Thread Terry Reedy
On 11/26/2010 1:13 PM, Greg Lindstrom wrote: I am working on a project where I'm using dictionaries to hold the translations to codes (i.e., {'1':'Cheddar','2':'Ice Hockey','IL':'Thermostat Broken'}). The bulk of the application requires me to translate codes to their meaning, but it would be ni

Re: Dictionaries inside out

2010-11-26 Thread Gaëtan Podevijn
Found in Dive in Python 3 : >>> a_dict = {'a': 1, 'b': 2, 'c': 3} >>> {value:key for key, value in a_dict.items()} {1: 'a', 2: 'b', 3: 'c'} 2010/11/26 Burton Samograd > Greg Lindstrom writes: > > > I am working on a project where I'm using dictionaries to hold the > > translations to codes (i

Re: Dictionaries inside out

2010-11-26 Thread Burton Samograd
Greg Lindstrom writes: > I am working on a project where I'm using dictionaries to hold the > translations to codes (i.e., {'1':'Cheddar','2':'Ice > Hockey','IL':'Thermostat Broken'}).  The bulk of the application > requires me to translate codes to their meaning, but it would be nice > to be abl

Re: Dictionaries and loops

2008-09-08 Thread Bruno Desthuilliers
Mike P a écrit : Thanks for the solution above, The raw data looked like User-ID,COUNTS 576460840144207854,6 576460821700280307,2 576460783848259584,1 576460809027715074,3 576460825909089607,1 576460817407934470,1 and i used CSV_INPUT1 = "C:/Example work/Attr_model/Activity_test.csv" fin1 = op

Re: Dictionaries and loops

2008-09-08 Thread Mike P
Thanks for the solution above, The raw data looked like User-ID,COUNTS 576460840144207854,6 576460821700280307,2 576460783848259584,1 576460809027715074,3 576460825909089607,1 576460817407934470,1 and i used CSV_INPUT1 = "C:/Example work/Attr_model/Activity_test.csv" fin1 = open(CSV_INPUT1, "rb"

Re: Dictionaries and loops

2008-09-08 Thread Bruno Desthuilliers
[EMAIL PROTECTED] a écrit : Bruno Desthuilliers: This doesn't look like a CSV file at all... Is that what you actually have in the file, or what you get from the csv.reader ??? I presume you are right, the file probably doesn't contain that stuff like I have assumed in my silly/useless solutio

Re: Dictionaries and loops

2008-09-08 Thread bearophileHUGS
Bruno Desthuilliers: > This doesn't look like a CSV file at all... Is that what you actually > have in the file, or what you get from the csv.reader ??? I presume you are right, the file probably doesn't contain that stuff like I have assumed in my silly/useless solutions :-) Bye, bearophile -- h

Re: Dictionaries and loops

2008-09-08 Thread bearophileHUGS
Few solutions, not much tested: data = """{None: ['User-ID', 'Count']} {None: ['576460847178667334', '1']} {None: ['576460847178632334', '8']}""" lines = iter(data.splitlines()) lines.next() identity_table = "".join(map(chr, xrange(256))) result = {} for line in lines: parts = line.translate

Re: Dictionaries and loops

2008-09-08 Thread Bruno Desthuilliers
Mike P a écrit : Hi All i have a CSV file that i'm reading in and each line has the look of the below {None: ['User-ID', 'Count']} {None: ['576460847178667334', '1']} {None: ['576460847178632334', '8']} This doesn't look like a CSV file at all... Is that what you actually have in the file, or

Re: Dictionaries and dot notation

2007-04-23 Thread Bruno Desthuilliers
Martin Drautzburg a écrit : > Daniel Nogradi wrote: > > > What if I want to create a datastructure that can be used in dot notation without having to create a class, i.e. because those objects have no behavior at all? >>> >>>A class inheriting from dict and implementing __getattr__ a

Re: Dictionaries and dot notation

2007-04-23 Thread Antoon Pardon
On 2007-04-22, Martin Drautzburg <[EMAIL PROTECTED]> wrote: > Daniel Nogradi wrote: > > >>> > What if I want to create a datastructure that can be used in dot >>> > notation without having to create a class, i.e. because those >>> > objects have no behavior at all? >>> >>> A class inheriting from d

Re: Dictionaries and dot notation

2007-04-23 Thread Gabriel Genellina
En Mon, 23 Apr 2007 03:14:32 -0300, Martin Drautzburg <[EMAIL PROTECTED]> escribió: > I did not notice that I can use a single class (or a module) for > all my datastructures, because I can "plug in" new attributes into the > instance without the class knowing about them. > > I was mistaken to b

Re: Dictionaries and dot notation

2007-04-22 Thread Martin Drautzburg
Alex Martelli wrote: > Martin Drautzburg <[EMAIL PROTECTED]> wrote: > >> > mydata = data( ) >> > mydata.foo = 'foo' >> > mydata.bar = 'bar' >> > >> > print mydata.foo >> > print mydata.bar >> >> I am aware of all this. >> Okay let me rephrase my question: is there a way of using dot >> notation

Re: Dictionaries and dot notation

2007-04-22 Thread Alex Martelli
Martin Drautzburg <[EMAIL PROTECTED]> wrote: > > mydata = data( ) > > mydata.foo = 'foo' > > mydata.bar = 'bar' > > > > print mydata.foo > > print mydata.bar > > I am aware of all this. > Okay let me rephrase my question: is there a way of using dot notation > without having to create a class?

Re: Dictionaries and dot notation

2007-04-22 Thread Ben Finney
Martin Drautzburg <[EMAIL PROTECTED]> writes: > Okay let me rephrase my question: is there a way of using dot > notation without having to create a class? Dot notation, e.g. 'foo.bar', is parsed by the interpreter as "access the attribute named 'bar' of the object 'foo'". Objects have attributes

Re: Dictionaries and dot notation

2007-04-22 Thread Martin Drautzburg
Daniel Nogradi wrote: >> > What if I want to create a datastructure that can be used in dot >> > notation without having to create a class, i.e. because those >> > objects have no behavior at all? >> >> A class inheriting from dict and implementing __getattr__ and >> __setattr__ should do the tri

Re: Dictionaries and dot notation

2007-04-22 Thread Martin Drautzburg
> mydata = data( ) > mydata.foo = 'foo' > mydata.bar = 'bar' > > print mydata.foo > print mydata.bar I am aware of all this. Okay let me rephrase my question: is there a way of using dot notation without having to create a class? -- http://mail.python.org/mailman/listinfo/python-list

Re: Dictionaries and dot notation

2007-04-22 Thread Bruno Desthuilliers
Martin Drautzburg a écrit : > This may be pretty obvious for most of you: > > When I have an object (an instance of a class "Foo") I can access > attributes via dot notation: > > aFoo.bar > > however when I have a dictionary > > aDict = {"bar":"something"} > > I have to write

Re: Dictionaries and dot notation

2007-04-22 Thread Bruno Desthuilliers
Bruno Desthuilliers a écrit : > Martin Drautzburg a écrit : > >> This may be pretty obvious for most of you: >> >> When I have an object (an instance of a class "Foo") I can access >> attributes via dot notation: >> >> aFoo.bar >> >> however when I have a dictionary >> aDict = {"ba

Re: Dictionaries and dot notation

2007-04-22 Thread Daniel Nogradi
> > This may be pretty obvious for most of you: > > > > When I have an object (an instance of a class "Foo") I can access > > attributes via dot notation: > > > > aFoo.bar > > > > however when I have a dictionary > > > > aDict = {"bar":"something"} > > > > I have to write > > > >

Re: Dictionaries and dot notation

2007-04-22 Thread Daniel Nogradi
> > This may be pretty obvious for most of you: > > > > When I have an object (an instance of a class "Foo") I can access > > attributes via dot notation: > > > > aFoo.bar > > > > however when I have a dictionary > > > > aDict = {"bar":"something"} > > > > I have to write > > > >

Re: Dictionaries and dot notation

2007-04-22 Thread Stefan Behnel
Martin Drautzburg wrote: > This may be pretty obvious for most of you: > > When I have an object (an instance of a class "Foo") I can access > attributes via dot notation: > > aFoo.bar > > however when I have a dictionary > > aDict = {"bar":"something"} > > I have to write >

Re: Dictionaries again - where do I make a mistake?

2006-10-19 Thread Tim Chase
> but I can not create Newdict to be sorted properly. > > Where do I make a mistake? By assuming that dictionaries *can* be sorted. For more reading: http://aspn.activestate.com/ASPN/Python/Cookbook/Recipe/52306 http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/107747 They're intrinsical

Re: Dictionaries again - where do I make a mistake?

2006-10-19 Thread Dustin J. Mitchell
Lad wrote: > Sorting seems to be OK,. > the command > print key,val > prints the proper values > but I can not create Newdict to be sorted properly. > > Where do I make a mistake? > Thank you for help. Dictionaries are unordered -- the order in which items come out is unspecified. It's based on

Re: Dictionaries

2006-10-19 Thread Lad
Steven, Thank you for help; Here is a code that works in a way I need A={'c':1,'d':2,'e':3,'f':2} B={'c':2,'e':1} if len(A)>=len(B): Delsi=B C = A.copy() else: Delsi=A C = B.copy() for key, value in Delsi.items(): if C.has_key(key): C[key]=

Re: Dictionaries

2006-10-18 Thread Gary Herron
Fredrik Lundh wrote: > Lad wrote: > > >> The answer: the values should be added together and assigned to the key >> That is >> {'a':1, 'b':5} >> ( from your example below) >> >> Is there a solution? >> > > have you tried coding a solution and failed, or are you just expecting > people to c

Re: Dictionaries

2006-10-18 Thread Steven D'Aprano
On Wed, 18 Oct 2006 09:31:50 -0700, Lad wrote: > > Steven, > Thank you for your reply and question. > >> >> What should the result be if both dictionaries have the same key? > The answer: the values should be added together and assigned to the key > That is > {'a':1, 'b':5} > ( from your example

Re: Dictionaries

2006-10-18 Thread Fredrik Lundh
Lad wrote: > The answer: the values should be added together and assigned to the key > That is > {'a':1, 'b':5} > ( from your example below) > > Is there a solution? have you tried coding a solution and failed, or are you just expecting people to code for free? -- http://mail.python.org/mai

Re: Dictionaries

2006-10-18 Thread Rob De Almeida
Lad wrote: > Let's suppose I have > > a={'c':1,'d':2} > b={'c':2} > but > a.update(b) > will make > {'c': 2, 'd': 2} > > and I would need > {'c': 3, 'd': 2} > > (because `c` is 1 in `a` dictionary and `c` is 2 in `b` dictionary, so > 1+2=3) > > How can be done that? dict([(k, a.get(k, 0) + b.ge

Re: Dictionaries

2006-10-18 Thread Lad
Steven, Thank you for your reply and question. > > What should the result be if both dictionaries have the same key? The answer: the values should be added together and assigned to the key That is {'a':1, 'b':5} ( from your example below) Is there a solution? Thanks for the reply L. > > a={'a':

Re: Dictionaries

2006-10-18 Thread Tim Chase
> However It does not work as I would need. > Let's suppose I have > > a={'c':1,'d':2} > b={'c':2} > but > a.update(b) > will make > {'c': 2, 'd': 2} > > and I would need > {'c': 3, 'd': 2} Ah...a previously omitted detail. There are likely a multitude of ways to do it. However, the one th

Re: Dictionaries

2006-10-18 Thread Lad
Tim Chase wrote: > > How can I add two dictionaries into one? > > E.g. > > a={'a:1} > > b={'b':2} > > > > I need > > > > the result {'a':1,'b':2}. > > >>> a.update(b) > >>> a > {'a':1,'b':2} > > -tkc Thank you ALL for help. However It does not work as I would need. Let's suppose I have a={'c'

Re: Dictionaries

2006-10-18 Thread Steven D'Aprano
On Wed, 18 Oct 2006 08:24:27 -0700, Lad wrote: > How can I add two dictionaries into one? > E.g. > a={'a:1} > b={'b':2} > > I need > > the result {'a':1,'b':2}. > > Is it possible? What should the result be if both dictionaries have the same key? a={'a':1, 'b'=2} b={'b':3} should the result

Re: Dictionaries

2006-10-18 Thread Boris Borcic
dict(a.items() + b.items()) Lad wrote: > How can I add two dictionaries into one? > E.g. > a={'a:1} > b={'b':2} > > I need > > the result {'a':1,'b':2}. > > Is it possible? -- http://mail.python.org/mailman/listinfo/python-list

Re: Dictionaries

2006-10-18 Thread Tim Chase
> How can I add two dictionaries into one? > E.g. > a={'a:1} > b={'b':2} > > I need > > the result {'a':1,'b':2}. >>> a.update(b) >>> a {'a':1,'b':2} -tkc -- http://mail.python.org/mailman/listinfo/python-list

Re: Dictionaries

2006-10-18 Thread Gary Herron
Lad wrote: > How can I add two dictionaries into one? > E.g. > a={'a:1} > b={'b':2} > > I need > > the result {'a':1,'b':2}. > > Is it possible? > > Thank you > L. > > Yes, use update. Beware that this modifies a dictionary in place rather than returning a new dictionary. >>> a={'a':1} >>> b={

Re: Dictionaries

2006-10-18 Thread Simon Brunning
On 18 Oct 2006 08:24:27 -0700, Lad <[EMAIL PROTECTED]> wrote: > How can I add two dictionaries into one? > E.g. > a={'a:1} > b={'b':2} > > I need > > the result {'a':1,'b':2}. >>> a={'a':1} >>> b={'b':2} >>> a.update(b) >>> a {'a': 1, 'b': 2} -- Cheers, Simon B [EMAIL PROTECTED] http://www.brunn

Re: dictionaries - returning a key from a value

2006-09-01 Thread bearophileHUGS
Fredrik Lundh: > better in what sense? With better I may mean faster, or needing less memory, or requiring a shorter code, or other things. It depends on many things, related to the program I am creating. Thank you for the timings, you are right, as most times. Sometimes I am wrong, but I try to

Re: dictionaries - returning a key from a value

2006-09-01 Thread Fredrik Lundh
[EMAIL PROTECTED] wrote: > keys = [key for key in sampledict if sampledict[key] == '1974'] > > Or better, given: > > sampledict = {'the Holy Grail':1975, 'Life of Brian':1979, > 'Party Political Broadcast':1974,'Mr. Neutron':1974, > 'Hamlet':1974, 'Light Entertainment War

Re: dictionaries - returning a key from a value

2006-09-01 Thread bearophileHUGS
Avell Diroll: keys = [key for key in sampledict if sampledict[key] == '1974'] Or better, given: sampledict = {'the Holy Grail':1975, 'Life of Brian':1979, 'Party Political Broadcast':1974,'Mr. Neutron':1974, 'Hamlet':1974, 'Light Entertainment War':1974} keys = [key

Re: dictionaries - returning a key from a value

2006-09-01 Thread Avell Diroll
Michael Malinowski wrote: (snip) > However, I am curious to know if its possible to get the key from giving > a value (basically the opposite of what I did above, instead of getting > a value from a key, I want the key from a value). Is there a way of > doing this? Or would I need to cycle all the

Re: dictionaries vs. objects

2006-08-31 Thread Andre Meyer
On 8/28/06, Steve Holden <[EMAIL PROTECTED]> wrote: Andre Meyer wrote:> Hi all>> I have the following question: I need a representation of a physically> inspired environment. The environment contains a large number of objects> with varying attributes. There may be classes of objects, but there is >

Re: dictionaries vs. objects

2006-08-28 Thread Steve Holden
Andre Meyer wrote: > Hi all > > I have the following question: I need a representation of a physically > inspired environment. The environment contains a large number of objects > with varying attributes. There may be classes of objects, but there is > not necessarily a strictly defined hierarc

Re: Dictionaries -- ahh the fun.. (newbie help)

2006-05-10 Thread Gerard Flanagan
rh0dium wrote: > Hi all, > > Can someone help me out. I am trying to determing for each run whether > or not the test should pass or fail but I can't seem to access the > results .. > > Alternatively can someone suggest a better structure ( and a lesson as > to the reasoning ) that would be great

Re: Dictionaries -- ahh the fun.. (newbie help)

2006-05-09 Thread Bruno Desthuilliers
rh0dium a écrit : > Hi all, > > Can someone help me out. I am trying to determing for each run whether > or not the test should pass or fail but I can't seem to access the > results .. > (snip) > cells={} > > cells["NOR3X1"]= { > 'run' : [ 'lvs', 'drc' ], >

Re: Dictionaries -- ahh the fun.. (newbie help)

2006-05-09 Thread Scott David Daniels
rh0dium wrote: > Hi all, > > Can someone help me out. I am trying to determing for each run whether > or not the test should pass or fail but I can't seem to access the > results .. > > Alternatively can someone suggest a better structure ( and a lesson as > to the reasoning ) that would be grea

Re: Dictionaries -- ahh the fun.. (newbie help)

2006-05-09 Thread BJörn Lindqvist
> Can someone help me out. I am trying to determing for each run whether > or not the test should pass or fail but I can't seem to access the > results .. > > Alternatively can someone suggest a better structure ( and a lesson as > to the reasoning ) that would be great too!! Flat is better than

Re: Dictionaries -- ahh the fun.. (newbie help)

2006-05-09 Thread James Stroud
rh0dium wrote: > Hi all, > > Can someone help me out. I am trying to determing for each run whether > or not the test should pass or fail but I can't seem to access the > results .. > > Alternatively can someone suggest a better structure ( and a lesson as > to the reasoning ) that would be grea

Re: dictionaries/pointers

2005-10-07 Thread Dave Hansen
On 7 Oct 2005 14:23:49 -0700, "Rob Conner" <[EMAIL PROTECTED]> wrote: >I dont know how to do this and can't think of a simple way to. > >All I want is a dictionary where two keys point to the same object. >(to steal the ascii art from >http://starship.python.net/crew/mwh/hacks/objectthink.html) >I

Re: dictionaries/pointers

2005-10-07 Thread Erik Max Francis
Rob Conner wrote: > I dont know how to do this and can't think of a simple way to. > > All I want is a dictionary where two keys point to the same object. > (to steal the ascii art from > http://starship.python.net/crew/mwh/hacks/objectthink.html) > I want sometihng like this: > > ,--.

Re: dictionaries and threads

2005-06-02 Thread Bloke
thanks. Looks good. -- http://mail.python.org/mailman/listinfo/python-list

Re: dictionaries and threads

2005-06-02 Thread John Abel
Bloke wrote: >I've been trying to find a Python tutorial on threading - does anyone >have a reference? > >Rob > > > You could try this: http://heather.cs.ucdavis.edu/~matloff/Python/PyThreads.pdf J -- http://mail.python.org/mailman/listinfo/python-list

Re: dictionaries and threads

2005-06-02 Thread Bloke
I've been trying to find a Python tutorial on threading - does anyone have a reference? Rob -- http://mail.python.org/mailman/listinfo/python-list

Re: dictionaries and threads

2005-06-01 Thread Tim Peters
[Gary Robinson] > I know the Global Interpreter Lock ensures that only one python thread > has access to the interpreter at a time, which prevents a lot of > situations where one thread might step on another's toes. Not really. The CPython implementation's C code relies on the GIL in many ways t

Re: dictionaries and threads

2005-06-01 Thread Paul Rubin
Gary Robinson <[EMAIL PROTECTED]> writes: > As far as I can tell, if the Python bytecodes that cause dictionary > modifications are atomic, then there should be no problem. But I don't > know that they are because I haven't looked at the bytecodes. Depending on behavior like that is asking for

Re: Dictionaries of Lists

2005-03-07 Thread Steven Bethard
Tony Meyer wrote: [a for a in list] (or a[:]) makes a copy of a list. or list(a) STeVe -- http://mail.python.org/mailman/listinfo/python-list

Re: Dictionaries of Lists

2005-03-07 Thread Terry Hancock
On Monday 07 March 2005 11:09 pm, gf gf wrote: > I'd like to associate certain lists with keywords, and > retrieve them. But this is not possible as lists are > not hashable. > > What is the best workaround? I don't mind making my > lists immutable. Is there a way to tupelize them? > I tried my

RE: Dictionaries of Lists

2005-03-07 Thread Tony Meyer
> I'd like to associate certain lists with keywords, and > retrieve them. But this is not possible as lists are > not hashable. A dictionary's values don't have to be hashable, so if the keywords are the keys in the dictionary, this would work. >>> d = {} >>> d['key1'] = [1,2,3] >>> d['key2'] =

Re: Dictionaries of Lists

2005-03-07 Thread Robert Kern
gf gf wrote: I'd like to associate certain lists with keywords, and retrieve them. But this is not possible as lists are not hashable. Do you want mydict[mylist] = mykey or mydict[mykey] = mylist ? The former is not possible with lists for the reason you noted. The latter, however, works just

Re: Dictionaries of Lists

2005-03-07 Thread Erik Max Francis
gf gf wrote: I'd like to associate certain lists with keywords, and retrieve them. But this is not possible as lists are not hashable. You can convert them to tuples with the `tuple' function: aDictionary[tuple(aList)] = aKeyword > What is the best workaround? I don't mind making my > lis