编写一个 Python示例来访问列表的索引和值。在 Python 中,列表项是基于索引值组织的。因此,我们可以使用索引来访问每个列表项或值。在这个 Python 示例中,for 循环 range (for i in range(len(orgList))迭代列表项,打印雄蕊在每个索引位置打印值。
# Access List Index and Values
orgList = [10, 20, 30, 40, 50]
print("List Index and Values are")
for i in range(len(orgList)):
print("Index Number = ", i, " and Value = ", orgList[i])
# Access List Index and Values
orgList = [2, 4, 6, 8, 10, 12]
print("List Index and Values are")
for index, value in enumerate(orgList):
print("Index Number = ", index, " and Value = ", value)
Python 使用枚举输出访问列表索引和值
List Index and Values are
Index Number = 0 and Value = 2
Index Number = 1 and Value = 4
Index Number = 2 and Value = 6
Index Number = 3 and Value = 8
Index Number = 4 and Value = 10
Index Number = 5 and Value = 12
在这个 Python 示例中,我们使用列表理解来访问和返回列表项或值以及相应的索引位置。
# Access List Index and Values
orgList = [13, 43, 56, 78, 65]
list2 = ([list((i, orgList[i])) for i in range(len(orgList))])
print("List Index and Values are")
print(list2)
list3 = ([(i, orgList[i]) for i in range(len(orgList))])
print("List Index and Values are")
print(list3)
Python 使用列表理解输出来访问列表索引和值
List Index and Values are
[[0, 13], [1, 43], [2, 56], [3, 78], [4, 65]]
List Index and Values are
[(0, 13), (1, 43), (2, 56), (3, 78), (4, 65)]
这个 Python示例使用 zip 、 range 和 len 函数来返回列表索引和值。
# Access List Index and Values
orgList = [22, 44, 66, 88, 122]
print("List Index and Values are")
for index, value in zip(range(len(orgList)), orgList):
print("Index Number = ", index, " and Value = ", value)
Python 使用 zip、range 和 len 函数输出来访问列表值和索引号
List Index and Values are
Index Number = 0 and Value = 22
Index Number = 1 and Value = 44
Index Number = 2 and Value = 66
Index Number = 3 and Value = 88
Index Number = 4 and Value = 122
标签: seo