简体   繁体   English

如何遍历列表的索引?

[英]How can I iterate through the indices of the list?

I am trying to compute three lists for example例如,我正在尝试计算三个列表

M =[[3, 0, 2, -8, -8], [5, 3, 2, 2, 3], [2, 5, 2, 1, 4]]

I want to first sum every first value in the list x=3+5+2我想先求和列表中的每个第一个值x=3+5+2

and the rest values in all the lists add them together as y并且所有列表中的其余值将它们加在一起作为y

finally the difference of absolute value of xy which is 10 - 8最后difference of absolute value of xy which is 10 - 8

and again sum every first two values in the list x=3+5+2+0+3+5并再次求和列表中的前两个值x=3+5+2+0+3+5

and the rest values in all the lists add them together as y并且所有列表中的其余值将它们加在一起作为y

finally the difference of absolute value of xy which is 18 - 0最后difference of absolute value of xy which is 18 - 0

again the same process until the last value in the list再次相同的过程,直到列表中的最后一个值

lastly If difference is the minimum stored value, set the minimum value to difference and the minimum index to index, and finally return the slices found by getting the minimum value最后如果差异是存储的最小值,则将最小值设置为差异,将最小索引设置为索引,最后返回通过获取最小值找到的切片

for this example slices found is list1=[3,5,2] list2=[0,2,-8,-8,3,2,2,3,2,5,2,1,4] i calculated it manually but i couldn't implement it对于这个例子,找到的切片是list1=[3,5,2] list2=[0,2,-8,-8,3,2,2,3,2,5,2,1,4]我手动计算的但我无法实现它

 def Vertical_UpperMatrix(M):
        ds=range(5)
        diffList = [(abs(sum(ds[:i]) - sum(ds[i:]))) for i in range(3)]
        return diffList.index(min(diffList))
 M=[[3, 0, 2, -8, -8], [5, 3, 2, 2, 3], [2, 5, 2, 1, 4]]

I am in EST timezone, so at the time this answer is written, it is 2:40am.我在 EST 时区,所以在撰写此答案时,是凌晨 2:40。 But I am taking time to answer, basically to repay your willingness to improve.但我抽时间回答,基本上是为了报答你愿意改进的。

We will use a library called numpy .我们将使用一个名为numpy的库。 If you don't have numpy on your machine, please Google-Search how you can install it on your OS.如果你的机器上没有numpy ,请谷歌搜索如何在你的操作系统上安装它。 It is important for your career onwards, and you will see why.这对您以后的职业生涯很重要,您会明白为什么。

First, create your numpy array:首先,创建您的numpy数组:

>>> import numpy as np
>>> l = np.array([[3, 0, 2, -8, -8], [5, 3, 2, 2, 3], [2, 5, 2, 1, 4]])
>>> l
array([[ 3,  0,  2, -8, -8],
       [ 5,  3,  2,  2,  3],
       [ 2,  5,  2,  1,  4]])

Now you can access the first column by doing现在您可以通过执行访问第一列

>>> l[:, 0]
array([3, 5, 2])

The first row can be accessed by第一行可以通过

>>> l[0, :]
array([ 3,  0,  2, -8, -8])

The first row from the second element to the fourth element by第一行从第二个元素到第四个元素

>>> l[0, 1:4]
array([ 0,  2, -8])

Based on all the features above, the target function you want is basically this:基于以上所有特性,你想要的目标函数基本上是这样的:

>>> d = 1
>>> np.abs(np.sum(l[:, 0:d]) - np.sum(l[:, d:]))
2

d is your dividing index. d是你的分割指数。 Now you can iterate over d in a for loop.现在您可以在 for 循环中遍历d

I suggest you learn about at least numpy and scipy if you plan to carry on your career with Python .如果你打算继续你的Python职业生涯,我建议你至少学习numpyscipy

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM