python, golang several data collection type (II)

Connected to a

2. Dictionary

    1) python

  Dictionary-based python '{}' of said array with '[]' is represented tuple in tuple '()' of FIG.

  a) initialization

  With key: value format initialization on the line.

1 >>> a={1:2,"2":3,"3":"4"}
2 >>> a
3 {1: 2, '3': '4', '2': 3}

  key, value type, in which a dictionary can have multiple (Note: key can not be arrays (List), because the key can not be modified, may be a tuple (tuple)) . If during initialization, write the same key, so after writing key override earlier.

1 >>> b={1:"aaa",1:"bbb"}
2 >>> b
3 {1: 'bbb'}

  b) access element

  Directly by [] to take, a bit like array (List), but the middle brackets is key, not a subscript.

1 >>> a["2"]
2 3
3 >>> a[1]
4 2
5 >>> a[2]
6 Traceback (most recent call last):
7   File "<stdin>", line 1, in <module>
8 KeyError: 2

  Without this key error. It is generally to do judgment:

. 1 >>> A = {. 1: " AAA " , 2: " BBB " ,. 3: " CCC " }
 2 >>> a.has_key (. 3)   # dictionary method of carrying has_key 
. 3  True
 . 4 >>> A. has_key (. 4 )
 . 5  False
 . 6 >>>. 3 in a   # Val Key in dict methods, and the like in list array. 
. 7  True
 . 8 >>>. 4 in A
 . 9 False

  Traversal:

1 for k in a:
2     print "k:",k
3 
4 for (k,v) in a.items():
5     print "k:",k,",v:",v
6 
7 for k,v in a.iteritems():
8     print "k:",k,",v:",v

  Where k The first way is to key value, not value value; array (list) returns the value of such traversal, instead of the subscript (subscript returned saying also lacks significance ah ..O__O "...) It should be. pay attention to the next.

  c) modify elements

  Direct assignment. You can modify the value of any type. Note If you modify the value in traversal. For example in the above example, the assignment can not be changed v = xxx dictionary itself.

1 a[1] = "2"
2 a[1] = 22
3 a[1] = {"k1":"v1","k2":"v2"}

  d) insert elements

  Like modify elements of the operation. Direct assignment, key does not exist is to insert new elements.

  e) Removing elements

  1. You can use the keyword del. Remove elements dictionary (Similarly, if the key does not exist to a [key] value will be reported abnormal way), or delete the dictionary itself. This is similar to the operation of the array.

 1 >>> a={1:"aaa",2:"bbb"}
 2 >>> del a[1]  #删除字典元素
 3 >>> a
 4 {2: 'bbb'}
 5 
 6 >>> del a  #删除字典
 7 >>> a
 8 Traceback (most recent call last):
 9   File "<stdin>", line 1, in <module>
10 NameError: name 'a' is not defined

  2. Use the dictionary built-in methods

. 1 >>> A = {. 1: " AAA " , 2: " BBB " }
 2 >>> a.pop (. 1)   # using pop (key) method. But if the key does not exist, an exception report. 
. 3  ' AAA ' 
. 4 >>> A
 . 5 {2: ' BBB ' }
 . 6  
. 7 >>> a.pop (. 3, "" )   # POP method can be used with default parameters (there is an empty string ""), and so if key 3 is not present, the default value of "return." 
. 8  '' 
. 9  
10  
. 11 >>> a.clear ()    # can clear (), 
Clear the entire dictionary (different from the del, the dictionary itself is still here). 12 is >>> A
 13 is {}

 

2) golang

  a) initialization 

The key can be any map can be used == or! Type = comparison operator, such as string, int, float. Therefore arrays, slices, and not as a key structure (Note: The structure comprising an array of slices not as the key, only the built-in type comprising a struct is used as a key), but the pointer and can interface type. It may be any type of value, including functions other interface types.

For performance reasons, or for a large map will be rapid expansion of the map, even if only a vague idea capacity, also a good idea to indicate its capacity capacity.

var a map [int] string // definition just finished a, the value is nil, a direct reference will Throws
b: = make (map [int] string) // initialize finished array can be used
By assigning a = b //, a complete initialization
var a map [int] string = map [int] string {123: "abc", 456: "aaa"} // initialization is completed
a: = map [int] string {123: "abc", 456: "aaa"} // initialization complete  
a: = make (map [int ] string, 100) // initialize an array, and gives the initial capacity

 

  b) access element

  Direct use [] value by key (although already fast, but still much slower than [] array and slice values).

value: = the value zero if the key does not exist map1 [key] //, returns a value (i.e., a string value is "", value is a pointer to an interface or the like is nil). This is not the same and python. 
value, ok: = map1 [key ] // determines whether ok key can be based on the existence, true that there is; to false does not exist, its value is given a value of zero.

  Traversal:

The underlying map is a hash table to achieve, so the map will not be sort key.

map1 := map[int]string{123: "aaa", 234: "bbb", 345: "ccc"}
for key: = range map1 {// Here python and the like
	...
}

for key, value := range map1 {
	...
}

for key, _ := range map1 {
	...
}

for _, value := range map1 {
	...
}

  

  c) modify elements

Use [] direct assignment. Generally, the value is taken out of operation again, it is not up to the purpose of modifying the map itself. This is the same python.

for key, value := range map1 {
	map1 [key] = "ddd" // modifies the value of map1
	value = "ddd" // here only copy out the modified value, can not modify the value in the map1
}

  

  d) insert elements

Like modify elements of the operation. Direct assignment, key does not exist is to insert new elements.

  e) Removing elements

delete (map1, key1) // even if key1 does not exist, it does not complain. This is the python del a [key] will complain is not the same.

  

Guess you like

Origin www.cnblogs.com/zxq89/p/10697664.html