"Unlike sequences, which are indexed by a range of numbers, dictionaries are indexed by keys, which can be any immutable type; strings and numbers can always be keys. Tuples can be used as keys if they contain only strings, numbers, or tuples; if a tuple contains any mutable object either directly or indirectly, it cannot be used as a key. "
Think dict as an unordered set of [key:value] pairs, with a requirement that the key is unique.
1. How to generate a dictionary?
1.1 Direct way
a = {} --- empty dictionary
1.2 Generator
a = {x:x**2 for x in range(10)}
1.3 Convert
a = [[1,2], [2,3]]; b = dict(a);
2. Operators (what is in the dict?)
2.1 a.keys() --- return all keys as a list
2.2 a.values() --- return all values as a list, including duplicates
2.3 a.has_key(x) --- return True/False, based on whether dict has the key
2.4 a.pop(x) --- return the value for key x, and then remove the key:value list
2.5 del a[x] --- directly delete the element from dict a
2.6 a.update(b) --- add the entries in dict b into a
Now seems like the basic data structures in "raw" python is discussed. It is very interesting that the Python language fully utilized the keyboard: () as tuple, [] as list, and {} as dict. How interesting it is?
Btw, if one want to do some looping, then one can use enumerate() to generate a enumerate type for looping:
for i,j in enumerate(['this', 'is', 'a', 'good', 'day']:
print i,j
while the generated enumerate type could be directly translated into list/tuple/dict by using list()/tuple()/dict() functions.
Saturday, March 8, 2014
Python Study Note: Data Structure - Set
"A set is an unordered collection with no duplicate elements. Basic uses include membership testing and eliminating duplicate entries. Set objects also support mathematical operations like union, intersection, difference, and symmetric difference."
This is pretty much about a "set". Set is a simple object, it could be very helpful to used as a "check-existence" or "eliminate duplicates" tool.
1. Generate set
1.1 From List or tuple
a = [1,2,3]; b = (4,5,6); c = set(a); c = set(b)
Here there is one interesting fact, the elements in a set should be "hashable"(which means "An object is hashable if it has a hash value which never changes during its lifetime"). Since a "list"'s value could be changed, it may be only a pointer, so one cannot include a "list" as an entry in a set; but one can include a "tuple" as an element of a set.
2. Usage
2.1 "check-existence"
a = range(10); b = set(a);
10 in c
2.2 "eliminate duplicates"
a = [1,2,3,1,2,5,2,10]; b = set(a); c = list(b);
3. Evaluation
3.1 len(s); x in s; x not in s; --- whether element inside set
3.2 s.issubset(t); s.issuperset(t) --- whether set s is sub/super set of t
3.3 s.union(t) --- equivalent to s | t
3.4 s.intersection(t) --- equivalent to s & t
3.5 s.difference(t) --- equivalent to s - t
3.6 s.symmetric_difference(t) --- new set with elements in either s or t but not both
3.7 s.copy() --- new set with a shadow copy of s
4. Operator
4.1 s.update(t) --- return set s with elements added from t
4.2 s.difference_update(t) --- return set s after removing elements found in t
4.3 s.intersection_update(t) --- return set s keeping only elements also found in t
4.4 s.symmetric_difference_update(t)
4.5 s.add(x)
4.6 s.remove(x); s.discard(x) --- if there is no such element, remove return error, while discard does not
4.7 s.pop() --- remove and return an arbitrary element from s; or error if not ..
4.8 s.clear() -- remove all elements from a set
This is pretty much about a "set". Set is a simple object, it could be very helpful to used as a "check-existence" or "eliminate duplicates" tool.
1. Generate set
1.1 From List or tuple
a = [1,2,3]; b = (4,5,6); c = set(a); c = set(b)
Here there is one interesting fact, the elements in a set should be "hashable"(which means "An object is hashable if it has a hash value which never changes during its lifetime"). Since a "list"'s value could be changed, it may be only a pointer, so one cannot include a "list" as an entry in a set; but one can include a "tuple" as an element of a set.
2. Usage
2.1 "check-existence"
a = range(10); b = set(a);
10 in c
2.2 "eliminate duplicates"
a = [1,2,3,1,2,5,2,10]; b = set(a); c = list(b);
3. Evaluation
3.1 len(s); x in s; x not in s; --- whether element inside set
3.2 s.issubset(t); s.issuperset(t) --- whether set s is sub/super set of t
3.3 s.union(t) --- equivalent to s | t
3.4 s.intersection(t) --- equivalent to s & t
3.5 s.difference(t) --- equivalent to s - t
3.6 s.symmetric_difference(t) --- new set with elements in either s or t but not both
3.7 s.copy() --- new set with a shadow copy of s
4. Operator
4.1 s.update(t) --- return set s with elements added from t
4.2 s.difference_update(t) --- return set s after removing elements found in t
4.3 s.intersection_update(t) --- return set s keeping only elements also found in t
4.4 s.symmetric_difference_update(t)
4.5 s.add(x)
4.6 s.remove(x); s.discard(x) --- if there is no such element, remove return error, while discard does not
4.7 s.pop() --- remove and return an arbitrary element from s; or error if not ..
4.8 s.clear() -- remove all elements from a set
Python Study Note: Data Structure - Tuple
In my opinion, tuple is similar as List, while it is "immutable", that means, its elements cannot be changed.
For example,
a = [1,2,3] is a list
b = (1,2,3) is a tuple
We can run: a[0] = 1000;
However, we cannot run b[0] = 1000;
Basically, once the tuple is set, it is set!
If we try to understand the List and Tuple in C, the List variable name is more like a pointer; while the tuple is more like a well-structured box.
1. Why list is like Pointer?
a = [1,2,3];
b = a;
a[1] = 1000;
then b value also changes to [1,1000,3].
2. List in Tuple
a = [1,2,3]; b = [4,5,6]; c = (a,b);
a[0] = 1000;
then the tuple's value also changes to ([1000,2,3], [4,5,6]), this may because the tuple has the list's name/pointer fixed, however, its pointed space's value could change.
If we want to make a "real" copy of one list, we can do:
a = [1,2,3]; b = list(a);
Done ~
Python Study Note: Data Structure - List
List is one of the most powerful structures in Python. It is defined as "a container that holds a number of other objects, in a given order". So it is quite general and extendable.
For List type, there are several aspects we need to know:
1. Generation
1.1. Direct way
a = []; b = [1,2,3]; c = ['today', 'is', 1, 'of', 'the', 0.5, 'day'];
b = list(1,2,3);
1.2. Sequence generation
a = [x**2 for x in range(10)]
1.3 Convert from other data structures
a = (1,2,3); b = list(a)
2. Access
2.1. Direct way
a = range(10); b = a[1:5]
2.2 Sequence way
a = range(100); b = [1,2,3,10,40]; c = [a[i] for i in b];
3. Modification (list-level)
3.1 list.append() --- add item to the list
a.append(10);
3.2 list.extend() --- add elements from list to the list
a.extend([1,2,3]);
3.3 list.insert(i, x) --- add item to the list on specific location
a.insert(len(a), 10) is equivalent to a.append(10)
3.4 list.remove(x) --- remove the first item from the list whose value is x, return error if there is no such item.
3.5 list.pop([i]) --- Remove the item at the given position in the list, and return it. If no index is specified, a.pop() removes and returns the last item in the list.
3.6 list.index(x) --- Return the index in the list of the first item whose value is x, return error if there is no such item
3.7 list.count(x) --- Return the number of times x appears in the list, return error if there is no such item
3.8 list.sort() or list.sort(reverse = True) or list.reverse() --- sort the list ...
3.9 del statement --- delete either any element (del(a[0])) or the whole list! (del a). Powerful, yet dangerous :)
4. Nested List
If one generate a nested list as this:
a = [[a,b] for a in range(10) for b in range(5)]
To access to each individual:
a[i][j]
Some additional useful functions on List:
1. How to merge two lists?
c = a + b very straight forward, remember there is no a - b operation
NOTE: List is different from the matrix or vector as in Matlab, one can not directly run any mathematical operation on it, such as a = a + 1.
NOTE: two derived structures: Queue and Stack, how to do this? following examples:
1. Queue
a = range(10); a.append('new-entry'); a.pop(0);
2. Stack
a = range(10); a.append('new-entry'); a.pop();
For List type, there are several aspects we need to know:
1. Generation
1.1. Direct way
a = []; b = [1,2,3]; c = ['today', 'is', 1, 'of', 'the', 0.5, 'day'];
b = list(1,2,3);
1.2. Sequence generation
a = [x**2 for x in range(10)]
1.3 Convert from other data structures
a = (1,2,3); b = list(a)
2. Access
2.1. Direct way
a = range(10); b = a[1:5]
2.2 Sequence way
a = range(100); b = [1,2,3,10,40]; c = [a[i] for i in b];
3. Modification (list-level)
3.1 list.append() --- add item to the list
a.append(10);
3.2 list.extend() --- add elements from list to the list
a.extend([1,2,3]);
3.3 list.insert(i, x) --- add item to the list on specific location
a.insert(len(a), 10) is equivalent to a.append(10)
3.4 list.remove(x) --- remove the first item from the list whose value is x, return error if there is no such item.
3.5 list.pop([i]) --- Remove the item at the given position in the list, and return it. If no index is specified, a.pop() removes and returns the last item in the list.
3.6 list.index(x) --- Return the index in the list of the first item whose value is x, return error if there is no such item
3.7 list.count(x) --- Return the number of times x appears in the list, return error if there is no such item
3.8 list.sort() or list.sort(reverse = True) or list.reverse() --- sort the list ...
3.9 del statement --- delete either any element (del(a[0])) or the whole list! (del a). Powerful, yet dangerous :)
4. Nested List
If one generate a nested list as this:
a = [[a,b] for a in range(10) for b in range(5)]
To access to each individual:
a[i][j]
Some additional useful functions on List:
1. How to merge two lists?
c = a + b very straight forward, remember there is no a - b operation
NOTE: List is different from the matrix or vector as in Matlab, one can not directly run any mathematical operation on it, such as a = a + 1.
NOTE: two derived structures: Queue and Stack, how to do this? following examples:
1. Queue
a = range(10); a.append('new-entry'); a.pop(0);
2. Stack
a = range(10); a.append('new-entry'); a.pop();
Python Study Note: "Help-like" command & Data Structures Intro.
In "raw" Python language, there are quite a few command to help me to understand what is going one with the software, these commands act like "help" functions for me. They are:
1. dir
Without arguments, return the list of names in the current local scope. With an argument, attempt to return a list of valid attributes for that object.
So, if I have no idea about how many variables are existing in the space, I run "dir()"; if I forget what attributes a variable has, I can run "dir(variable_name)", quite handy.
2. type
With one argument, return the type of an object. The return value is a type object.
Many times I may forget what is the type of some variable, and may not be able to add any functions on it! Type could help this very much.
Ex. >> type('') is str
Basic data structure is the basis for any "fancy" structures (Pandas.DataFrame, etc). They are:
1. Basic: int, float, string
2. List
3. Tuple
4. Dictionary
5. Set
6. Sequence
The data structures are quite rich, and will write something about them in a separate Post.
1. dir
Without arguments, return the list of names in the current local scope. With an argument, attempt to return a list of valid attributes for that object.
So, if I have no idea about how many variables are existing in the space, I run "dir()"; if I forget what attributes a variable has, I can run "dir(variable_name)", quite handy.
2. type
With one argument, return the type of an object. The return value is a type object.
Many times I may forget what is the type of some variable, and may not be able to add any functions on it! Type could help this very much.
Ex. >> type('') is str
Basic data structure is the basis for any "fancy" structures (Pandas.DataFrame, etc). They are:
1. Basic: int, float, string
2. List
3. Tuple
4. Dictionary
5. Set
6. Sequence
The data structures are quite rich, and will write something about them in a separate Post.
Python Study Note: Raw Python -> Numpy/SciPy -> Pandas
After using so many different software and programming languages, I found it would be quite challenge to switch from one language mind to another. For example, these days, I may use Python the most, and after a while I may need to switch to Matlab for a specific programming homework, and after one week, I need to go back to Python for daily work ... The barrier of fast switching between languages are its syntax and common functions, it is better to record them down, in case need to do the switch again :)
For the language Python, I like it very much. It is very useful for data analysis and Machine Learning technology application. Better to make sure I can dive into Python programming anytime in the future ~
The high-level Python data analysis tools, such as Pandas, are always built based on some low-level packages, such as NumPy and SciPy; and those low-level packages are based on the Basic syntax of Python itself ... what a hierarchical structure. To use Pandas smoothly, it would be important to make sure I ink the low-level concepts in my mind.
For the language Python, I like it very much. It is very useful for data analysis and Machine Learning technology application. Better to make sure I can dive into Python programming anytime in the future ~
The high-level Python data analysis tools, such as Pandas, are always built based on some low-level packages, such as NumPy and SciPy; and those low-level packages are based on the Basic syntax of Python itself ... what a hierarchical structure. To use Pandas smoothly, it would be important to make sure I ink the low-level concepts in my mind.
Subscribe to:
Posts (Atom)