I tried to summarize the python array, dictionary generation method, and loop method. When learning a new language, I would like to suppress the arrangement, dictionary generation method, and loop method in a hurry, so I think it would be convenient if such information is gathered, and this article also serves as a memorandum for myself. I posted. We hope that it will be helpful for those who have recently started using python, and that it can be used as a memorandum.
array.py
#Array generation
arr = [] #Empty array generation
arr = [1, 2, 3] #Array generation with elements
#Add array elements from 1 to 5
# ※ range(start, stop)Start-stop-Up to 1 is repeated.
#If start is not specified, it will start from 0.
for i in range(1, 6):
arr.append(i)
#Array loop
for val in arr:
print(f"val:{val}")
#Loop of array with index
for index, val in enumerate(arr):
print(f"index:{index}, val:{val}")
I often misspell ʻenumerate` when I want to loop with an index. I'm going to copy and paste it to myself. ..
dict.py
dic = {} #Empty dictionary generation
dic = {"key1": 1, "key2": 2} #Generate dictionary with elements
#Add dictionary
for i in range(1, 6):
key = f"key{i}"
dic[key] = i
#Loop dictionary key
for key in dic.keys():
print(f"key:{key}, val:{dic[key]}")
#Loop the dictionary value
for val in dic.values():
print(f"val:{val}")
#dictionary key,Loop value
for key, val in dic.items():
print(f"key:{key}, val:{val}")
The dictionary ʻitems () is much easier to come up with than the array ʻenumerate
spelling: relaxed:
Whether you want to loop only the key or only the value of the dictionary
For the time being, I think it's okay to use ʻitems ()` to get both key and value.
It is used when you want to generate a new array or dictionary from an array or dictionary. With the usual method, you need to first create an empty array, then loop the original array and add elements to the new array, but with comprehension, such an operation is 1 It can be achieved with a line. It also improves performance.
list_comp.py
#Initialization of original array / dictionary
arr = [1, 2, 3, 4, 5]
dic = {"key1":1, "key2": 2, "key3": 3, "key4": 4, "key5": 5}
#Array to array(Double each array element)
conv_arr = [val * 2 for val in arr]
print(conv_arr)
#Dict from array(index to key)
conv_dic = {f"key{index + 1}": val for index, val in enumerate(arr)}
print(conv_dic)
#Array from dict(key-in the value string)
conv_arr = [f"{key}-{val}" for key, val in dic.items()]
print(conv_arr)
#From dict to dict(Capitalize key and double value)
conv_dic = {key.upper(): val * 2 for key, val in dic.items()}
print(conv_dic)
Grammatically, the case of an array
[Expression for arbitrary variable name in iterable object]
In the case of dictionary
{key: value for arbitrary variable name in iterable object}
It will be.
It is also possible to add an IF condition to the list comprehension notation and extract only the elements that meet the condition.
list_comp.py
#Initialization of the original array
arr = [1, 2, 3, 4, 5]
#Array to array(Get only even elements and multiply the elements by 10)
conv_arr = [val * 10 for val in arr if val % 2 == 0]
print(conv_arr)
We hope that this article will be of some help to you. Have a good python life!
Recommended Posts