Re: [Tutor] Re: Self referencing within a dictionary
Andrei wrote: Liam Clarke wrote on Sat, 2 Apr 2005 22:12:49 +1200: I know that as above doesn't work, but was just wondering if it's possible, and if it's a Bad Thing? Max has already shown it's possible. Whether it's a Bad Thing... I don't see why it would be. But I also can't imagine right now any realistic cases where it would be useful to have a dictionary contain itself as a value. It wouldn't contain itself as a value. This is basically what Liam said: >>> foo = { 'a':1, 'b':2 } >>> foo = { 'a':1, 'b':2, 'c':foo['a'] } In this case, the second line creates a _new_ dictionary, which maps 'c' to whatever the previous dictionary mapped 'a' to. The original dictionary is discarded. The result looks like this: >>> foo {'a': 1, 'c': 1, 'b': 2} And it is absolutely the same as you would get if you just did: >>> foo = { 'a': 1, 'c': 1, 'b': 2 } I don't think anyone would call that a Bad Thing.. You could also do this: >>> foo = { 'a':1, 'b':2 } >>> foo['c'] = foo['a'] Now we're not creating a new dictionary. But the code still says: "Look up the value associated with 'a' in foo, and then associate that value with 'c' in foo". And the end result is the same: >>> foo {'a': 1, 'c': 1, 'b': 2} Of course, if foo['a'] were mutable, it could cause problems. >>> foo = {'a':[]} >>> foo['b'] = foo['a'] >>> foo {'a': [], 'b': []} >>> foo['a'].append(3) >>> foo {'a': [3], 'b': [3]} But again, there are other ways of achieving this. (eg, x = []; foo = {'a':x, 'b':x}) And maybe it's even what you wanted. You could even make foo contain itself as a value. >>> foo = {'a':1, 'b':2} >>> foo['c'] = foo >>> foo['c']['a'] 1 >>> foo['c']['c']['c']['a'] 1 >>> foo['c']['c']['c']['c']['c']['c']['a'] 1 Whether this is useful, I'm not sure... But there's no magic and it's not breaking any rules. To answer what maybe the original question meant to be --- "Can I make it so that foo['c'] is the _same_ as foo['a'], and if I reassign to foo['a'], this will also change foo['c']" --- I don't think so. Not with the unmodified builtin dictionary. HTH. -- John. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] What is the best book to start? - Many Thanks!
Dear All, I would like to thank all of you that kindly, answered my question regarding good Python books to start. As I told you guys before, I am a newbie and I decided to start studying Python, that seems to be a nice programming language. That was my first post in this group, and I appreciated your hints. Well, I will follow your hints... See you later, Hoffmann __ Do you Yahoo!? Yahoo! Personals - Better first dates. More second dates. http://personals.yahoo.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to get user input as regex
Pierre Barbier de Reuille wrote at 07:22 4/2/2005: You should do something much simpler ^_^ : import re s = raw_input("regex : ") digs = re.compile(s) [...] Aha! Thanks! Dick ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Re: Self referencing within a dictionary
Liam Clarke wrote on Sat, 2 Apr 2005 22:12:49 +1200: > I know that as above doesn't work, but was just wondering if it's > possible, and if it's a > Bad Thing? Max has already shown it's possible. Whether it's a Bad Thing... I don't see why it would be. But I also can't imagine right now any realistic cases where it would be useful to have a dictionary contain itself as a value. -- Yours, Andrei = Real contact info (decode with rot13): [EMAIL PROTECTED] Fcnz-serr! Cyrnfr qb abg hfr va choyvp cbfgf. V ernq gur yvfg, fb gurer'f ab arrq gb PP. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to get user input as regex
You should do something much simpler ^_^ : import re s = raw_input("regex : ") digs = re.compile(s) [...] That should work :) pierre Dick Moores a écrit : I'm just beginning to learn regex, and am wondering how to get user input that can be used as a regex. Of course my attempt below doesn't work, but how should I modify it? I'm practicing with phone.txt, which is 513 lines, each line having a name and phone number, and some other details like an address and times. Sorry for the dumb question. import re s = raw_input("regex: ") digs = re.compile(r"s") for line in open("data//phone.txt"): if digs.search(line): print line Thanks, Dick Moores [EMAIL PROTECTED] ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor -- Pierre Barbier de Reuille INRA - UMR Cirad/Inra/Cnrs/Univ.MontpellierII AMAP Botanique et Bio-informatique de l'Architecture des Plantes TA40/PSII, Boulevard de la Lironde 34398 MONTPELLIER CEDEX 5, France tel : (33) 4 67 61 65 77fax : (33) 4 67 61 56 68 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] How to get user input as regex
I'm just beginning to learn regex, and am wondering how to get user input that can be used as a regex. Of course my attempt below doesn't work, but how should I modify it? I'm practicing with phone.txt, which is 513 lines, each line having a name and phone number, and some other details like an address and times. Sorry for the dumb question. import re s = raw_input("regex: ") digs = re.compile(r"s") for line in open("data//phone.txt"): if digs.search(line): print line Thanks, Dick Moores [EMAIL PROTECTED] ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Self referencing within a dictionary
On Apr 2, 2005, at 12:12, Liam Clarke wrote: Hi, Out of curiosity, is it possible to create a dictionary like this - foo = {'a':1, 'b':2, 'c':foo['a']} I know that as above doesn't work, but was just wondering if it's possible, and if it's a Bad Thing? Regards, Liam Clarke It doesn't work because the expression {'a':1, 'b':2, 'c':foo['a']} is evaluated before the assignment. It has to be, as Python doesn't do this kind of lazy evaluation. But when the expression is evaluated, foo doesn't exist yet, as the assignment hasn't happened yet. Therefore it fails. If foo existed before that line, it would work. Witness the following example: >>> foo = {'a':555, 'b':1} >>> foo {'a': 555, 'b': 1} >>> foo = {'a':1, 'b':2, 'c':foo['a']} >>> foo {'a': 1, 'c': 555, 'b': 2} You'd have to have a good reason to do that, though. -- Max maxnoel_fr at yahoo dot fr -- ICQ #85274019 "Look at you hacker... A pathetic creature of meat and bone, panting and sweating as you run through my corridors... How can you challenge a perfect, immortal machine?" ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Self referencing within a dictionary
Hi, Out of curiosity, is it possible to create a dictionary like this - foo = {'a':1, 'b':2, 'c':foo['a']} I know that as above doesn't work, but was just wondering if it's possible, and if it's a Bad Thing? Regards, Liam Clarke -- 'There is only one basic human right, and that is to do as you damn well please. And with it comes the only basic human duty, to take the consequences. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor