查找发生事件的 forloop 的值 Python
嘿伙计们,这非常令人困惑......
我试图通过以下方式找到数组的最小值:
for xpre in range(100): #used pre because I am using vapor pressures with some x molarity
xvalue=xarray[xpre]
for ppre in range(100): #same as xpre but vapor pressures for pure water, p
pvalue=parray[p]
d=math.fabs(xvalue-pvalue) #d represents the difference(due to vapor pressure lowering, a phenomenon in chemistry)
darray.append(d) #darray stores the differences
mini=min(darray) #mini is the minimum value in darray
darr=[] #this is to make way for a new set of floats
所有数组(xarr,parr,darr)都已经定义了,还有什么没有定义。他们每个都有 100 个浮点数
,所以我的问题是如何找到 pvap 和 xvap @ 找到 min(darr) ?
编辑 更改了一些变量名称并添加了变量描述,对不起大家
hey guys, this is very confusing...
i am trying to find the minimum of an array by:
for xpre in range(100): #used pre because I am using vapor pressures with some x molarity
xvalue=xarray[xpre]
for ppre in range(100): #same as xpre but vapor pressures for pure water, p
pvalue=parray[p]
d=math.fabs(xvalue-pvalue) #d represents the difference(due to vapor pressure lowering, a phenomenon in chemistry)
darray.append(d) #darray stores the differences
mini=min(darray) #mini is the minimum value in darray
darr=[] #this is to make way for a new set of floats
all the arrays (xarr,parr,darr)are already defined and what not. they have 100 floats each
so my question is how would I find the pvap and the xvap @ which min(darr) is found?
edit
have changed some variable names and added variable descriptions, sorry guys
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有几件事:
enumerate
list
,使用dict
并将dvp
值存储为键,使用xindex< /code> 和
pindex
变量作为值这是代码
解释
enumerate
函数允许您迭代列表并接收列表的索引物品。它是range(100)
的替代方案。请注意,我没有在索引xpre
、ppre
处获取值的行,这是因为enumerate
函数为我提供了两者索引和值作为元组。然而,最重要的变化是,您的
darr
不再是这样的列表:它现在是这样的字典:
所以现在,不再只存储
dvp
仅值,我还将索引存储到生成这些dvp
值的x
和p
中。现在,如果我想知道什么,例如,哪个x
和p
值产生dvp
值 43?我会这样做:现在 x 和 p 是有问题的值。
注意我个人会存储产生特定
dvp
的值,而不是这些值的索引。但你要求提供索引,所以我给了你答案。我假设您有理由想要处理这样的索引,但在 Python 中,当您以 Pythonic 方式编程时,通常您不会发现自己以这种方式处理索引。这是一种非常 C 的做事方式。A couple things:
enumerate
darr
being alist
, use adict
and store thedvp
values as keys, with thexindex
andpindex
variables as valuesHere's the code
Explanation
The
enumerate
function allows you to iterate over a list and also receive the index of the item. It is an alternative to yourrange(100)
. Notice that I don't have the line where I get the value at indexxpre
,ppre
, this is because theenumerate
function gives me both index and value as a tuple.The most important change, however, is that instead of your
darr
being a list like this:It is now a dictionary like this:
So now, instead of just storing the
dvp
values alone, I am also storing the indices intox
andp
which generated thosedvp
values. Now, if I want to know something, say, Whichx
andp
values produce thedvp
value of 43? I would do this:Now
x
andp
are the values in question.Note I personally would store the values which produced a particular
dvp
, and not the indices of those values. But you asked for the indices so I gave you that answer. I'm going to assume that you have a reason for wanting to handle indices like this, but in Python generally you do not find yourself handling indices in this way when you are programming in Pythonic manner. This is a very C way of doing things.编辑:这并没有回答OP的问题:
从右到左:
zip采用xpre和ppre并分别制作第一个,第二个,...元素的元组,如下所示:
enumerate enumerates添加仅从 0 开始向上计数来索引:
这会解压每个嵌套元组:
i 是 enumerate 生成的索引,a 和 b 是 xarr 和 parr 的元素。
这构建了一个由差异和索引组成的元组:
min(...) 之间的整个内容是一个生成器表达式。 min 然后找到这些值中的最小值,并且赋值将它们解包:
Edit: This doesn't answer the OP's question:
right to left:
zip takes xpre and ppre and makes a tuple of the 1st, 2nd, ... elements respectively, like so:
enumerate enumerates adds the index by just counting upwards from 0:
This unpacks each nestet tuple:
i is the index generated by enumerate, a and b are the elements of xarr and parr.
This builds a tuple consisting of a difference and the index:
The whole thing inbetween the min(...) is a generator expression. min then finds the minimal value in these values, and the assignment unpacks them: