Understanding Recursion "local" variable in Python - python

I'm just learning about recursion and am trying to apply it in some for-fun, come-to-understand-it ways. (Yes, this whole thing is better done by three nested for loops)
def generate_string(current_string, still_to_place):
if still_to_place:
potential_items = still_to_place.pop(0)
for item in potential_items:
generate_string(current_string + item, still_to_place)
#print("Want to call generate_string({}, {})".format(current_string + item, still_to_place))
else:
print(current_string)
generate_string("", [['a','b','c'],['d','e','f'],['g','h','i']])
If I comment out the recursive call and uncomment the print, it prints exactly what I'd hope it would be calling. However, just uncommenting the print shows that it calls an empty still_to_place array even when it should still have the [d,e,f], [g,h,i] from the "higher up" recursion I think.
What am I missing in my understanding? Thanks!

Right, this is the expected behavior. The reason is that still_to_place is being shared between each function call. Mutable objects in Python are 'passed by assignment', meaning that, if you pass a list to a function, that function shares a reference to the SAME list. This thread has more detail.
So, each time you call still_to_place.pop(0), you are popping the list in every recursive call. They all share the exact same list.
This behavior is not always desirable, often you want your list to be immutable. In this case, you need to pass your recursive call a modified copy of the data structure. Here's what your code would look like using the immutable approach:
def generate_string(current_string, still_to_place):
if still_to_place:
potential_items = still_to_place[0]
for item in potential_items:
generate_string(current_string + item, still_to_place[1:])
print("Want to call generate_string({}, {})".format(current_string + item, still_to_place))
else:
print(current_string)
generate_string("", [['a','b','c'],['d','e','f'],['g','h','i']])
As a rule of thumb, methods on the object (e.g. .pop) will modify it in-place. Also, different languages approach mutability differently, in some language, data structures are ALWAYS immutable.

I outputted what I got during each iteration of generate_string and this is what I got. It's probably all confusing because nothing is behaving how you expected, but let me explain what Python is thinking.
#1st time
current_string = ""
still_to_place = [['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']]
We start out by passing in the above data, however, as we walk through what happens, we first pop the first array ['a', 'b', 'c'], and we begin to iterate through this popped array. However because we called .pop(0), we now only have the latter part of the array, the still_to_place.pop(0) on the first recursive call that is made to generate_string()
#2nd time
current_string = "a"
still_to_place = [['d', 'e', 'f'], ['g', 'h', 'i']]
This is exactly what was in current_string and still_to_place the first time the recursive call was made. Now we are going to begin executing the function again from the beginning. We call the pop function again, removing the second array ['d', 'e', 'f']. Now we are only left with the third and final array.
#3rd time
current_string = "ad"
still_to_place = [['g', 'h', 'i']]
As we iterate through ['g', 'h', 'i'], because still_to_place is now empty. (We have just popped the last array.) Any calls to generate_string will go directly to the else clause, and we are going to be printing out the "ad" string plus the values in the array we just popped.
#4th, 5th and 6th time
still_to_place = []
current_string = "adg"
still_to_place = []
current_string = "adh"
still_to_place = []
current_string = "adi"
We now continue where the last recursive call left off, when we where going through the second time. This is where things get confusing. When we left off current_string = "a" and still_to_place was originally [['d', 'e', 'f'], ['g', 'h', 'i']], but we have since popped everything off of the array. You see, arrays behave differently than numbers or strings. All versions of an array share the same data. You change the data once, it changes it everywhere that array is used. (objects and dictionaries also behave this way.)
So with all that said still_to_place = [], and still_to_place will stay empty for the remainder of the recursive calls. potential_items still has the data that it popped of ['d', 'e', 'f']. We've already executed the 'd' string in steps #4, #5, and #6, so we can finish where we left of
#7th and 8th times
still_to_place = []
current_string = "ae"
still_to_place = []
current_string = "af"
Once again potential_items has ['a', 'b', 'c'] and we've already executed 'a'. Unlike still_to_place, potential_items is a local variable with a smaller scope. If you know how scopes work, then it will make since why we can have multiple potential_items, but it is the same still_to_place that was being used. Each time we popped an item off of still_to_place we where adding the popped result to a new potential items variable with a limited scope. still_to_place was global to the entire program, and so one change to still_to_place would cause changes that where not being anticipated.
Hopefully I made things more confusing, and not less confusing. Leave a comment on what you need more clarification on.

Related

Rearrange a list of strings

I want to rearrange or modify he sequence of elements (strings) in a list. This is the original list
['A', 'B', 'C', 'D', 'E', 'F', 'G']
I want to move E and F behind (or after?) B.
['A', 'B', 'E', 'F', 'C', 'D', 'G']
^^^ ^^^
The decision what to move comes from the user. There is no rule behind and no way to formulate that in an algorithm. In other words the action move something behind something other is input from the user; e.g. the user mark two elements with her/his mouse and drag an drop it behind another element.
My code works and is able to do this. But I wonder if there is a more efficient and pythonic way to do this. Maybe I missed some of Python's nice in-build features.
#!/usr/bin/env python3
# input data
original = list('ABCDEFG')
# move "EF" behind "B" (this is user input)
to_move = 'EF'
behind = 'B'
# expected result
rearanged = list('ABEFCDG')
# index for insertion
idx_behind = original.index(behind)
# each element to move
for c in reversed(to_move): # "reverse!"
# remove from original position
original.remove(c)
# add to new position
original.insert(idx_behind + 1, c)
# True
print(original == rearanged)
You can assume
Elements in original are unique.
to_move always exist in original.
behind always exist in original.
The elements in to_move are always adjacent.
Other example of possible input:
Move ['B'] behind F
Move ['A', 'B'] behind C
This is not possible:
Move ['A', 'F'] behind D
Don't use .remove when the goal is to erase from a specific position; though you may know what is at that position, .remove a) will search for it again, and b) remove the first occurrence, which is not necessarily the one you had in mind.
Don't remove elements one at a time if you want to remove several consecutive elements; that's why slices exist, and why the del operator works the way that it does. Not only is it already harder to iterate when you can say what you want directly, but you have to watch out for the usual problems with modifying a list while iterating over it.
Don't add elements one at a time if you want to add several elements that will be consecutive; instead, insert them all at once by slice assignment. Same reasons apply here.
Especially don't try to interleave insertion and removal operations. That's far more complex than necessary, and could cause problems if the insertion location overlaps the source location.
Thus:
original = list('ABCDEFG')
start = original.index('E')
# grabbing two consecutive elements:
to_move = original[start:start+2]
# removing them:
del original[start:start+2]
# now figure out where to insert in that result:
insertion_point = original.index('B') + 1
# and insert:
original[insertion_point:insertion_point] = to_move
If it is just a small number of items you want to rearrange, just swap the relevant elements:
lst = ['A', 'B', 'C', 'D', 'E', 'F', 'G']
lst[2], lst[4] = lst[4], lst[2] # switch 'C' and 'E'
lst[3], lst[5] = lst[5], lst[3] # switch 'D' and 'F'
lst
['A', 'B', 'E', 'F', 'C', 'D', 'G']

python for everyone L8 exercise

could you help me to figure out smth in one exercise that relates to Lists topic (Ch 8 - https://www.py4e.com/)?
here is my code:
def delete_head(t):
del t[0]
letters = ['a', 'b', 'c', 'd', 'f', 'g']
q = delete_head(letters)
print(q)
delete_head(letters)
print(letters)
I got this output:
output
I cannot understand why output is not like ['b', 'c', 'd', 'f', 'g']
q = delete_head(letters)
This deletes 'a'
delete_head(letters)
and this deletes 'b' (since 'a' is already gone)
You are calling delete_head() twice, first in line 4 where you assign the return value of the function to q, and a second time in line 6.
Notice how q is None. This is because your function does not return the list. It get's a reference to the original list and deletes the first item. It does not return the list, but the changes aren't lost because you are using a reference to the original list. Therefore the return value of delete_head() is None

Questions about UserList __init__, ( [:], isinstance )

I wanted to extend the class list in python37 with some custom methods.
and ended up reading the UserList cpython code. After reading it new questions arose with regards of [:] usage.
If I understand correctly the `[:]` makes a slice copy of the whole
`self.data`. But I am trying to see what is the point of using `[:]`
at the left side of the `=` operator.
Is there any difference between option one and two? Tried in the python
interpreter, and both seem to have the same effect, am I missing
something?
letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
# option (1)
letters[:] = []
# option (2)
letters = []
Now it comes my questions with regards to UserList code. I added comments with questions I have.
class UserList(_collections_abc.MutableSequence):
def __init__(self, initlist=None):
self.data = []
if initlist is not None:
if type(initlist) == type(self.data):
# NOTE: Is this if statement doing the same?
# if isinstance(initlist, list):
self.data[:] = initlist
# NOTE: wouldn't in this case self.data keep a reference to initlist
# instead of a copy?
# self.data[:] = initlist[:] # could one replace that line with this one?
elif isinstance(initlist, UserList):
self.data[:] = initlist.data[:]
# NOTE: would this line accomplish the same?
# self.data = initlist.data[:]
else:
self.data = list(initlist)
...
They don't behave the same if you have another reference to letters.
Scenario 1: modifying letters in place.
>>> letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
>>> lst = letters
>>> letters[:] = []
>>> letters
>>> []
>>> lst
>>> []
Scenario 2, reassigning the name letters to an empty list.
>>> letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
>>> lst = letters
>>> letters = []
>>> letters
>>> []
>>> lst
>>> ['a', 'b', 'c', 'd', 'e', 'f', 'g']
Since names are reassigned independently, lst does not see any change.
If you had
self.data = initlist
mutations to initlist would affect self.data (since they are the same object in memory).
When you specify a on the left side of the = operator, you are using Python's normal assignment, which changes the name a in the current context to point to the new value. This does not change the previous value to which a was pointing.
By specifying a[0:2] on the left side of the = operator, you are telling Python you want to use Slice Assignment. Slice Assignment is a special syntax for lists, where you can insert, delete, or replace contents from a list.
See: How assignment works with python list slice
Maybe this helps you.

Surprising behaviour when iterating over a list while modifying it

I'm aware that this is an example of code you should never write, and I'm not asking for the right approach. My concern is that I don't understand its behaviour, so I was hoping someone could shed light on it:
def foo(list_of_chars):
'''Returns the set of words that can be obtained by removing one character from list_of_chars.'''
result = set()
my_copy = list(list_of_chars)
for elem in my_copy:
my_copy.remove(elem)
result.add(''.join(my_copy))
my_copy = list(list_of_chars)
return result
I would have expected either behaviour from this function (let's say list_of_chars is ['h', 'e', 'l', 'l', 'o']):
the code runs flawlessly because we restore my_copy at the end of each iteration, so when the for loop assigns the next element to elem it is the same as if we had never touched the list (so we do iterate over 'h', 'e', 'l', 'l', 'o');
the code fails miserably because the assignment at the end of the loop is somehow ignored, so we first remove 'h', skip 'e', remove 'l', skip the next 'l', and then remove 'o' -- and possibly crash.
What actually happens is stranger: we iterate over 'h', 'l', 'l', 'o', so 'e' is skipped, but it's the only character that is skipped. Other examples behave in the same way: only the second element of list_of_chars is overlooked. Can someone explain this? (Python 2 and 3 yield the same result).
The for loop does not "read" my_copy again on every iteration, but my_copy.remove does.
for elem in my_copy:
my_copy.remove(elem)
On the first iteration, my_copy in both lines refers to the same object. You're actually modifying the object for iterates over. However, at the end of the iteration, you replace my_copy with something else. The for loop retains its original object reference, but my_copy.remove refers to the current version of my_copy. So now the object the for loop iterates over and the object that you remove an element from are two different objects.
Hence remove interferes with the loop only on the first iteration.

Python insert operation on list

I am newbie to Python and I have a doubt regarding insert operation on the list.
Example 1:
mylist = ['a','b','c','d','e']
mylist.insert(len(mylist),'f')
print(mylist)
Output:
['a', 'b', 'c', 'd', 'e', 'f']
Example 2:
mylist = ['a','b','c','d','e']
mylist.insert(10,'f')
print(mylist)
Output:
['a', 'b', 'c', 'd', 'e', 'f']
In second example why it still inserts 'f' element in the list even if I am giving index 10 to insert method?
The list.insert function will insert before the specified index. Since the list is not that long anyways in your example, it goes on the end. Why not just use list.append if you want to put stuff on the end anyways.
x = ['a']
x.append('b')
print x
Output is
['a', 'b']
The concept here is "insert before the element with this index". To be able to insert at the end, you have to allow the invalid off-the-end index. Since there are no well-defined "off-the-end iterators" or anything in Python, it makes more sense to just allow all indices than to allow one invalid one, but no others.

Categories

Resources