This is a summary for those who have studied once, but "What was this? How was this ...".
Python can now be selected in FE (Fundamental Information Technology Engineer Examination). Originally, this spring exam was scheduled to be the first question, but it was canceled due to the influence of Corona.
Such a Python list is also given in the FE example, so I tried to summarize how to use it.
You can look back just before the exam, or remember the methods that you tend to forget in everyday programming. Please use all means.
First of all, from the basic list declaration.
alist = []
blist = [1, 1, 2, 3, 5, 8, 13]
clist = list(range(5))
dlist = [i for i in range(10) if (i % 2 == 0)] #List comprehension
print(f"alist = {alist}\nblist = {blist}\nclist = {clist}\ndlist = {dlist}")
output
alist = []
blist = [1, 1, 2, 3, 5, 8, 13]
clist = [0, 1, 2, 3, 4]
dlist = [0, 2, 4, 6, 8]
It's a blind spot. Therefore, in the following handling of list, both passing by reference and passing by value will be explained.
--Substitute list with =
--Pass by reference
--The assignment source (ʻalist) changes →
blistalso changes --Substitute the return value of the
.copy () method --Pass by value --The assignment source (ʻalist
) has changed → clist
has not changed
alist = [1, 2, 3]
#Pass by reference
blist = alist
#Pass by value
clist = alist.copy()
def delete_middle(list_obj, msg):
list_obj.pop(int(len(list_obj)/2))
print(msg, list_obj)
delete_middle(alist, "alist =")
print(f"blist = {blist}")
print(f"clist = {clist}")
output
alist = [1, 3]
blist = [1, 3]
clist = [1, 2, 3]
--Add only one element to the end
--. Append (x)
method
--Add one to the specified index location
--. Insert (index, obj)
method
--Add list to list
--Use +
operator
--Even if the elements of blist
and clist
change after the operation, it does not affect dlist
.
alist = [1, 2, 3]
blist = alist
clist = alist.copy()
#Add 4 to the back
alist.append(4)
#Add 100 to index of 1
alist.insert(1, 100)
#Add list to list
dlist = blist + clist
print(f"alist = {alist}\nblist = {blist}\nclist = {clist}\ndlist = {dlist}")
alist = [1, 100, 2, 3, 4]
blist = [1, 100, 2, 3, 4]
clist = [1, 2, 3]
dlist = [1, 100, 2, 3, 4, 1, 2, 3]
alist = [1, 3, 5, 7, 9]
blist = alist
clist = alist.copy()
del alist[2]
print(f"del alist[2] =>\n\talist = {alist}\n\tblist = {blist}\n\tclist = {clist}\n")
alist.pop(2)
print(f"alist.pop(2) =>\n\talist = {alist}\n\tblist = {blist}\n\tclist = {clist}")
output
del alist[2] =>
alist = [1, 3, 7, 9]
blist = [1, 3, 7, 9]
clist = [1, 3, 5, 7, 9]
alist.pop(2) =>
alist = [1, 3, 9]
blist = [1, 3, 9]
clist = [1, 3, 5, 7, 9]
Only the value at the beginning is deleted. Not all of its values.
alist = [1, 2, 2, 3, 3, 3]
blist = alist
clist = alist.copy()
alist.remove(2)
print(f"alist = {alist}\nblist = {blist}\nclist = {clist}")
output
alist = [1, 2, 3, 3, 3]
blist = [1, 2, 3, 3, 3]
clist = [1, 2, 2, 3, 3, 3]
If you do not put an argument in .pop ()
, it is the same as .pop (-1)
, and the last element of the list is deleted.
alist = [1, 3, 5, 7, 9]
blist = alist
clist = alist.copy()
alist.pop()
print(f"alist.pop(2) =>\n\talist = {alist}\n\tblist = {blist}\n\tclist = {clist}")
output
alist.pop() =>
alist = [1, 3, 5, 7]
blist = [1, 3, 5, 7]
clist = [1, 3, 5, 7, 9]
alist = [1, 2, 3]
blist = alist
clist = alist.copy()
# alist = []The same thing is done.
# alist = []It's confusing with the declaration, but it's the development team's preference.
alist.clear()
print(f"alist = {alist}\nblist = {blist}\nclist = {clist}")
output
alist = []
blist = []
clist = [1, 2, 3]
alist = [20, 1, 5, 13, 8]
blist = alist
clist = alist.copy()
alist.sort()
print(f"alist = {alist}\nblist = {blist}\nclist = {clist}")
output
alist = [1, 5, 8, 13, 20]
blist = [1, 5, 8, 13, 20]
clist = [20, 1, 5, 13, 8]
alist = [333, 1, 333, 22, 333, 22]
#22 is in what order
index = alist.index(22)
print(f"index = {index}")
output
index = 3
alist = [0, 1, 1, 0, 1, 0 ,1, 0, 0]
#How many times 1 came out
count = alist.count(1)
print(f"count = {count}")
output
count = 4
I think that the list method of the major is suppressed. I wanted to explain the behavior of passing by reference and passing by value, so it became difficult to read. Excuse me mm
I hope you can use it as a memorandum. Well then.
Recommended Posts