For example,
names=[a,b,c,d]
Assume there is a list with the list name
a=[1,2,3]
b=[4,5,6]...
when a separate index is provided.
I would like to get the letters a, b, c, d in the for statement and get each element of the index in the list corresponding to the name, but if I turn it around as below, the list name itself will be displayed like #.
How do I improve to index 1, 2, 3...
for index, name in enumrate (names):
for i in enumrate (name):
print(name[i])
#a
#b
#c
#d
enumerate is an embedded function that enumerates indexes and values.
If you simply want to get an element, you can get it by writing in followed by the name.
If you simply want to index, you can use the range function to get an index starting with zero.
Even if I rewrite enumrate
in the questionnaire to enumerate
, the variable name does not appear, but if you have any additional information, please write it down.
a=[1,2,3]
b = [4,5,6]
c=[7,8,9]
d = [10,11,12]
names = [a, b, c, d ]
for index, name in enumerate (names):
# for i in enumerate (name)—i in returns (index, numeric)
for i in enumerate (name):
print(i)#(0,1), (1,2), (2,3)...
# I only want to get elements
For element in name:
print(element)
# I just want to get the index
for i in range (len(name)) :
print("The value of index {} is {}.format(i,name[i]))
© 2024 OneMinuteCode. All rights reserved.