Various processing of Python

A memorandum for myself

While coding using Python, I will leave what I searched for as "Oh, how do I do this process" in this post as a memorandum for myself. Will be updated from time to time.

Manipulation of dictionary type data (dict: dictionary type data to be operated)

Key-only list, value-only list

dict.keys()
dict.values()

By the way, a list that includes both keys and values

dict.items()

Add new keys and values

dict[New key] =value

Change the value corresponding to the key

dict[Key] =value
dict[Key] += 1

Specify a key to delete a dictionary element

del dict[Key]

List type data (list: list type data to be operated)

Determine if it has an element (returns True or False)

An element in list

Get the index of an element in the list

list.index(An element)

Add and insert elements in the list

list.append(An element) # リストの末尾にAn elementを追加
list.insert(0,An element) #Add the element at the top of the list (change 0 to insert the element at the corresponding position)

Decompose the string into a list (let's say the string is str)

list = str.split(' ') #This decomposes str with whitespace and creates a list with each as an element of the list.

A list with n elements and 0 elements

list = [0] * n

Reverse the list

list.reverse()

Concatenate lists that have strings as elements into strings

"".join(list) # ""If you put a specific symbol inside, that character is put in between and connected

Get a list in which each element of a list with int as an element is converted to a string

map(str, list)

Delete and cut list elements

del(list[index]) #Delete element
list.pop(index) #This is a cut, so
list1.append(list2.pop()) #You can add the last element of list2 to the end of list1
#By the way, pop()If no argument is specified for, the last element is targeted.

List with numbers from 0 to 9 as elements (list comprehension)

list = [i for i in range(10)]
list = [i for i in range(10) if i % 2 == 0] #In addition, if statements can be included

Checks for elements in the list and returns a dictionary where the key is the element in the list and the value is the number of times the key appears.

import collections
list = ['a', 'b', 'c', 'b', 'd', 'a']
counter = collections.Counter(list) # -> {'a':2, 'b':2, 'c':1, 'd':1}

sort

Sort in ascending order

sorted(list) #Sort the elements in the list in ascending order
sorted(dict.keys()) #Sort the dictionary in ascending key order
sorted(dict.items(), key=lambda x:x[1]) #Sort the dictionary in ascending order of value

Sort in descending order

sorted(list, reverse=True) #Just assign True to the reverse argument

Variable scope

Global variables

count = 0
def add_count():
    global count    #Access variables defined outside the function and
                    #Add global to apply the change outside the function
    count += 1

Recommended Posts

Various processing of Python
About various encodings of Python 3
Post processing of python (NG)
Introduction of Python
python image processing
Python file processing
Basics of python ①
# 3 [python3] Various operators
Copy of python
Introduction of Python
Basics of binarized image processing with Python
(Java, JavaScript, Python) Comparison of string processing
Summary of various for statements in Python
Grayscale by matrix-Reinventor of Python image processing-
[Python] Various data processing using Numpy arrays
1. Statistics learned with Python 1-3. Calculation of various statistics (statistics)
Various format specifications of str.format () method of Python3
Drawing with Matrix-Reinventor of Python Image Processing-
[Python] Various combinations of strings and values
Status of each Python processing system in 2020
Matrix Convolution Filtering-Reinventor of Python Image Processing-
Python distributed processing Spartan
[Python] Operation of enumerate
List of python modules
File processing in Python
Python: Natural language processing
Communication processing by Python
Multithreaded processing in python
Unification of Python environment
Copy of python preferences
[python] Create a list of various character types
Basics of Python scraping basics
View the result of geometry processing in Python
[python] behavior of argmax
First Python image processing
Various Python visualization tools
Usage of Python locals ()
the zen of Python
Text processing in Python
Installation of Python 3.3 rc1
1. Statistics learned with Python 1-2. Calculation of various statistics (Numpy)
Various settings of Python static blog generation tool'Pelican'
Image processing? The story of starting Python for
Image processing with Python
# 4 [python] Basics of functions
Basic knowledge of Python
Sober trivia of python3
Summary of Python arguments
Addition of fixed processing when starting python interpreter
Basics of python: Output
Installation of matplotlib (Python 3.3.2)
Application of Python 3 vars
Python string processing illustration
Basic processing of librosa
Image processing by matrix Basics & Table of Contents-Reinventor of Python image processing-
Implementation example of simple LISP processing system (Python version)
Full-width and half-width processing of CSV data in Python
Old openssl causes problems in various parts of python
[Chapter 5] Introduction to Python with 100 knocks of language processing
Examine the close processing of Python dataset (SQLAlchemy wrapper)
[Chapter 2] Introduction to Python with 100 knocks of language processing