[PYTHON] Array operation etc. 1

Yes. It's a story based on 2.7 series. It's spicy (´ ・ ω ・ `) It will be covered in various ways. It's spicy ... φ (・ ω ・ `c⌒ Are the arrays you'll use early on for lists, sets, tuples, dictionaries (associations)? I don't think using deques is anymore for beginners. .. References such as deque http://docs.python.jp/2/library/collections.html

Reference: http://docs.python.jp/2/tutorial/datastructures.html In the following, the variable name of the array is written as l.

List type

Probably the most frequently used. There is a guarantee of the order of the elements inside. A very good child who can change only a part of the elements inside, or add / delete at the beginning, end, or somewhere in the middle.

Initialization

l=[]

Additional operation

l.append(x)
Add x to the end of the list. Do not write l = l.append (x). Or rather, l will be empty. l.insert(i, x)
Add x at the i-th position in the list. l.extend(l2)
Add l2 to the end of list l. If l = [1,2,3] l2 = [4,5,6], then l = [1,2,3,4,5,6]. If you want to keep the original l, write l3 = l + l2.

Delete operation

l.remove(x)
Delete the first x in the list (the youngest index, the leftmost). If you try to delete a value that does not exist, you will get a ValueError, so try except or if in. l.pop(i)
Take out the i-th element. i is not specified, otherwise it will be automatically added to the end. The element removed by p = l.pop (i) can be assigned to another variable.

Other operations

l.count(x)
Count the number of x in the list. l.sort() Sort the elements in the list in ascending order. Even if int type and str type are mixed in the list, no error will occur, but I don't think they will be mixed, so details are omitted. l.reverse()
Sort in reverse order in the list. Not in descending order. If you want to sort in descending order, sort () and then reverse ().

Set type

Probably the second most frequently used. There are no duplicate elements inside. There is no guarantee of order.

Initialization

l=set()

Additional operation

l.add(x)
Add x. Adding an element that already exists does not cause an error. It seems that list type things cannot be put in. When managing coordinates by route search etc., it seems that it is necessary to make it a tuple type such as l.add ((y, x)).

Delete operation

l.remove(x)
Remove the x inside. The x, which exists only once, disappears. l.pop() Take out one of the elements inside. I don't think there is any guarantee what will come out. The element retrieved by p = l.pop () can be assigned to another variable.

Other operations

No guarantee of order, no concept itself? It seems that you can brute force the elements inside with for i in l :. Logical operation? You can do various things with. I would like to add this later.

Dictionary type (associative array)

Probably the third most frequently used. The key is not duplicated in the element inside. Specifically, it is abbreviated at the time of this problem, but in the input restriction of the problem, the dictionary type is used, such as when the candidates that become keys are 0 to 10 ^ 9, but only about 100 types of keys are actually processed. Will you use it? I think that the process is quite painful by making an array of length 10 ^ 9 with 0 padding.

Initialization

l={} The elements inside are {key1: value1, key2: value2} key and value are paired. The key does not exist in duplicate, so if you try to add a duplicate key, the one that originally existed will disappear.

Additional operation

Add key l[key]=value
It is dangerous to add it suddenly, so I think you should check if the key already exists. If l.has_key (key): or if key in l :? I only use the former pattern for no particular reason. Since the key cannot be changed and must be an immutable element, list type etc. cannot be specified. Add value Since various types of values can be used for value, increase / decrease the number of occurrences of key by l [key] + = 1, or l [key] .append ("hoge") something that belongs to a specific key. You can do it.

Delete operation

Delete the key itself (value also disappears) del l[key]
Delete value Since some value needs to exist, I think it will be 0, "", or [] depending on the purpose.

Other operations

l.keys() You can retrieve only the key to another list with s = l.keys () etc. for k,v in sorted(l.items()): Sort by key value for k,v in sorted(l.items(), key=lambda x:x[1])

Tuple type

The elements inside cannot be changed. There is a concept of order.

Initialization

l=() Even if it is, it will be treated as immutable, so I don't think it will be prepared by initialization.

Add / delete operation

I feel that there is no addition or deletion. I don't think it's possible to combine two or more tuples by reassigning or assigning to a new variable, or trying hard to slice and delete. .. ..

Other operations

I don't think there is anything in particular to do. I feel that it is used only in situations where it is useless unless it is a tuple type and the elements inside are not changed, such as using a dictionary type key or putting it in a set type to change the coordinates.

A light story that does not limit the type

For list type, for i in range (n): or index value, do not strictly manage what is likely to be out of range, and try ~ except to cheat with pass when an error occurs. The process up to the occurrence of the error is performed, and it is effective, so use it carefully. When it comes to array operations, I think I have to write a story about how to use for, but this time.

Recommended Posts

Array operation etc. 1
[python] Array slice operation
Bit operation
Unattended operation of Google Spreadsheets (etc.) in Python