如何从参数自动创建嵌套类

发布于 2025-01-18 13:12:49 字数 1406 浏览 1 评论 0原文

我想首先提供一些上下文。

我有一个看起来像:

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分别将在ny拆分中。使用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.q1nall.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

指尖上得阳光 2025-01-25 13:12:49

'N' 上的可用项进行分区,然后在非单例组上调用该函数:

from collections import defaultdict
def to_tree(vals, d, l = 0): # signature stores a running level counter (l)
   t = defaultdict(list)
   for i in vals:
      # check if (d) stores a 'Y' value for the given (i) value at current level (l)
      t[['N', 'Y'][any(j[l] == 'Y' for j in d[i])]].append(i) #append (i) to 'Y' partition of above condition is met, else append to 'N'
   # apply recursion to partitions if the partition contains more than one value
   return {a:b[0] if len(b) == 1 else to_tree(b, d, l + 1) for a, b in t.items()}    

import json
data = [['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']]
d = defaultdict(list)
for a, *b in data:
    d[a].append(b)

print(json.dumps(to_tree([*d], d), indent=4))

您可以使用递归函数来构建树,方法是对 'Y':

{
    "Y": {
        "N": "A",
        "Y": "B"
    },
    "N": {
        "N": "C",
        "Y": {
            "Y": "D",
            "N": "E"
        }
    }
}

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:

from collections import defaultdict
def to_tree(vals, d, l = 0): # signature stores a running level counter (l)
   t = defaultdict(list)
   for i in vals:
      # check if (d) stores a 'Y' value for the given (i) value at current level (l)
      t[['N', 'Y'][any(j[l] == 'Y' for j in d[i])]].append(i) #append (i) to 'Y' partition of above condition is met, else append to 'N'
   # apply recursion to partitions if the partition contains more than one value
   return {a:b[0] if len(b) == 1 else to_tree(b, d, l + 1) for a, b in t.items()}    

import json
data = [['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']]
d = defaultdict(list)
for a, *b in data:
    d[a].append(b)

print(json.dumps(to_tree([*d], d), indent=4))

Output:

{
    "Y": {
        "N": "A",
        "Y": "B"
    },
    "N": {
        "N": "C",
        "Y": {
            "Y": "D",
            "N": "E"
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文