Please enable JavaScript.
Coggle requires JavaScript to display documents.
Python3 Lecture06 (extract dict (we cannot get the single value like list…
Python3 Lecture06
extract dict
we cannot get the single value like list using index, we need to use key to get the value CANNOT GET VALUE THROUGH KEY.
set() can get key, but it can also change the type from dict to set
can also get key though this e.g.enzymes.keys()
get value e.g. enzymes.values()
get pairs or items e.g. enzymes.items()
get the specific value. enzymes.get("'") cannot get the key through this kind of way
other three ways cannot get the specific value or key but we can list(enzymes.item()) make the dict to list, then we can get specific key or value through the normal way
The get() method lets us specify a default for when the key isn't found, this case, when you cannot find the specific key, the output is 0
nonzero_counts.get('GA',0)
creating a dict
firstly, we need to start and end with curly brackey {}
-
-
we write dict one element one line, it is easy to follow
warning
foo = list(enzymes) foo.sort() foo
then you will get the right sorted enzymes.
but foo= list(enzymes).sort()
the foo will be a NoneType.
enzymes[ ]= inside of the bracket and right end of the equal, integer will be no quotes and string will have quotation mark
actually, the dict has no order if we want it to have order like enter. we need to "from collections import OrderedDict" then enzymes.OrderedDict(), this time, it is not curly bracket only bracket
-
loop
loop with dict
can make it become list, e.g. all_count.keys() make the key into list
loop with pairs
for dinuc,count in all_count.items()
Lookup vs. iteration
if you just want to find the a single value, you can only use find
-
build a dict
like a list, firstly we need have a empty dict, and add one element one time
enzymes = {}
enzymes['EcoRI'] = 'GAATTC'
enzymes['AvaII'] = 'GGACC'