Check how to use the enumerate function by dividing it into patterns.
-Retrieve the index number of the array -Set the initial value of the index number arbitrarily ・ Extract elements
You can retrieve the element without enumerate, so use it when you want the index number.
for a, b in enumerate(A, n):
└ "a": Variable to put the index number
└ "b": Variable to put the element
└ "A": array (or tuple)
└ "n": Initial value of index number
The print method is often used to see the output.
** ▼ Point ** ・ Used as a set with a for statement └ Not used by enumerate alone
-Define two variables └ Variable to put index number └ Variable to put the element
・ Even one variable is OK └ Index number and element are a set └ Output: (index number, element)
・ The initial value of the index number can be set arbitrarily. └ Default starts from 0 └ Only integers (minus can be specified)
▼ Output (Index number, element)
1 variable
list = ["AAA","BBB","CCC","DDD"]
for a in enumerate(list):
print(a)
#Output result
(0, 'AAA')
(1, 'BBB')
(2, 'CCC')
(3, 'DDD')
Of course, the variables are arbitrary. Either i or AAA is OK.
for a,b in enumerate(A):
└ "a": Variable that contains the index number
└ "b": Variable that contains the element
└ "A": array (or tuple)
Only the variable defined earlier (here, "a") is used at the time of output.
Extract only index number(2 variables)
list = ["AAA","BBB","CCC","DDD"]
for a,b in enumerate(list):
print(a)
#Output result
0
1
2
3
・ A = (index number, element) ・ A [0] = index number
Extract only index number(1 variable)
list = ["AAA","BBB","CCC","DDD"]
for a in enumerate(list):
print(a[0])
#Output result
0
1
2
3
for a,b in enumerate(A):
└ "a": Variable that contains the index number
└ "b": Variable that contains the element
└ "A": array (or tuple)
Only the variable defined later (here, "b") is used at the time of output.
Extract only index number(2 variables)
list = ["AAA","BBB","CCC","DDD"]
for a,b in enumerate(list):
print(b)
#Output result
AAA
BBB
CCC
DDD
・ A = (index number, element) ・ A [1] = index number
Extract only index number(1 variable)
list = ["AAA","BBB","CCC","DDD"]
for a in enumerate(list):
print(a[1])
#Output result
AAA
BBB
CCC
DDD
Both variables defined at the time of output (here, "a" and "b") are used.
Extract index number and element separately
list = ["AAA","BBB","CCC","DDD"]
for a,b in enumerate(list):
print(a,b)
#Output result
0 AAA
1 BBB
2 CCC
3 DDD
Output example using format method
list = ["AAA","BBB","CCC","DDD"]
for a,b in enumerate(list):
print("index number={:^4}element={:^8}".format(a,b))
#Output result
index number=0 element= AAA
index number=1 element= BBB
index number=2 elements= CCC
index number=3 elements= DDD
How to use the format method here
for a, b in enumerate(A, n):
└ "a": Variable to put the index number
└ "b": Variable to put the element
└ "A": array (or tuple)
└ "n": Initial value of index number (integer)
Any integer entered in "n" will be the initial value.
1 variable
list = ["AAA","BBB","CCC","DDD"]
for a in enumerate(list, 999):
print(a)
#Output result
(999, 'AAA')
(1000, 'BBB')
(1001, 'CCC')
(1002, 'DDD')
2 variables
list = ["AAA","BBB","CCC","DDD"]
for a, b in enumerate(list, 999):
print(a)
#Output result
999
1000
1001
1002
Minus can also be specified
list = ["AAA","BBB","CCC","DDD"]
for a, b in enumerate(list, -2):
print(a)
#Output result
-2
-1
0
1
Fraction (float) is an error
list = ["AAA","BBB","CCC","DDD"]
for a, b in enumerate(list, 999.9):
print(a)
#Output result
# TypeError: 'float' object cannot be interpreted as an integer
It is commonly used for lists, but it can also be used for tuples.
Click here for a description of tuples
Used for tuple
tuple = "AAA","BBB","CCC","DDD"
type(tuple)
#Output: tuple
for a in enumerate(tuple):
print(a)
#Output result
(0, 'AAA')
(1, 'BBB')
(2, 'CCC')
(3, 'DDD')
Recommended Posts