试图了解Python中的[]

我学习了Python,然后看到了以下代码。该代码涉及:创建一个数组,其中包含100个介于0和1之间的随机值。此外,确定该条目的值是其索引可被2除的所有条目中的最大值。

var1 = numpy.random.uniform(0, 1, 100)

var2 = numpy.arange(0, 100, 2)

var3 = var1[var2]

print(var3[numpy.argmin(var3)])

However, I do not entirly understand line 2 and 3 of the code. Can someone clarify this? Why do we create line 2? And what does var1[var2] mean?

先感谢您。

评论
深蓝Xx
深蓝Xx

In this case, the brackets refer to an index within the list var1. For instance, if I have the list nums = [0, 1, 5, 4, 2], print(nums[2]) would yield an output of 5.

点赞
评论