From:http://www.cnblogs.com/michael-kong/archive/2012/07/11/2585840.html
- Dictionary is one of the built-in data types for Python, which defines the relationship between the key and the value.
- Each element is a Key-value pair, and the entire element set is enclosed in curly braces.
- You can reference its value by key, but you cannot get key by value
- There can be no duplicate key in a dictionary. Assigning a value to an existing key overrides the original value. New key-value can be added at any time. This syntax is the same as modifying the existing value.
- When using dictionary, you need to know: Dictionary key is case-sensitive
- Dictionary is not just for storing strings. The value of Dictionary can be any data type, including strings, integers, objects, and even other Dictionary. In a single dictionary, the value of dictionary does not need to be all the same data type and can be mixed and matched as needed. Dictionary's keys are much stricter, but they can be strings, integers, and several other types (which are discussed later). You can also mix and match key data types in a dictionary
- del allows you to remove individual elements from a dictionary using key.
- Clear clears all elements from a dictionary. Note the empty Brace collection represents a dictionary with no elements.
- The list is a set of ordered elements enclosed in square brackets.
- The List can be used as an array starting with the 0 subscript. The first element of any non-empty list is always li[0]
- a negative index accesses an element by counting forward from the tail of the list. The last element of any non-empty list is always li[-1]. If the negative index makes you feel confused, you can understand this: li[-n] = = Li[len (li)-n]. So in this list, li[-3] = = li[5-3] = = li[2].
- you can get a subset of the list by specifying 2 indexes, called a "slice". The return value is a new list that contains all the elements in the list that start in order from the first slice index (here , li[1]), until but not including the second slice index (here, li[3]).
- if all two shard indexes are omitted, this will include all elements of the list. But unlike the original list named Li, it is a new list that has exactly the same elements as Li . li[:] is a shorthand for generating a list full copy.
- Append appends a single element to the end of the list.
- Insert Inserts a single element into the list. The numeric parameter is the index of the insertion point. Note that the elements in the list do not have to be unique, there are two independent elements that have the same value as ' new ' .
- extend is used to connect to list. Note that you do not use multiple parameters to invoke extend, which is called using a list parameter.
- Lists's two methods extend and append look similar, but in fact they are completely different. extend accepts a parameter, which is always a list, and adds each element in the list to the original list
- On the other hand, append accepts a parameter, which can be any data type, and is simply appended to the tail of the list. Call the append method here using a list parameter containing 3 elements.
- Index finds the first occurrence of a value in the list and returns the index value.
- To test whether a value is inside the list, use in, and return TrueIf the value exists, otherwise return to False .
- Remove removes the first occurrence of a value from the list.
- Pop is an interesting thing. It does two things: delete the last element of the list, and then return the value of the deleted element. Note that this differs from Li[-1] , which returns a value but does not change the list itself. Also differs from li.remove (value), which changes the list but does not return a value.
- Lists can also be connected with the + operator. List = list + otherlist is equivalent to list. Extend (otherlist). But the + operator returns a new (connected) list as a value, and extend only modifies the existing list. In other words, for large lists, the extend executes faster.
- Python supports the + = operator. Li + = [' both '] is equivalent to li.extend ([' both ']). The + = operator can be used for lists, strings, and integers, and it can also be overloaded for user-defined classes.
- The * operator can act as a repeating device on the list. Li = [1, 2] * 3 equals to li = [1, 2] + [1, 2] + [1, 2], and three lists are connected as one.
- A tuple is an immutable list. One is that creating a tuple cannot change it in any way.
- A tuple is defined in the same way as a list, except that the entire set of elements is surrounded by parentheses rather than square brackets.
- The elements of a tuple are sorted in the same order as the list. The tuples index is the same as list starting from 0, so the first element of a non-empty tuple is always t[0].
- A negative index is counted as a list from the tail of a tuple.
- As with list shards (slice) can also be used. Note that when a list is split, a new list is obtained, and when a tuple is split, a new tuple is obtained.
- Tuple has no method: No append or extend method, no remove or pop method, no index method, can use in to see if an element exists in a tuple.
The list () function and the tuple () function accept objects that can be stroked today (such as a sequence) as parameters, and create a new list and tuple by shallow copying the data. Although strings are also sequence types, they are generally not used for list () and tuple (). more cases, They are used for Xuan exchange between two types, such as you need to convert an existing tuple into a list type (then you can modify its elements), or vice versa.
alist=[' 123 ', ' 456 '];
Atuple=tuple (alist);
Print Atuple
>>> (' 123 ', ' 456 ')
Alist==atuple
>>> False
Alist2=list (Atuple)
Alist2==alist
>>>true
Alist is Alist2
>>>false
Again ID () confirm, [ID (x) for x in Alist,atuple,alist2]
>>>[10903800,12003900,11730280]
So either list () or tuple () is not possible to complete the conversion, that is, passing a tuple to the list does not become a real list, while passing a list to a tuple () will not become a true tuple. Although the previous two objects have the same data set, But the variables point to not the same object. Note that all of their values are the same, and a list cannot be "equal" to a tuple.
[Go] Python list, tuple, dict difference