如何从方法中获取赋值?

问题描述

我想在设置某个字段的类中收集方法,例如 import numpy as np import itertools from collections import Counter import pandas as pd bag = np.hstack(( np.repeat(0,80),np.repeat(1,21),np.repeat(3,5),np.repeat(7,1) )) #107*106*105*104*103*102*101*100*99*98 #Out[176]: 127506499163211168000 #Permutations ##Need to reduce the number to sample from without changing the number of possible combinations reduced_bag = np.hstack(( np.repeat(0,10),## 0 can be chosen all 10 times np.repeat(1,## 1 can be chosen all 10 times np.repeat(3,## 3 can be chosen up to 5 times np.repeat(7,1) ## 7 can be chosen once )) ## There are 96 unique combinations number_unique_combinations = len(set(list(itertools.combinations(reduced_bag,10)))) ### sorted list of all combinations unique_combinations = sorted(list(set(list(itertools.combinations(reduced_bag,10))))) ### sum of each unique combination sums_list = [sum(uc) for uc in unique_combinations] ### probability for each unique combination probability_dict = {0:80,1:21,3:5,7:1} ##Dictionary to refer to n = 107 ##Number in the bag probability_list = [] ##This part is VERY slow to run because of the itertools.permutations for x in unique_combinations: print(x) p = 1 ##Start with the probability again n = 107 ##Start with a full bag for each combination count_x = Counter(x) for i in x: i_left = probability_dict[i] - (Counter(x)[i] - count_x[i]) ##Number of that type left in bag p *= i_left/n ##Multiply the probability n = n-1 # non replacement count_x[i] = count_x[i] - 1 ##Reduce the number in the bag p *= len(set(list(itertools.permutations(x,10)))) ##Multiply by the number of permutations per combination probability_list.append(p) ##sum(probability_list) ## Has a rounding error ##Out[57]: 1.0000000000000002 ## ##Put the combinations into dataframe ar = np.array((unique_combinations,sums_list,probability_list)) df = pd.DataFrame(ar).T ##Name the columns df.columns = ["combination","sum","probability"] ## probability that sum is >= 6 df[df["sum"] >= 6]['probability'].sum() ## 0.24139909236232826 ## probability that sum is == 6 df[df["sum"] == 6]['probability'].sum() ## 0.06756408790812335 。 我知道如何获取方法的 int i = 2;

Statement

assignments 似乎是表达式,我不知道如何找到它们。如何在方法中获取(赋值)表达式?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)