[Tutor] List methods inside a dictionary of list
Hello, I am playing lists and dictionaries and I came across this counter-intuitive result. >>> d = dict(zip(['a', 'q', 'c', 'b', 'e', 'd', 'g', 'j'],8*[[0]])) >>>d Out: {'a': [0], 'b': [0], 'c': [0], 'd': [0], 'e': [0], 'g': [0], 'j': [0], 'q': [0]} >>> d['a'].__setitem__(0,4) >>> d Out: {'a': [4], 'b': [4], 'c': [4], 'd': [4], 'e': [4], 'g': [4], 'j': [4], 'q': [4]} I was not expecting all the keys to be updated. Is there any documentation I could read on how different datatypes' methods and operators interact differently when inside a dictionary? I would also like to find a way of being able to use list methods in side a dictionary so that >>> d['a'][0] = 5 Does not return {'a': [5], 'b': [5], 'c': [5], 'd': [5], 'e': [5], 'g': [5], 'j': [5], 'q': [5]} But rather {'a': [5], 'b': [0], 'c': [0], 'd': [0], 'e': [0], 'g': [0], 'j': [0], 'q': [0]} Where d is made by d = dict(zip(['a', 'q', 'c', 'b', 'e', 'd', 'g', 'j'],8*[[0]])) Thanks a bunch, Rafael ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] List methods inside a dictionary of list
I did not understand the behavior of array multiplication. In fact, I just now learned what it was called thanks to your email. Best wishes, Rafael On Mon, Jul 11, 2011 at 9:33 AM, Brett Ritter wrote: > On Mon, Jul 11, 2011 at 9:26 AM, Rafael Turner > wrote: >> I am playing lists and dictionaries and I came across this >> counter-intuitive result. >> >>>>> d = dict(zip(['a', 'q', 'c', 'b', 'e', 'd', 'g', 'j'],8*[[0]])) > ... >>>>> d['a'].__setitem__(0,4) > ... >> >> I was not expecting all the keys to be updated. Is there any >> documentation I could read on how different datatypes' methods and >> operators interact differently when inside a dictionary? I would also >> like to find a way of being able to use list methods in side a >> dictionary so that > > As has been mentioned, this isn't the dictionary doing anything weird, > this is that "8*[[0]]" gives you a list of 8 references to the same > list. You can play with just that part and see that that's the source > of your issue. > > To achieve what you are trying, try this instead: > > d = dict([(x,[0]) for x in ['a', 'q', 'c', 'b', 'e', 'd', 'g', 'j']]) > > Can you understand how this behaves differently than 8*[[0]] ? Check > the Python docs for array multiplication if you're confused, but the > basic idea is that "[0]" isn't getting evaluated freshly for every > piece in the array for 8*[[0]], but in a list comprehension it is. > -- > Brett Ritter / SwiftOne > swift...@swiftone.org > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Writing the Map function as a oneliner
Hello, I am trying to write the map function as a oneliner. I currently have implement map as a list comprehension: map = lambda F,li: [F(x) for x in li] But I would like to make recursive version. Here is what I was thinking: I can write map as def pyMap(F,li): if li == []: return [] else: return [F(li[0])] + map2(F, li[1:]) I can logical encode an if-then-else structure as follows: Let Q be the result of the following IF,THEN, ELSE conditional structure: If(S): Then: A Else: B We can implement this only using logical constructors. Q = (A AND S) OR (B AND NOT S) where: S is the input for the If condition, A is the input to Then subexpression B is the input for the Else subexpression Q is the output of the entire If-Then-Else expression. So, I tried: def pyMap2(F, li): def ifFun(P,eq): return li == [] return ([] and ifFun(li,[])) or ([F(li[0])] + pyMap2(F, li[1:]) and not []) But it's no use. pyMap works fine but pyMap2 does not. I saved the above code as map.py and ran it in the interpreter. >>> import map >>> map.pyMap2(lambda x: x + 2, [1,2,3]) Traceback (most recent call last): File "", line 1, in File "map.py", line 15, in pyMap2 return ([] and fIF(li,[])) or ([F(li[0])] + pyMap2(F, li[1:]) and not []) File "map.py", line 15, in pyMap2 return ([] and fIF(li,[])) or ([F(li[0])] + pyMap2(F, li[1:]) and not []) File "map.py", line 15, in pyMap2 return ([] and fIF(li,[])) or ([F(li[0])] + pyMap2(F, li[1:]) and not []) File "map.py", line 15, in pyMap2 return ([] and fIF(li,[])) or ([F(li[0])] + pyMap2(F, li[1:]) and not []) IndexError: list index out of range Thank you for your help, Rafael ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor