[英]Making a decision tree
I have ten systems, A, B, C, ..., J. Each system can be up or down. 我有十个系统,A,B,C,...,J。每个系统都可以启动或关闭。 If, for example, systems A, B, D and J are down, with the remainder being up then I want to take action X. If systems C, D, and H are down, with the remainder being up then I want to take action Y. If systems A, E, F, H and I are down, with the remainder being up then I want to take action Z.
例如,如果系统A,B,D和J处于关闭状态,其余部分处于启动状态,那么我要采取措施X。如果系统C,D和H处于关闭状态,则其余部分处于启动状态,那么我要采取措施动作Y。如果系统A,E,F,H和I处于关闭状态,其余部分处于上升状态,则我要执行动作Z。
I am wanting to write a program that will print the various combinations (I believe with 10 systems and each can be up or down there are 100 combinations). 我想编写一个可以打印各种组合的程序(我相信有10个系统,每个系统可以向上或向下都有100个组合)。
I have this so far: 到目前为止,我有:
import itertools
status_list = (
"Up",
"Down",
)
component_list = (
"A",
"B",
"C",
"D",
"E",
"F",
"G",
"G",
"I",
"J",
)
combinations = itertools.product(component_list, status_list)
If you don't mind using a binary representation, we could use 0
to represent Up
and 1
to represent Down
如果您不介意使用二进制表示形式,则可以使用
0
表示Up
,使用1
表示Down
Our values would range from 0
, to 10^2-1
, 我们的值范围从
0
到10^2-1
,
which is 0
to 1023
, or 1023
0
到1023
,或者
0000000000
to 1111111111
. 0000000000
至1111111111
。
So we could just loop from 0 to 1023, printing the numbers out in a 10-digit binary format: 因此我们可以从0循环到1023,以10位二进制格式输出数字:
for x in range(2**10):
print '{0:010b}'.format(x)
Outputs: 输出:
0000000000
0000000001
0000000010
0000000011
0000000100
0000000101
0000000110
...etc
If you wanted to get fancier and print up
and down
instead of 0
and 1
, you could examine the digits one-by-one and convert: 如果你想更大胆的尝试并打印
up
和down
的,而不是0
和1
,您可以检查数字一个接一个和转换:
for x in range(2**10):
#Iterate over each character c in the binary number x
#Convert c to an int
#Look up the value in status_list by taking c mod length of status list
print(" ".join(status_list[int(c)%len(status_list)] for c in '{0:010b}'.format(x)))
Outputs: 输出:
Up Up Up Up Up Up Up Up Up Up
Up Up Up Up Up Up Up Up Up Down
Up Up Up Up Up Up Up Up Down Up
Up Up Up Up Up Up Up Up Down Down
You can print out every possible combination of Up
and Down
using itertools.product
: 您可以使用
itertools.product
打印出Up
和Down
每种可能组合:
import itertools
components = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J']
statuses = ['Up', 'Down']
for state in itertools.product(statuses, repeat=len(components)):
for c,s in zip(components, state):
print '{}:{}'.format(c,s),
print
This produces output like the following: 产生如下输出:
A:Up B:Up C:Up D:Up E:Up F:Up G:Up H:Up I:Up J:Up
A:Up B:Up C:Up D:Up E:Up F:Up G:Up H:Up I:Up J:Down
...
A:Up B:Down C:Down D:Down E:Down F:Down G:Down H:Down I:Down J:Up
A:Up B:Down C:Down D:Down E:Down F:Down G:Down H:Down I:Down J:Down
A:Down B:Up C:Up D:Up E:Up F:Up G:Up H:Up I:Up J:Up
A:Down B:Up C:Up D:Up E:Up F:Up G:Up H:Up I:Up J:Down
...
A:Down B:Down C:Down D:Down E:Down F:Down G:Down H:Down I:Down J:Up
A:Down B:Down C:Down D:Down E:Down F:Down G:Down H:Down I:Down J:Down
It deals nicely with a variable number of components or states (for example, you could make states = ['Up', 'Down', 'Unknown']
and you would get 3^10 = 59049 outputs). 它很好地处理了可变数量的组件或状态(例如,您可以使
states = ['Up', 'Down', 'Unknown']
并获得3 ^ 10 = 59049的输出)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.