Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
python_one_week_4_biologists_solutions
Manage
Activity
Members
Labels
Plan
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
hub-courses
python_one_week_4_biologists_solutions
Commits
c92f67f7
Commit
c92f67f7
authored
10 years ago
by
Bertrand NÉRON
Browse files
Options
Downloads
Patches
Plain Diff
add solutions for exercise on phylogenic tree
parent
7b8af965
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
source/Collection_Data_Types.rst
+35
-1
35 additions, 1 deletion
source/Collection_Data_Types.rst
with
35 additions
and
1 deletion
source/Collection_Data_Types.rst
+
35
−
1
View file @
c92f67f7
...
@@ -351,3 +351,37 @@ solution ::
...
@@ -351,3 +351,37 @@ solution ::
solution ::
solution ::
inverted_d = {v : k for k, v in d.items()}
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
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment