Re: [Tutor] Dict of Dict with lists

2018-04-26 Thread Neil Cerutti
On 2018-04-26, Alan Gauld via Tutor wrote: > OTOH its definitely not good OOP, ther are no methods and we > are just using the class as a record. (A named tuple might > actually be a better option on reflection.) namedtuple is great in lots of cases, but sometimes it transpires I wish to make alt

Re: [Tutor] Dict of Dict with lists

2018-04-26 Thread Alan Gauld via Tutor
On 26/04/18 14:48, Mats Wichmann wrote: >>> However personally I'd use a class to define your data structure and >>> just have a top level dictionary holding instances of the class. >> >> You are right (again). I haven't thougt of using classes, but that's exactly >> what they were invented for. T

Re: [Tutor] Dict of Dict with lists

2018-04-26 Thread Mats Wichmann
On 04/25/2018 12:46 PM, Kai Bojens wrote: > On 25/04/2018 –– 18:35:30PM +0100, Alan Gauld via Tutor wrote: >>> ... >>> for line in logfile: >>> result = pattern.search(line) > >> Doesn't this overwrite your data structure? >> I would strongly advise using another name. > > You are of

Re: [Tutor] Dict of Dict with lists

2018-04-25 Thread Kai Bojens
On 25/04/2018 –– 18:35:30PM +0100, Alan Gauld via Tutor wrote: > > ... > > for line in logfile: > > result = pattern.search(line) > Doesn't this overwrite your data structure? > I would strongly advise using another name. You are of course right. I accidentally shortened this name as

Re: [Tutor] Dict of Dict with lists

2018-04-25 Thread Alan Gauld via Tutor
On 25/04/18 14:22, Kai Bojens wrote: > The structure I'd like to have: > > result = { > 'f...@bar.de': { > 'Countries': [DE,DK,UK] > 'IP': ['192.168.1.1','172.10.10.10'] > 'Count': [12] > } > } > ... > for line in logfile: >

[Tutor] Dict of Dict with lists

2018-04-25 Thread Kai Bojens
Hello everybody, I'm coming from a Perl background and try to parse some Exim Logfiles into a data structure of dictionaries. The regex and geoip part works fine and I'd like to save the email adress, the countries (from logins) and the count of logins. The structure I'd like to have: result = {

Re: [Tutor] Dict

2013-11-26 Thread Danny Yoo
> > Is it possible to sort dict & to get result as dict? > if it is possible, please give some example. > Just to understand the question more, can you give more details? Why would you want a sorted dict? That is, it is not directly obvious what your intent is yet, so you need to say more. Are

Re: [Tutor] Dict

2013-11-26 Thread Steven D'Aprano
On Tue, Nov 26, 2013 at 04:37:28PM +0530, Sunil Tech wrote: > Hi Friends, > > Is it possible to sort dict & to get result as dict? > if it is possible, please give some example. No it is not possible, dicts are deliberately unsorted. Please read my explanation why from yesterday: https://mail.p

Re: [Tutor] Dict

2013-11-26 Thread Dominik George
Hi, > Is it possible to sort dict & to get result as dict? > if it is possible, please give some example. By what do you want to sort it? By key? By value? By a combination of both? Cheers, Nik -- Ein Jabber-Account, sie alle zu finden; ins Dunkel zu treiben und ewig zu binden; im Nat

[Tutor] Dict

2013-11-26 Thread Sunil Tech
Hi Friends, Is it possible to sort dict & to get result as dict? if it is possible, please give some example. Thank you in advance. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/li

Re: [Tutor] dict vs several variables?

2012-02-18 Thread Leam Hall
On 02/18/2012 03:39 AM, Peter Otten wrote: Leam Hall wrote: On 02/17/2012 09:26 AM, Dave Angel wrote: There are two ways to think of a class. One is to hold various related data, and the other is to do operations on that data. If you just consider the first, then you could use a class like a d

Re: [Tutor] dict vs several variables?

2012-02-18 Thread Peter Otten
Leam Hall wrote: > On 02/17/2012 09:26 AM, Dave Angel wrote: >> There are two ways to think of a class. One is to hold various related >> data, and the other is to do operations on that data. If you just >> consider the first, then you could use a class like a dictionary whose >> keys are fixed (k

Re: [Tutor] dict vs several variables?

2012-02-17 Thread Steven D'Aprano
Leam Hall wrote: I'm building a program that uses one of my own modules for a bunch of formula defs and another module for the tkinter GUI stuff. There are half a dozen input variables and about the same in calculated variables. Is it better/cleaner to just build a global dict and have everythi

Re: [Tutor] dict vs several variables?

2012-02-17 Thread Leam Hall
On 02/17/2012 09:26 AM, Dave Angel wrote: There are two ways to think of a class. One is to hold various related data, and the other is to do operations on that data. If you just consider the first, then you could use a class like a dictionary whose keys are fixed (known at "compile time"). I t

Re: [Tutor] dict vs several variables?

2012-02-17 Thread Alan Gauld
On 17/02/12 14:10, leam hall wrote: The variables input seem to be assumed to be strings They are not assumed to be strings, they *are* strings. Users can only type characters at the keyboard (the usual source of input). Your program has to interpret those characters and convert to the right

Re: [Tutor] dict vs several variables?

2012-02-17 Thread Peter Otten
leam hall wrote: > On 2/17/12, Peter Otten <__pete...@web.de> wrote: >> leam hall wrote: >>> and they may have to have their type set >> >> I have no idea what you mean by "have their type set". Can you give an >> example? > > Peter, > > The variables input seem to be assumed to be strings and I

Re: [Tutor] dict vs several variables?

2012-02-17 Thread Dave Angel
On 02/17/2012 09:06 AM, leam hall wrote: On 2/17/12, Dave Angel wrote: Real question is whether some (seldom all) of those variables are in fact part of a larger concept. If so, it makes sense to define a class for them, and pass around objects of that class. Notice it's not global, it's sti

Re: [Tutor] dict vs several variables?

2012-02-17 Thread leam hall
On 2/17/12, Peter Otten <__pete...@web.de> wrote: > leam hall wrote: >> and they may have to have their type set > > I have no idea what you mean by "have their type set". Can you give an > example? Peter, The variables input seem to be assumed to be strings and I need them to be an integer or a

Re: [Tutor] dict vs several variables?

2012-02-17 Thread leam hall
On 2/17/12, Dave Angel wrote: > Real question is whether some (seldom all) of those variables are in > fact part of a larger concept. If so, it makes sense to define a class > for them, and pass around objects of that class. Notice it's not > global, it's still passed as an argument. This can

Re: [Tutor] dict vs several variables?

2012-02-17 Thread Peter Otten
leam hall wrote: > My concern with variables is that they have to be passed in specific > order to the function, Yes, unless you use keywords. You can invoke def div(x, y): return x // y a = div(3, 2) b = div(y=3, x=2) assert a == b > and they may have to have their type set I have no ide

Re: [Tutor] dict vs several variables?

2012-02-17 Thread Dave Angel
On 2/17/12, Peter Otten<__pete...@web.de> wrote: Leam Hall wrote: I'm building a program that uses one of my own modules for a bunch of formula defs and another module for the tkinter GUI stuff. There are half a dozen input variables and about the same in calculated variables. Is it better/c

Re: [Tutor] dict vs several variables?

2012-02-17 Thread Sander Sweers
On 17 February 2012 14:04, leam hall wrote: > My concern with variables is that they have to be passed in specific > order to the function, and they may have to have their type set > multiple times so that you can perform the right functions on them. In > a dict you could set it on insert and not

Re: [Tutor] dict vs several variables?

2012-02-17 Thread leam hall
Thanks Peter! My concern with variables is that they have to be passed in specific order to the function, and they may have to have their type set multiple times so that you can perform the right functions on them. In a dict you could set it on insert and not have to worry about it. Thanks! Leam

Re: [Tutor] dict vs several variables?

2012-02-17 Thread Peter Otten
Leam Hall wrote: > I'm building a program that uses one of my own modules for a bunch of > formula defs and another module for the tkinter GUI stuff. There are > half a dozen input variables and about the same in calculated variables. > Is it better/cleaner to just build a global dict and have eve

[Tutor] dict vs several variables?

2012-02-17 Thread Leam Hall
I'm building a program that uses one of my own modules for a bunch of formula defs and another module for the tkinter GUI stuff. There are half a dozen input variables and about the same in calculated variables. Is it better/cleaner to just build a global dict and have everything go into it or

Re: [Tutor] dict['_find']

2011-02-20 Thread Max Niederhofer
Steven, Alan, Knacktus, thanks for your help. I was indeed very confused because I thought '_find' was calling something special instead of just being added to the dictionary (the confusion stemming from http://www.python.org/dev/peps/pep-0004/ where find module is obsolete). When I ran the code,

Re: [Tutor] dict['_find']

2011-02-20 Thread Knacktus
Am 20.02.2011 05:14, schrieb Max Niederhofer: Hello all, Hello Max, first post, please be gentle. I'm having serious trouble finding an alternative for the deprecated find module for dictionaries. The code (from Zed Shaw's Hard Way, exercise 40) goes something like this. Hope indentation sur

Re: [Tutor] dict['_find']

2011-02-20 Thread Alan Gauld
"Max Niederhofer" wrote first post, please be gentle. I'm having serious trouble finding an alternative for the deprecated find module for dictionaries. I think you are misunderstanding some of the terminology. There is no deprecated find module. You are not using any modules in your code. An

Re: [Tutor] dict['_find']

2011-02-19 Thread Steven D'Aprano
Max Niederhofer wrote: Hello all, first post, please be gentle. I'm having serious trouble finding an alternative for the deprecated find module for dictionaries. What find module for dictionaries? The code (from Zed Shaw's Hard Way, exercise 40) goes something like this. Hope indentation

[Tutor] dict['_find']

2011-02-19 Thread Max Niederhofer
Hello all, first post, please be gentle. I'm having serious trouble finding an alternative for the deprecated find module for dictionaries. The code (from Zed Shaw's Hard Way, exercise 40) goes something like this. Hope indentation survives. cities = {'CA': 'San Francisco', 'MI': 'Detroit', 'FL'

Re: [Tutor] Dict of function calls

2010-09-23 Thread Steven D'Aprano
On Fri, 24 Sep 2010 01:45:29 am you wrote: > > Oh, and (4)... in Python circles, it's traditional but not > > compulsory to use metasyntactic variables named after Monty Python > > sketches rather than foo and bar. So spam, ham, eggs, parrot (dead > > or otherwise), names of cheeses, aardvark..., a

Re: [Tutor] Dict of function calls

2010-09-23 Thread Pete
> Oh, and (4)... in Python circles, it's traditional but not compulsory > to use metasyntactic variables named after Monty Python sketches > rather than foo and bar. So spam, ham, eggs, parrot (dead or > otherwise), names of cheeses, aardvark..., although there is no > standard order. Hm, some

Re: [Tutor] Dict of function calls

2010-09-22 Thread Steven D'Aprano
On Thu, 23 Sep 2010 09:21:27 am Pete wrote: > > (3) There's no need to keep the instances floating around as > > independent names, unless you need direct access to them. > > > > Putting these together, we get: > > > > dispatch_table = { 'foo': foo().do, 'bar': bar().do } [...] > I see. And are th

Re: [Tutor] Dict of function calls

2010-09-22 Thread Pete
On 2010-09-22, at 5:50 PM, Steven D'Aprano wrote: > On Thu, 23 Sep 2010 06:07:21 am Pete wrote: >> For a plugin mechanism, I'm populating a dict with calls to the >> implemented plugins. > [...] >> Now I get that I need to instantiate foo and bar first before I can >> refer to them in the dict. >

Re: [Tutor] Dict of function calls

2010-09-22 Thread Steven D'Aprano
On Thu, 23 Sep 2010 06:07:21 am Pete wrote: > For a plugin mechanism, I'm populating a dict with calls to the > implemented plugins. [...] > Now I get that I need to instantiate foo and bar first before I can > refer to them in the dict. > > so something like > > foo_instance = foo() > bar_instance

[Tutor] Dict of function calls

2010-09-22 Thread Pete
For a plugin mechanism, I'm populating a dict with calls to the implemented plugins. Consider this: >>> class foo: ... def do(self): ... print 'do foo' ... >>> class bar: ... def do(self): ... print 'do bar' ... >>> list = { 'foo': foo.do(), 'bar': bar.do() } T

Re: [Tutor] dict() versus {}

2009-01-22 Thread Kent Johnson
On Wed, Jan 21, 2009 at 10:54 PM, wormwood_3 wrote: > When creating a list of dictionaries through a loop, I ran into a strange > issue. I'll let the code talk: t = [] for thing in l: > ... t.append(dict(thing=1)) > ... t > [{'thing': 1}, {'thing': 1}, {'thing': 1}, {'thing': 1

Re: [Tutor] dict() versus {}

2009-01-21 Thread Mark Tolonen
to complain that it didn't get a string or an int as it expects for a keyword argument? Maybe I am missing the use case, so far it just seems strange to force the keyword to a string. -Sam ------ From: bob ga

Re: [Tutor] dict() versus {}

2009-01-21 Thread wormwood_3
dictionary equal to {"one": 2, "two": 3}: ... dict(one=2, two=3) From: bob gailer To: wormwood_3 Cc: Python Tutorlist Sent: Wednesday, January 21, 2009 11:02:35 PM Subject: Re: [Tutor] dict() versus {} wormwood_3 wrote: When creating a li

Re: [Tutor] dict() versus {}

2009-01-21 Thread bob gailer
al to {"one": 2, "two": 3}: ... dict( two=3) From: bob gailer To: wormwood_3 Cc: Python Tutorlist Sent: Wednesday, January 21, 2009 11:02:35 PM Subject: Re: [Tutor] dict() versus {} wormwood_3 wrote: When creating a list of dictionaries thr

Re: [Tutor] dict() versus {}

2009-01-21 Thread wormwood_3
hotos - http://www.flickr.com/photos/samuelhuckins/ AIM - samushack | Gtalk - samushack | Skype - shuckins From: bob gailer To: wormwood_3 Cc: Python Tutorlist Sent: Wednesday, January 21, 2009 11:02:35 PM Subject: Re: [Tutor] dict() versus {} wormwood_3 wrote:

Re: [Tutor] dict() versus {}

2009-01-21 Thread bob gailer
wormwood_3 wrote: When creating a list of dictionaries through a loop, I ran into a strange issue. I'll let the code talk: >>> l = 'i am a special new list'.split() >>> t = [] >>> for thing in l: ... t.append({thing: 1}) ... >>> t [{'i': 1}, {'am': 1}, {'a': 1}, {'special': 1}, {'

[Tutor] dict() versus {}

2009-01-21 Thread wormwood_3
When creating a list of dictionaries through a loop, I ran into a strange issue. I'll let the code talk: >>> l = 'i am a special new list'.split() >>> t = [] >>> for thing in l: ... t.append({thing: 1}) ... >>> t [{'i': 1}, {'am': 1}, {'a': 1}, {'special': 1}, {'new': 1}, {'list': 1}] This

Re: [Tutor] Dict operation question.

2005-07-08 Thread Javier Ruere
Kent Johnson wrote: > My understanding of the OP is that he wants an exception if there are items > in errdict. Also this solution will change self.somedict even in the error > case which may not be desirable. I understood what the OP said but think he expressed himself incorrectly or the alg

Re: [Tutor] Dict operation question.

2005-07-08 Thread David Driver
Really I was just wondering if this was a bad practice or if there was some way to get the same result. So if the list comprehension isn't a bad way to to it then I will stick with it. I am at the point where I have used python for simple stuff for a few years and I am attempting to get past a n

Re: [Tutor] Dict operation question.

2005-07-08 Thread Kent Johnson
Javier Ruere wrote: > I should also add the most simple and obvious implementation which is also > the fastest: > > > class Test: > def __init__(self, dict): > self.somedict = dict > > def updateit(self, **mydict): > errdict = {} > for k, v in mydict.iteritems():

Re: [Tutor] Dict operation question.

2005-07-08 Thread Kent Johnson
David Driver wrote: > I have a function > > def updateit(self,**mydict): > > which is intended to update a dictionary self.somedict. > > If there are keys in mydict that are not in self.somedict I want them > returned in a new dict. > > here is what i am doing now: > > errdict = dict([(a,b) f

Re: [Tutor] Dict operation question.

2005-07-07 Thread Javier Ruere
I should also add the most simple and obvious implementation which is also the fastest: class Test: def __init__(self, dict): self.somedict = dict def updateit(self, **mydict): errdict = {} for k, v in mydict.iteritems(): if k not in self.somedict:

Re: [Tutor] Dict operation question.

2005-07-07 Thread Javier Ruere
David Driver wrote: > I have a function > > def updateit(self,**mydict): > > which is intended to update a dictionary self.somedict. > > If there are keys in mydict that are not in self.somedict I want them > returned in a new dict. > > here is what i am doing now: > > errdict = dict([(a,b) f

[Tutor] Dict operation question.

2005-07-07 Thread David Driver
I have a function def updateit(self,**mydict): which is intended to update a dictionary self.somedict. If there are keys in mydict that are not in self.somedict I want them returned in a new dict. here is what i am doing now: errdict = dict([(a,b) for a,b in mydict.items() if not a in self.so