如何从参数自动创建嵌套类
我想首先提供一些上下文。
我有一个看起来像:
ID Q1 Q2 Q3
A Y N N
A N N Y
A N N N
B Y Y N
C N N Y
C N N N
D N N N
D N N Y
D N Y N
E N N N
E N Y N
E N N N
有4个项目:A,B,C和D。基于值是y还是n的项目(一个y使拆分y)。例如,假设第一个拆分是由Q1
完成的,然后:A在split y
中与B一起进行,并且C在splitn 。我们可以使用
Q2
进一步拆分这两个;然后,A和B分别将在n
和y
拆分中。使用Q2
,C将转到n
拆分,E和D将进行y
。然后,Q3
仅需要创建D和E的拆分,因为所有其他项目都是单独的。使用Q3
然后d转到y
,E转到n
。
遵循此过程会生成这样的树结构:
Initial
/ \ (Using Q1)
N Y N: C,D,E -- Y: A,B
/ \ / \ (Using Q2)
N Y N Y NN: C - NY: D,E -- YN: A - YY: B
/ \ (Using Q3)
N Y NYN: E - NYY: D
因此,我想要的是创建一个类,该类可以使用列自动将项目划分,直到它们被挑出为止。这需要嵌套的类或属性。我想象all
之类的东西,然后all.q1n
和all.q1y
,然后all.q1n.q2y
,等等。在最后(树叶),我想计算该项目的实例。例如。 all.q1n.q2n.values = 2
,因为它们上有两个行。
我不知道Python是否可以做到这一点,如果是,我不知道该怎么做。我一直在搜索,但还没有找到我可以使用的任何东西。如果有人能告诉我Python的可行性,如果是,如果是的,我会很高兴的是,如果他们查明可以用来实现此目的的一些资源(特殊功能装饰器)。我没想到有人会为此编写代码(尽管我不会生气);相反,我只想知道要使用什么然后做。如果我能够做到这一点,我将在此处发布代码。
I'd like to first provide a little bit of context.
I have a dataframe that looks like:
ID Q1 Q2 Q3
A Y N N
A N N Y
A N N N
B Y Y N
C N N Y
C N N N
D N N N
D N N Y
D N Y N
E N N N
E N Y N
E N N N
So, there are 4 items: A, B, C, and D. I'd like to create a class with nested classes (or attributes) that can go column by column and creates splits of the items based on whether the value is Y or N (one Y makes the split Y). For example, let's say the first split is done by Q1
, then: A goes with B in the split Y
, and C goes with D and E in the split N
. We can further split these two using Q2
; then A and B would be in the N
and Y
split respectively. With Q2
, C would go to the N
split, and E and D would go the Y
. Then, Q3
is only needed to create a split of D and E because all the other items are alone. Using Q3
then D goes to Y
and E goes to N
.
Following this procedure generates a tree structure like this:
Initial
/ \ (Using Q1)
N Y N: C,D,E -- Y: A,B
/ \ / \ (Using Q2)
N Y N Y NN: C - NY: D,E -- YN: A - YY: B
/ \ (Using Q3)
N Y NYN: E - NYY: D
So, what I would like is to create a class that automatically divides the items using the columns until they are singled out. This requires nested classes or attributes. I imagine something like all
, then all.Q1N
and all.Q1Y
, then all.Q1N.Q2Y
, and so on. At the very end (the tree leaves), I want to count how many instance of the item there are. For example. all.Q1N.Q2N.values = 2
since there are two rows with C on them.
I have no idea if this is possible with python, and if it is, I have no idea how to do it. I've been searching, but haven't quite found anything that I can use. I'd appreciate if someone can tell me how feasible this is in python, and if it is, if they pinpoint some resource (special function decorator) that can be used to accomplish this. I am not expecting someone to write the code for this (although I wouldn't be angry if someone did); instead I just want to know what to use and then do it. I'll post the code here if I can manage to do it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
'N' 上的可用项进行分区,然后在非单例组上调用该函数:
您可以使用递归函数来构建树,方法是对
'Y'
和:
You can use a recursive function to build the tree by partitioning your available items on
'Y'
and'N'
, and then calling the function on non-singleton groups:Output: