【问题标题】:Saving output from powerset保存 powerset 的输出
【发布时间】:2020-04-05 20:44:34
【问题描述】:

我最近开始学习一些关于 python 的 powerset-possibility 的知识。我使用的代码如下:

 def printPowerSet(set,set_size): 

    # set_size of power set of a set 
    # with set_size n is (2**n -1) 
    pow_set_size = (int) (math.pow(2, set_size)) 
    counter = 0 
    j = 0 
    zahl = 0  #um zu sehen, wie viele Kombinationsmöglichkeiten existieren
    # Run from counter 000..0 to 111..1 
    for counter in range(0, pow_set_size): 
        for j in range(0, set_size): 

            # Check if jth bit in the  
            # counter is set If set then  
            # print jth element from set  
            if((counter & (1 << j)) > 0): 
                print(set[j], end="")
        print("")
        zahl = zahl+1
    print(zahl)
# Driver program to test printPowerSet 
set = variants           
printPowerSet(set, len(set)) 

变量变量有以下数据:

>>> variants
['(4|W)', '(12|T)', '(21|V)', '(31|S)', '(41|Q)', '(42|E)', '(62|W)',
'(78|A)', '(84|C)', '(84|H)', '(89|C)', '(89|H)', '(93|V)', '(114|G)', '(119|R)']

函数的输出给了我以下输出(这里是一瞥):

(31|S)(41|Q)(62|W)(78|A)(84|C)(84|H)(89|C)(89|H)(93|V)(114|G)(119|R)
(4|W)(31|S)(41|Q)(62|W)(78|A)(84|C)(84|H)(89|C)(89|H)(93|V)(114|G)(119|R)
(12|T)(31|S)(41|Q)(62|W)(78|A)(84|C)(84|H)(89|C)(89|H)(93|V)(114|G)(119|R)
(4|W)(12|T)(31|S)(41|Q)(62|W)(78|A)(84|C)(84|H)(89|C)(89|H)(93|V)(114|G)(119|R)
(21|V)(31|S)(41|Q)(62|W)(78|A)(84|C)(84|H)(89|C)(89|H)(93|V)(114|G)(119|R)
(4|W)(21|V)(31|S)(41|Q)(62|W)(78|A)(84|C)(84|H)(89|C)(89|H)(93|V)(114|G)(119|R)
(12|T)(21|V)(31|S)(41|Q)(62|W)(78|A)(84|C)(84|H)(89|C)(89|H)(93|V)(114|G)(119|R)
(4|W)(12|T)(21|V)(31|S)(41|Q)(62|W)(78|A)(84|C)(84|H)(89|C)(89|H)(93|V)(114|G)(119|R)
(42|E)(62|W)(78|A)(84|C)(84|H)(89|C)(89|H)(93|V)(114|G)(119|R)
(4|W)(42|E)(62|W)(78|A)(84|C)(84|H)(89|C)(89|H)(93|V)(114|G)(119|R)
(12|T)(42|E)(62|W)(78|A)(84|C)(84|H)(89|C)(89|H)(93|V)(114|G)(119|R)
(4|W)(12|T)(42|E)(62|W)(78|A)(84|C)(84|H)(89|C)(89|H)(93|V)(114|G)(119|R)
(21|V)(42|E)(62|W)(78|A)(84|C)(84|H)(89|C)(89|H)(93|V)(114|G)(119|R)
(4|W)(21|V)(42|E)(62|W)(78|A)(84|C)(84|H)(89|C)(89|H)(93|V)(114|G)(119|R)
(12|T)(21|V)(42|E)(62|W)(78|A)(84|C)(84|H)(89|C)(89|H)(93|V)(114|G)(119|R)
(4|W)(12|T)(21|V)(42|E)(62|W)(78|A)(84|C)(84|H)(89|C)(89|H)(93|V)(114|G)(119|R)
(31|S)(42|E)(62|W)(78|A)(84|C)(84|H)(89|C)(89|H)(93|V)(114|G)(119|R)
(4|W)(31|S)(42|E)(62|W)(78|A)(84|C)(84|H)(89|C)(89|H)(93|V)(114|G)(119|R)
(12|T)(31|S)(42|E)(62|W)(78|A)(84|C)(84|H)(89|C)(89|H)(93|V)(114|G)(119|R)
(4|W)(12|T)(31|S)(42|E)(62|W)(78|A)(84|C)(84|H)(89|C)(89|H)(93|V)(114|G)(119|R)
(21|V)(31|S)(42|E)(62|W)(78|A)(84|C)(84|H)(89|C)(89|H)(93|V)(114|G)(119|R)

好消息是 powerset 函数确实为我提供了所有组合。每个组合可能性都在一行中给出。此步骤在 if 循环中完成。有人可以给我一个想法或方法,我如何将输出保存在变量中,而不是打印出来?我想使用此步骤/输出进行进一步分析。 谢谢。

【问题讨论】:

    标签: python python-3.x powerset


    【解决方案1】:

    将打印语句转换为字符串连接。

    def printPowerSet(set,set_size): 
        # Set_size of power set of a set 
        # with set_size n is (2**n -1) 
        pow_set_size = (int) (math.pow(2, set_size)) 
        counter = 0 
        j = 0 
        zahl = 0  # Um zu sehen, wie viele Kombinationsmöglichkeiten existieren
        result = ""  # String to store the result
    
        # Run from counter 000..0 to 111..1 
        for counter in range(0, pow_set_size): 
            for j in range(0, set_size): 
    
                # Check if jth bit in the  
                # counter is set If set then  
                # print jth element from set  
                if((counter & (1 << j)) > 0):
                    result += str(set[j])
    
            result += "\n"
            zahl += 1
    
        return result + "\n" + str(zahl)
    
    # Driver program to test printPowerSet 
    set = variants           
    result = printPowerSet(set, len(set)) 
    
    print("Result:")
    print(result)
    

    【讨论】:

    • 为什么我没有这样想?非常感谢!
    • 别提了,如果觉得有用就点个赞吧。
    猜你喜欢
    • 1970-01-01
    • 2013-09-15
    • 2016-08-07
    • 2015-02-14
    • 1970-01-01
    • 1970-01-01
    • 2016-08-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多