我有一个字典(lengths_data
),我使用numpy计算不同的统计参数,然后将其存储在新字典(stats_
)中。后来我在一个新函数中使用了这个对象,但是当我在函数中打印这个对象时,我得到了原始字典(lengths_data
)。
这是我的代码:
from numpy import std
from numpy import mean
from numpy import percentile
lengths_data = {}
lengths_data["Value"]=[1742, 102, 66, 30, 30, 30, 66, 30, 1473, 3798, 30, 30, 30, 30, 66, 30, 1473, 14027, 1126, 1004, 131, 250, 426, 287, 891, 4228, 463, 410, 1807, 4067, 393, 1993, 1468, 1883, 55, 3555, 7158, 3889, 1431, 131, 378, 4596, 68, 807, 757, 2042, 160, 815, 5922, 2785, 3598, 1811, 286, 540, 2407, 1414, 6873, 734, 289, 3200, 2097, 3254, 300, 1175, 3587, 2243, 3188, 4480, 684, 7155, 3070, 1811, 1724, 2218, 4928, 2874, 3394, 863, 3333, 503, 864, 1109, 794, 2480, 1749, 3367, 679, 1698, 1862, 1068, 101, 3157, 2570, 2090, 530, 5913, 535, 1708, 912, 513, 1392, 1783, 1401, 1803, 4303, 850, 134, 5234, 991, 3200, 213, 625, 718, 2315, 5230, 3994, 139, 3631, 1375, 2064, 139, 534, 2989, 1168, 3296, 3138, 4655, 1255, 281, 1669, 390, 1007, 2137, 269, 129, 2681, 1282, 716, 739, 1241, 315, 2603, 1397, 2263, 402, 2572, 1086, 1308, 9293, 810, 2486, 316, 495, 2234, 588, 483, 3949, 1481, 3266, 1301, 346, 2227, 143, 1971, 1095, 7988, 2880, 1299, 1638, 2545, 1033, 5842, 10395, 4694, 4796, 257, 6724, 513, 8380, 260, 1756, 1158, 3006, 2402, 1233, 478, 3492, 2283, 323, 1886, 167, 2264, 306, 1135, 926, 164, 3, 1031, 1307, 624, 2103, 988, 1942, 528, 928, 41, 232, 3150, 2905, 1420, 488, 16, 13, 238, 217, 559, 1367, 2790, 951, 1069, 18, 1435, 2470, 4508, 1379, 474, 692, 819, 1186, 305, 206, 4995, 2622, 60, 1041, 186, 1291, 937, 1400, 1452, 1267, 3819, 2072, 2697, 472, 735, 2237, 563, 585, 361, 2840, 658, 1948, 271, 2432, 585, 361, 2840, 658, 1948, 271, 2790, 1032, 492, 2110, 1031, 492, 749, 9, 504, 1171, 202, 3671, 9, 1682, 1032, 492, 2110, 1032, 492, 1501, 3030, 3647, 1019, 6491, 476, 1245, 2096, 1419, 359, 3110, 293, 1507, 4626, 4341, 1099, 1549, 6719, 1838, 358, 8, 2828, 379, 1129, 1168, 1295, 519, 649, 2164, 1554, 4318, 1646, 1581, 2653, 577, 4982, 1881, 1278, 2455, 1695, 4060, 3043, 380, 4285, 1573, 2107, 2103, 1666, 2568, 696, 170, 9, 3876, 1171, 504, 9, 2217, 816, 799, 4139, 183, 27, 65, 2007, 875, 1568, 2882, 443, 1089, 2080, 762, 392, 522, 2997, 4750, 759, 3255, 3690, 2765, 81, 276, 288, 54, 72, 837, 261, 2179, 1061, 3426, 415, 3838, 9, 3670, 202, 1171, 504, 9, 287, 740, 3594, 4287, 871, 214, 805, 6271, 5002, 38, 679, 2111, 1545, 1615, 5388, 224, 1683, 665, 406, 102, 1431, 1932, 2676, 209, 3565, 641, 1367, 3027, 241, 505, 54, 3177, 3066, 1061, 5175, 58, 5485, 2327, 132, 2597, 9, 986, 1373, 114, 3730, 510, 504, 1760, 1169, 1059, 1141, 955, 781, 1386, 1345, 1188, 3597, 1323, 768, 433, 1786, 462, 394, 2314, 3342, 1547, 1841, 78, 2323, 745, 119, 798, 1040, 774, 472, 542, 46, 2760, 1164, 606, 5559, 310, 4431, 980, 1806, 147, 431, 1120, 639, 1387, 3846, 508, 239, 1409, 447, 4831, 1384, 3496, 2227, 2063, 57, 483, 337, 7793, 786, 366, 4831, 1436, 152, 1632, 1980, 301, 1095, 3654, 610, 402, 2954, 539, 2428, 1386, 3229, 1471, 155, 1995, 1101, 3850, 2283, 1635, 37, 3282, 326, 163, 161, 94, 1236, 1071, 383, 499, 876, 22, 1077, 6765, 6385, 243, 2663, 1901, 3918, 21, 408, 226, 554, 191, 2430, 6269, 1297, 2582, 180, 487, 505, 2531, 3590, 4397, 2665, 687, 248, 6739, 1368, 2096, 1288, 1896, 1246, 9, 5096, 504, 9, 3276, 232, 5012, 360, 1332, 6101, 219, 214, 191, 2251, 1935, 180, 1271, 3001, 1003, 957, 2036, 9, 5090, 9, 3221, 475, 3290, 3045, 1393, 312, 1316, 85, 757, 1603, 823, 4534, 1701, 854, 826, 996, 2738, 3048, 2104, 1800, 282, 378, 1953, 1592, 2110, 820, 88, 3678, 3525, 1870, 4432, 7824, 934, 38, 955, 3398, 2021, 9, 1622, 1639, 1437, 3989, 985, 5514, 249, 1473, 30, 66, 30, 30, 30, 30, 4671, 249, 1473, 30, 66, 30, 30, 30, 30, 1581]
stats_ = {}
stats_["Value"]=[1694, 1737, 411, 2422, 3, 14027]
def lenstatistics(lengths_data):
stats_ = dict.fromkeys(lengths_data)
for combin in lengths_data.keys():
mean_ = int(mean(lengths_data[combin]))
std_ = int(std(lengths_data[combin]))
p25_ = int(percentile(lengths_data[combin], 25))
p75_ = int(percentile(lengths_data[combin], 75))
min_ = min(lengths_data[combin])
max_ = max(lengths_data[combin])
vect = [mean_, std_, p25_, p75_, min_, max_]
stats_[combin] = vect[:]
return stats_
stats_ = lenstatistics(lengths_data)
# If I do print stats_ I get the values of the mean the standard deviation, and the other expected values...
print stats_
def enzymes2(stats_, length):
print stats_
# If I do print stats_ I get the original dictionary lengths_data, with all the data used to create the mean, the standard deviation..
std_ = 0
for combin in Dict1.keys():
if length in range((stats_[combin][0] - stats_[combin][1]), (stats_[combin][0]+Stats[combin][1])):
if std_ == 0:
std_ = stats_[combin][1]
combine = combin
if std_ > stats_[combin][1]:
std_ = stats_[combin][1]
combine = combin
else:
pass
return stats_[combine]
Selection = enzymes2(lengths_data, 1800)
我该如何解决这个问题?或者我如何在第二个函数(enzymes2
)的末尾获得stats_
的预期(实际)值(不再计算)?
我对此问题的理解是stats_
的引用仍然指向lengths_data
,但只有__str__
方法被更改,而其他函数内部被刷新,但我是不确定这个......
答案 0 :(得分:2)
我没有看到你所描述的行为有任何问题:在enzy2方法中,你打印统计数据,它被重新定义为方法的第一个参数。这意味着在方法的上下文中,Stats是方法的第一个参数,而不是全局变量。当你最后调用enzymes2方法时,你传递Dict作为第一个参数,所以它打印Dict ......
如果你想在整个文件的上下文中使用Stats作为全局变量,那么就不要在方法的上下文中重新定义它...
在您的示例中,为了获得预期结果,我认为您可能只需用
替换最后一行Selection = enzymes2(Stats, 1800)
因为您之前调用的Stats = lenstatistics(Dict)已经计算出Stats变量