A variable contains multiple data.
Character strings and numerical values can be mixed
a = 1
b = 'Mandarin orange'
c = 2
li = [a, b, c]
print(li)
#output[1,Mandarin orange,2]
List in list
a = "Ichi"
b = 2
c = "Mr."
d = 4
li = [[a, b], [c, d]]
print(li)
#output: [["Ichi", 2], ["Mr.", 4]]
Indexes (numbers) are assigned to the list. You can specify it and retrieve it
① Starts as 0 from the left end and can be retrieved by specifying a numerical value.
② The last one-As 1
-2,-You can also take out in order of 3.
List = [0, 1, 2, 3]
print(List [0])
#"0" with index 0 is output
List = [0, 1, 2, 3]
print(List [-3])
#The third "1" from the back is output
① Number:Take out from the specified number with
②:Take out to the front of the number with the number(-In the case of, counting from the left)
al = ["0","1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]
print(al[1:4])
#Index 1-3: ["1", "2", "3"]
print(al[:4])
#Index 0-3: ["0", "1", "2", "3"]
print(al[7:])
#Index 7 ~ End["7", "8", "9","10"]Is output
print(al[0:100])
#All output because it exceeds the range
print(al[0:-5])
#Index 0 to 6th from the back["0","1", "2", "3", "4","5"]
As an additional method
① Number designation
② Finally with append(Only one can be done)
③ +=And finally(Can be multiple)
al = ["0","1", "2", "3", "4"]
al[0] = "A"
print(al)
#Output result: ["A", "1", "2", "3", "4"]
al = ["0","1", "2", "3", "4"]
al[1:3] = ["B", "C"]
print(al)
#Output result: ["0", "B", "C", "3", "4"]
al = ["0","1", "2", "3", "4"]
al.append("f")
print(al)
#Output result: ["0","1", "2", "3", "4","f"]
al = ["0","1", "2", "3", "4"]
al += ["f","g"]
print(al)
#Output result: ["0","1", "2", "3", "4","f","g"]
del list[index]
al = ["0","1", "2", "3", "4"]
del al[3:]
print(al)
#Output result: ["0", "1", "2"]
al = ["0","1", "2", "3", "4"]
del al[1:3]
print(al) # ["0", "3", "4"]Is output
When just assigning a list variable to a list variable
When the assignment destination is changed, the assignment source also changes.
#When only the contents of the list
list() #Use the one on the left.
#Variable as it is
al = ['0', '1', '2']
al_copy = al
al_copy[0] = 'A'
print(al_copy)
print(al)
#Output result
['A', '1', '2']
['A', '1', '2']
# list()Only the contents using
al = ['0', '1', '2']
al_copy = list(al)
al_copy[0] = 'A'
print(al_copy)
print(al)
#Output result
['A', '1', '2']
['0', '1', '2']
Recommended Posts