查找发生事件的 forloop 的值 Python

发布于 2024-09-14 23:57:27 字数 782 浏览 8 评论 0原文

嘿伙计们,这非常令人困惑......

我试图通过以下方式找到数组的最小值:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

§对你不离不弃 2024-09-21 23:57:27

有几件事:

  1. 尝试 enumerate
  2. 而不是 < code>darr 是一个 list,使用 dict 并将 dvp 值存储为键,使用 xindex< /code> 和 pindex 变量作为值

这是代码

for xindex, xvalue in enumerate(xarr):
  darr = {}
  for pindex, pvalue in enumerate(parr):
    dvp = math.fabs(xvalue - pvalue)
    darr[dvp] = {'xindex': xindex, 'pindex': pindex}
  mini = min(darr.keys())
  minix = darr[mini]['xindex']
  minip = darr[mini]['pindex']
  minindex = darr.keys().index(mini)


  print "minimum_index> {0}, is the difference of xarr[{1}] and parr[{2}]".format(minindex, minix, minip)
  darr.clear()

解释

enumerate 函数允许您迭代列表并接收列表的索引物品。它是 range(100) 的替代方案。请注意,我没有在索引 xpreppre 处获取值的行,这是因为 enumerate 函数为我提供了两者索引和值作为元组。

然而,最重要的变化是,您的 darr 不再是这样的列表:

[130, 18, 42, 37 ...]

它现在是这样的字典:

{
  130: {'xindex': 1, 'pindex': 4},
  18: {'xindex': 1, 'pindex': 6},
  43: {'xindex': 1, 'pindex': 9},
  ...
}

所以现在,不再只存储 dvp仅值,我还将索引存储到生成这些 dvp 值的 xp 中。现在,如果我想知道什么,例如,哪个 xp 值产生 dvp 值 43?我会这样做:

xindex = darr[43]['xindex']
pindex = darr[43]['pindex']
x = xarr[xindex]
p = parr[pindex]

现在 x 和 p 是有问题的值。

注意我个人会存储产生特定dvp的值,而不是这些值的索引。但你要求提供索引,所以我给了你答案。我假设您有理由想要处理这样的索引,但在 Python 中,当您以 Pythonic 方式编程时,通常您不会发现自己以这种方式处理索引。这是一种非常 C 的做事方式。

A couple things:

  1. Try enumerate
  2. Instead of darr being a list, use a dict and store the dvp values as keys, with the xindex and pindex variables as values

Here's the code

for xindex, xvalue in enumerate(xarr):
  darr = {}
  for pindex, pvalue in enumerate(parr):
    dvp = math.fabs(xvalue - pvalue)
    darr[dvp] = {'xindex': xindex, 'pindex': pindex}
  mini = min(darr.keys())
  minix = darr[mini]['xindex']
  minip = darr[mini]['pindex']
  minindex = darr.keys().index(mini)


  print "minimum_index> {0}, is the difference of xarr[{1}] and parr[{2}]".format(minindex, minix, minip)
  darr.clear()

Explanation

The enumerate function allows you to iterate over a list and also receive the index of the item. It is an alternative to your range(100). Notice that I don't have the line where I get the value at index xpre, ppre, this is because the enumerate 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:

[130, 18, 42, 37 ...]

It is now a dictionary like this:

{
  130: {'xindex': 1, 'pindex': 4},
  18: {'xindex': 1, 'pindex': 6},
  43: {'xindex': 1, 'pindex': 9},
  ...
}

So now, instead of just storing the dvp values alone, I am also storing the indices into x and p which generated those dvp values. Now, if I want to know something, say, Which x and p values produce the dvp value of 43? I would do this:

xindex = darr[43]['xindex']
pindex = darr[43]['pindex']
x = xarr[xindex]
p = parr[pindex]

Now x and p 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.

把昨日还给我 2024-09-21 23:57:27

编辑:这并没有回答OP的问题:

min_diff, min_idx = min((math.fabs(a - b), i) for i, (a, b) in enumerate(zip(xpre, ppre)

从右到左:

zip采用xpre和ppre并分别制作第一个,第二个,...元素的元组,如下所示:

[ (xpre[0],ppre[0]) , (xpre[1],ppre[1]) , ... ]

enumerate enumerates添加仅从 0 开始向上计数来索引:

[ (0 , (xpre[0],ppre[0]) ) , (1 , (xpre[1],ppre[1]) ) , ... ]

这会解压每个嵌套元组:

for i, (a, b) in ...

i 是 enumerate 生成的索引,a 和 b 是 xarr 和 parr 的元素。

这构建了一个由差异和索引组成的元组:

(math.fabs(a - b), i)

min(...) 之间的整个内容是一个生成器表达式。 min 然后找到这些值中的最小值,并且赋值将它们解包:

min_diff, min_idx = min(...)

Edit: This doesn't answer the OP's question:

min_diff, min_idx = min((math.fabs(a - b), i) for i, (a, b) in enumerate(zip(xpre, ppre)

right to left:

zip takes xpre and ppre and makes a tuple of the 1st, 2nd, ... elements respectively, like so:

[ (xpre[0],ppre[0]) , (xpre[1],ppre[1]) , ... ]

enumerate enumerates adds the index by just counting upwards from 0:

[ (0 , (xpre[0],ppre[0]) ) , (1 , (xpre[1],ppre[1]) ) , ... ]

This unpacks each nestet tuple:

for i, (a, b) in ...

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:

(math.fabs(a - b), i)

The whole thing inbetween the min(...) is a generator expression. min then finds the minimal value in these values, and the assignment unpacks them:

min_diff, min_idx = min(...)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文