【问题标题】:Converting from list comprehension back to for loops从列表推导转换回 for 循环
【发布时间】:2019-04-05 21:32:43
【问题描述】:
     mins = X.min().values #array of min vals
     maxs = X.max().values #array of max vals   
     test = []
     test = {
            i: [np.random.randint(mins[t], maxs[t]) for t in range(len(mins))]
            for i in range(k)
        }

或者这个

for c in range(k):
     indices = [distances.index(row) for row in distances if row[-1] == c]         
     test[c] = X.iloc[indices].mean().values

是否可以将此列表推导转换回 python 中的 for 循环?我对 python 比较陌生,语法有问题。

【问题讨论】:

  • {i: ... for i in range(k) } 实际上是一个字典理解。但内部是一个列表。
  • 无关:[np.random.randint(min_val,max_val) for min_val,max_val in zip(mins,max)] .. 我鄙视索引 ....
  • 是否可以将其转换为 for 循环? @Jeppe

标签: python python-3.x list for-loop list-comprehension


【解决方案1】:

你基本上只是向后工作,所以首先你有:

代码 1

{i: [np.random.randint(mins[t], maxs[t]) for t in range(len(mins))] for i in range(k)}

所以第一步是for i in range(k) 生成你的密钥,然后是生成一个列表,或者:

result = {}
for i in range(k):
    temp = [] 
    # something to do with making a list
    result[i] = temp

然后再次向后工作,您有 for t in range(len(mins)) 只剩下 np.random.randint(mins[t], maxs[t])

所以:

result = {}
for i in range(k):
    temp = []
    for t in range(len(mins)):
        temp.append(np.random.randint(mins[t], maxs[t]))
    result[i] = temp

在完整的代码示例中是

import numpy as np
mins = [1, 2, 3, 4]
maxs = [5, 6, 7, 8]
k = 3
test = []
test = {
    i: [np.random.randint(mins[t], maxs[t]) for t in range(len(mins))] for i in range(k)
    }
print(test)

result = {}
for i in range(k):
    temp = []
    for t in range(len(mins)):
        temp.append(np.random.randint(mins[t], maxs[t]))
    result[i] = temp
print(result)

第一个是你的理解,第二个是 for 循环。结果:

{0: [3, 3, 3, 4], 1: [2, 4, 3, 6], 2: [3, 2, 3, 5]}
{0: [4, 3, 4, 4], 1: [3, 3, 4, 4], 2: [3, 2, 6, 4]}

你可以看到我们通过从右到左解构理解得到了相同的结果!

案例2

indices = [distances.index(row) for row in distances if row[-1] == c]         

可以用同样的方法分解。从最后面的for 开始,我们发现:

for row in distances if row[-1] == c

相当于:

for row in distances:
    if row[-1] == c
        #do something

我们知道所有内容都包含在一个列表中,因此添加一个列表并将“某物”附加到其中非常简单:

indices = []
for row in distances:
    if row[-1] == c:
        indices.append(distances.index(row))

请注意,我还没有测试过这个(而且根据给出的信息也无法测试)

如果我们把这个放到原来的代码块中

for c in range(k):
    indices = [distances.index(row) for row in distances if row[-1] == c]        
    test[c] = X.iloc[indices].mean().values

我们现在有

for c in range(k):
    indices = []
    for row in distances:
        if row[-1] == c:
            indices.append(distances.index(row))        
     test[c] = X.iloc[indices].mean().values

【讨论】:

  • 第二个呢?
  • @AdvaitKulkarni 第二个呢?我需要更多与您的问题相关的背景信息...
  • 转换这个:for c in range(k): indices = [distances.index(row) for row in distances if row[-1] == c] test[c] = X. iloc[indices].mean().values
  • test[c] = X.iloc[indices].... 是否超出了 for 行循环? @Reedinationer
  • @AdvaitKulkarni 这不是列表理解。这将是分开的
【解决方案2】:

给你:

test = {}
for i in range(k):
  test[i] = []
  for t in range(len(mins)):
    test[i].append(np.random.randint(mins[t], maxs[t]))

希望对你有帮助!

【讨论】:

  • 它说列表分配索引超出了 test[i] = [] 的范围
  • @AdvaitKulkarni 将test 初始化为test = {},它将起作用。
  • 知道了,谢谢@Jeppe。另一个也可以吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-19
  • 2020-12-18
  • 2021-10-15
  • 2021-10-22
  • 1970-01-01
相关资源
最近更新 更多