简体   繁体   English

python策略覆盖for循环中的全局变量

[英]Pythonic strategy to overwrite global variables in a for loop

EDIT2: 编辑2:

Thanks for your help, problem solved, went with an intermediate approach: 感谢您的帮助,解决了问题,并采用了一种中间方法:

在此处输入图片说明

Will accept the answer when it becomes eligible to be accepted! 有资格被接受时将接受答案!


EDIT: 编辑:

I was asked for simpler variables, okay, I will try :) 我被要求提供更简单的变量,好的,我会尝试:)

Assume I want to have data in separate variables a , b , c , and d , how would I assign data to them in a for loop? 假设我要在单独的变量abcd中包含数据,我将如何在for循环中为它们分配数据?

An approach like this: 这样的方法:

a, b, c, d = [], [], [], []

for var,data in zip([a,b,c,d], some_data_array):
    var = #data that comes from some operation on data array

wouldn't work, the variables a, b, c, d would still be [] , since they are in the global scope. 将不起作用,变量a, b, c, d仍为[] ,因为它们在全局范围内。


I have a training dataset train_set (numpy array) that I want to reduce into smaller training datasets (90%, 80%, 70%, etc.). 我有一个训练数据集train_set (numpy数组),我想将其缩减为较小的训练数据集(90%,80%,70%等)。 I don't want to copy & paste the procedure 我不想复制并粘贴过程

train_90perc = train_set[np.random.choice(train_set.shape[0],\
        train_set.shape[0] * 0.9, replace=False),:]

and thought I could do it with a for loop. 并认为我可以使用for循环来做到这一点。 But how? 但是如何? My initial approach (below) has the problem that it wouldn't modify the variables in the global scope. 我最初的方法(如下)有一个问题,即它不会在全局范围内修改变量。 So when I would print eg, train_80perc after the for loop, it would still be [] . 因此,当我在for循环之后打印例如train_80perc ,它仍然是[] PS: The first 2 lines are to initialize the variables, is there also a way I can get around that (since it looks kind of ugly). PS:前两行用于初始化变量,还有一种方法可以解决该问题(因为它看起来很丑)。

train_100perc, train_90perc, train_80perc, train_70perc, train_60perc, train_50perc,\
train_40perc, train_30perc, train_20perc, train_10perc = [], [], [], [], [], [], [], [], [], []

for train,size in zip([train_100perc, train_90perc, train_80perc,\
                       train_70perc, train_60perc, train_50perc,\
                       train_40perc, train_30perc, train_20perc, train_10perc],\
                       [p for p in reversed([i/10 for i in range(1,11)])]
                     ):
    #global train
    train = train_set[np.random.choice(train_set.shape[0],\
    train_set.shape[0] * size, replace=False),:]

Thanks for your help! 谢谢你的帮助!

UPDATE 更新

Don't assign some data to var . 不要为var分配一些数据。 because it's just reference. 因为这只是参考。

a, b, c, d = [], [], [], []

for var,data in zip([a,b,c,d], some_data_array):
    # call var.extend or var.append
    # var = some_value # don't do this. because it's just reference.
    var.extend(some_list)

Do you have to generate train_100perc ... ? 您是否必须生成train_100perc ...?

My suggestion is 我的建议是

subset = {}
for percent in np.arange(10, 100, 10):
    np.random.shuffle(train_set) # shuffle in-place
    subset[percent] = train_set[:len(train_set)*percent*0.01].copy() # selection of training data

np.arange(10, 100, 10) can vary. np.arange(10, 100, 10)可以变化。

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

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