I will write it because it has the meaning of leaving a memo of knowledge that I forget to write even though I think that it is not often used in AtCoder. The explanation is shallow, so if you want to know more, please see other articles.
It is often checked whether the variable a is included in the list of data by ʻif a in data`. However, I was impressed if there was something that I could easily write back about what number it was in, so I will introduce it.
qiita1.py
box = [4, 2, 1, 0, 3]
print(box.index(3))
#4
By using ʻindex ()` in this way, the answer that the value of 3 is the fourth is returned.
It's very difficult to say the headline, but in simple terms, it feels like you can set priorities when sorting a multidimensional list. I think this is still difficult, but ...
box = [[1,1,8], [8,6,4], [5,9,8]]
If you have such a multidimensional list, you might think like this. "I want to sort them in ascending order, but I want them to be in ascending order ** in the order of the third number, the first number, and the second number." When this happens, the lambda expression comes into play.
qiita2.py
box = [[1,1,8], [8,6,4], [5,9,8]]
box_sort = sorted(box)
print(box_sort)
#[[1, 1, 8], [5, 9, 8], [8, 6, 4]]
box_sort = sorted(box, key=lambda box_i:(box_i[2], box_i[0], box_i[1]))
print(box_sort)
#[[8, 6, 4], [1, 1, 8], [5, 9, 8]]
You can do this. The result will change because the priority when sorting is sorted in the order of writing. There is a similar subject in the heading below, so please see that as well. I won't explain about lambda expressions. References ([Python] Sort multidimensional list by multiple keys)
box=[[1,1,8], [8,6,4], [5,9,8]]
If you have such a multidimensional list, you might think like this. "I want you to make ** ascending, descending, ascending ** in the order of the third number, the first number, and the second number." If this happens, you can't do it with the heading above, right? ?? I think
qiita3.py
box = [[1,1,8], [8,6,4], [5,9,8]]
box_sort = sorted(box, key=lambda box_i:(box_i[2], box_i[0], box_i[1]))
print(box_sort)
#[[8, 6, 4], [1, 1, 8], [5, 9, 8]]
box_sort = sorted(box, key=lambda box_i:(box_i[2], -box_i[0], box_i[1]))
print(box_sort)
#[[8, 6, 4], [5, 9, 8], [1, 1, 8]]
By adding -
in this way, the value is multiplied by -1
, so the larger the value, the smaller the value. You can use this in descending and ascending order. For the problem of ABC128_B, I think that it will be a good example to study if you solve it using this lambda expression.
After creating a dictionary type using Counter etc., I remember struggling for a while with how to process the dictionary type, so I will write it easily.
keys ()
: Only the key for each element can be retrieved.
values ()
: Only the values for each element can be retrieved.
ʻItems () `: Both key and value for each element can be retrieved.
It is used when taking out using these three or turning with for.
References (Python dictionary (dict) for loop processing (keys, values, items) )
Since it was the first time, I used polite language, but I think it will be sloppy from the next time. Since this is my first post, please contact me if you have any incorrect information or bad manners. Please note that the memo is also meaningful.
There was a mistake in the contents of strip (), it turned out that there is no need to use strip () because the error does not actually occur where the previous description says that an error occurs. Therefore, the description there has been deleted. Dissemination of uncertain information We are very sorry.
Recommended Posts