What's the difference between a list and list followed by a colon in brackets? For example: listA = [1,2,3,4,5] >>> listA [1,2,3,4,5] >>> listA[:] [1,2,3,4,5] I don't understand what that [:] is doing. I'm looking at a solution for a problem I couldn't figure out and they use this notation. I tried removing the colon and it crashed my computer, so it must be important, but I don't see what it's doing.
Here's the code that crashed when I removed the colon. Specifically it's the line ans = subsets[:]... what exactly is that command doing? Why would it not be OK just to say ans = subsets? def generate_all_subsets(s): """Return a sequence containing all the substrings of the string s.""" if len(s) == 0: return [""] # Include every subset of the elements that doesn't contain s[0]... subsets = generate_all_subsets(s[1:]) print "subsets",subsets ans = subsets[:] #print "ans", ans # ...and every subset that does (these are generated by adding s[0] to # every subset of the previously generated sequence). for subset in subsets: #print "ans",ans,"s[0]",s[0],"subset",subset ans.append(s[0] + subset) return ans
please use a code pasting site
it is a slice that includes the entire list, it basically 'returns' a copy of the list - a new object http://docs.python.org/library/stdtypes.html#sequence-types-str-unicode-list-tuple-bytearray-buffer-xrange http://dpaste.com/705253/
http://henry.precheur.org/python/copy_list http://effbot.org/zone/python-list.htm http://effbot.org/pyfaq/how-do-i-copy-an-object-in-python.htm http://en.wikibooks.org/wiki/Python_Programming/Lists http://docs.python.org/library/copy.html
Thanks a bunch! Now, I'm still having trouble grokking this thing... seems really simple but at the same time I can't quite understand how this code generates all subsets of a given list. I think what makes it odd is the recursion step that happens close to the beginning of the code rather than the end, which is what most of the recursion I've seen does. Can anyone explain to me in relatively simple terms and/or pseudocode, how is this thing working? Perhaps in relation to that question, it would help to answer why is it necessary to use the copy list (ans = subsets[:]) as opposed to just setting the lists equal (and = subsets)?
First: the ans = subsets[:] is the aliasing that the professors talk about intermittently. If you set ans = subsets, then when you type either 'ans' or 'subsets', you're referring to the same list and making a change to one (such as typing 'ans[0]=15'), you're changing the other (such that subsets[0] ALSO = 15). That would cause the code to infinitely loop in the final segment of code (for subset in subsets: ans.append...etc) which is I think what you meant by "crash". Second: What the code does. The code is easier to understand when you print out subsets and ans continually. It keeps calling itself with the first letter removed until it's just the end of your string. I ran it with 'abcdef' to illustrate it for myself. It runs until it passes back 'f' and then goes to the next letter back, passing back both that letter itself ('e') and all combinations with what's already in the 'subsets' list, in this case, 'e' + 'f' = 'ef'. Then it goes to the next letter back, 'd' and repeats the process with the subsets list. At this point, the letter you're adding is 'd' and the list is ['e','f','ef'], so these combine to ['d','de','df','def','e','f','ef']. This process continues with each letter back adding to the list of substrings until you're back at the top.
Thank you!!! Repped!
Join our real-time social learning platform and learn together with your friends!