import numpy as np
data1 = np.array([1,2,np.nan,4,5,6,7],dtype=float)
data2 = np.array([11,np.nan,9,4,5,6,71],dtype=float)
data3 = np.array([17,np.nan,13,4,15,6,17],dtype=float)
result1 = data1/data2
result2 = data1/data3
result3 = data3/data2
对于每个结果,我想将np.nan值转换为-9.0:
result1 = np.where(np.isnan(result1),-9.0,result1)
result2 = np.where(np.isnan(result2),-9.0,result2)
result3 = np.where(np.isnan(result3),-9.0,result3)
但是在我真正的问题中,我有数百个结果数组(result1,result2 ... result100)。所以,我不能像上面那样手动完成。所以,把它们放进小组:
my_groups = ['result1', 'result2', 'result3']
如何将字符串(my_groups的元素)更改为相应的变量?
for i in my_groups:
out[i] = np.where(np.isnan(i),-9.0,i)
等待解决问题的良好做法。
答案 0 :(得分:2)
要迭代模块范围中的变量,可以使用globals(),它包含模块中的所有变量。 globals()返回带有结构的字典:{" var_name":var,...},即:
x = 10
y = 10
print globals()
"服务"模块变量我们的x
和y
将在那里:
{"x": 10, "y": 10, ...}
至于你的例子:
import numpy as np
data1 = np.array([1,2,np.nan,4,5,6,7],dtype=float)
data2 = np.array([11,np.nan,9,4,5,6,71],dtype=float)
data3 = np.array([17,np.nan,13,4,15,6,17],dtype=float)
result1 = data1/data2
result2 = data1/data3
result3 = data3/data2
建议:
# It necessary to convert globals().keys() to list otherwise
# exception will be raised that we change dictionary (owner new variable var_name)
# during iterations.
for var_name in list(globals().keys()):
if var_name.startswith("result"):
value = globals()[var_name]
globals()[var_name] = np.where(np.isnan(value),-9.0,value)
print result1
print result2
print result3
结果:
[ 0.09090909 -9. -9. 1. 1. 1.
0.09859155]
[ 0.05882353 -9. -9. 1. 0.33333333 1.
0.41176471]
[ 1.54545455 -9. 1.44444444 1. 3. 1.
0.23943662]
<强>更新强>
但我应该注意,最佳做法是将您的result1
,result2
,... result100
,...变量放入集合中(我认为最合适的是< strong> list )然后迭代它。上面的建议可以是“不错”#34;如果您已经有一个包含这些已定义变量的巨大Python文件(resultX
)。
答案 1 :(得分:1)
像这样重写你的整个代码。
data = np.empty(shape=(3, 7), dtype=float)
result = np.empty(shape=(3, 7), dtype=float)
data[0] = np.array([1,2,np.nan,4,5,6,7],dtype=float)
data[1] = np.array([11,np.nan,9,4,5,6,71],dtype=float)
data[2] = np.array([17,np.nan,13,4,15,6,17],dtype=float)
result[0] = data[0]/data[1]
result[1] = data[0]/data[2]
result[2] = data[2]/data[1]
result = np.where(np.isnan(result),-9.0,result)
所以... 全部替换
data1, data2, data3,,,data100
result1, result2, result3,,,result100
到
data[0], data[1], data[2],,,data[99]
result[0], result[1], result[2],,,result[99]
以下是替换所有这些的代码。
source_code = '''
Paste your source code here...
'''
import re
def replace_data_num(r):
return "data[" + str(int(r.group(1)) - 1) + "]"
def replace_result_num(r):
return "result[" + str(int(r.group(1)) - 1) + "]"
source_code = re.sub(r'data(\d+)', replace_data_num, source_code)
source_code = re.sub(r'result(\d+)', replace_result_num, source_code)
print source_code
答案 2 :(得分:0)
您可以为这些变量指定值&#39;使用字典:
>>> string = 'result1'
>>> myobj = {}
>>> myobj[string] = 45
>>> myobj['result1']
45
>>>
回答你的问题:
>>> array = ['result1', 'result2', 'result3', 'result4', 'result5', 'result6', 'result7', 'result8', 'result9', 'result10']
>>> myobj = {}
>>> values = [5, 2, 87, 56, 23, 5, 99, 2, 24, 15] >>> for k in range(len(array)):
... myobj[array[k]] = values[k]
...
>>> myobj['result2']
2
>>> myobj['result6']
5
>>> myobj['result7']
99
>>>