Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
hub-courses
python_one_week_4_biologists_solutions
Commits
c92f67f7
Commit
c92f67f7
authored
Sep 04, 2014
by
Bertrand NÉRON
Browse files
add solutions for exercise on phylogenic tree
parent
7b8af965
Changes
1
Hide whitespace changes
Inline
Side-by-side
source/Collection_Data_Types.rst
View file @
c92f67f7
...
...
@@ -350,4 +350,38 @@ solution ::
solution ::
inverted_d = {v : k for k, v in d.items()}
\ No newline at end of file
inverted_d = {v : k for k, v in d.items()}
Exercise
--------
We assume that we have a phylogenic tree of mammals represented as nested lists. ::
mammals = ['Bovine', ['Gibbon', ['Orang Utan', ['Gorilla', ['Chimp', 'Human']]]], 'Mouse' ]
We want to work on the subtree of apes (Gibbon, Orang Utan, Gorilla, Chimp, Human)
* extract the this subtree in a new tree
* then insert 'Bonobo' at the same level of Chimp we want to obtanin something like this :[chimp, bonobo], Human]
what's append on mammals? explain the result. ::
import copy
mammals = ['Bovine', ['Gibbon', ['Orang Utan', ['Gorilla', ['Chimp', 'Human']]]], 'Mouse' ]
apes = copy.copy(mammals[1])
apes [1][1][1] = [['Chimp', 'Bonobo'], 'Human']
print mammals
['Bovine', ['Gibbon', ['Orang Utan', ['Gorilla', ['Chimp', 'Human']]]], 'Mouse' ]
what we should do to work with apes without modify mammals?
when we extract apes form mammals we did a shallow copy of mammals. tha mean we create a new list but each item in mammals
are not copy. when we modify apes we mutate an element of apes which was also referenced in mammals so mammals is modified to.
This is what we call a side effect. To avoid that we should use deepcopy from module copy.
to create apes we should write: ::
apes = copy.deepcopy(mammals[1])
deepcopy not only copy the list but make also a copy of each items of list recursively.
\ No newline at end of file
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment