Python嵌套列表创建

时间:2016-12-10 01:32:43

标签: python python-3.x

import random

class player():
    def __init__(self, deck):
        self.deck = deck 
        self.hand = []
        self.results = []

    def draw(self,draws):
        for counter in range(0, draws, 1):
            card = random.randrange(0, len(self.deck), 1)
            self.hand.append(self.deck[card])
            del self.deck[card]

    def return_hand(self):
        for returncounter in range(0, len(self.hand), 1):
            self.deck.append(self.hand[returncounter]) 
        for returncounter in range(0, len(self.hand), 1):
            del self.hand[0]

    def simple_function(self):
        for counter in range(0, 3, 1):
            print("Loop", counter)
            self.draw(3)
            print("Hand", simple_cards.hand)
            self.results.extend(self.hand)
            print("Results before return", simple_cards.results)
            self.return_hand()
            print("Results after return", simple_cards.results)
            print("")

simple_cards = player(["A1", "A2", "A3", "B1", "B2", "B3", "C1", "C2", "C3"])

simple_cards.simple_function()
print("Results after function", simple_cards.results)

此代码的示例结果将是

Loop 0
Hand ['B3', 'B2', 'C3']
Results before return ['B3', 'B2', 'C3']
Results after return ['B3', 'B2', 'C3']

Loop 1
Hand ['C2', 'B3', 'A1']
Results before return ['B3', 'B2', 'C3', 'C2', 'B3', 'A1']
Results after return ['B3', 'B2', 'C3', 'C2', 'B3', 'A1']

Loop 2
Hand ['C2', 'B2', 'C1']
Results before return ['B3', 'B2', 'C3', 'C2', 'B3', 'A1', 'C2', 'B2', 'C1']
Results after return ['B3', 'B2', 'C3', 'C2', 'B3', 'A1', 'C2', 'B2', 'C1']

Results after function ['B3', 'B2', 'C3', 'C2', 'B3', 'A1', 'C2', 'B2', 'C1']

如何制作结果3个嵌套列表,如[['B3','B2','C3'],['C2','B3','A1'],['C2','B2' ,'C1']]

我觉得有一个非常基本的东西,我不理解这里,这个问题肯定已经得到了回答,但我不知道如何通过搜索自己来更好地找到问题。

我尝试追加(self.hand)和扩展([self.hand])但是两者都创造了比他们解决的更多问题,导致了

的示例输出
Loop 0
Hand ['B3', 'A2', 'B2']
Results before return [['B3', 'A2', 'B2']]
Results after return [[]]

Loop 1
Hand ['B2', 'B3', 'C3']
Results before return [['B2', 'B3', 'C3'], ['B2', 'B3', 'C3']]
Results after return [[], []]

Loop 2
Hand ['C2', 'B3', 'A2']
Results before return [['C2', 'B3', 'A2'], ['C2', 'B3', 'A2'], ['C2', 'B3', 'A2']]
Results after return [[], [], []]

Results after function [[], [], []]

感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

您可以使用appendhand副本添加到results。仅添加hand的问题在于您稍后会使用del对其进行修改,这将反映到打印结果中,如您的示例所示。

如果您将self.results.extend(self.hand)更改为self.results.append(self.hand[:]),则会获得以下输出:

Loop 0
Hand ['A1', 'C2', 'B3']
Results before return [['A1', 'C2', 'B3']]
Results after return [['A1', 'C2', 'B3']]

Loop 1
Hand ['B1', 'B3', 'A1']
Results before return [['A1', 'C2', 'B3'], ['B1', 'B3', 'A1']]
Results after return [['A1', 'C2', 'B3'], ['B1', 'B3', 'A1']]

Loop 2
Hand ['C2', 'B1', 'C1']
Results before return [['A1', 'C2', 'B3'], ['B1', 'B3', 'A1'], ['C2', 'B1', 'C1']]
Results after return [['A1', 'C2', 'B3'], ['B1', 'B3', 'A1'], ['C2', 'B1', 'C1']]

Results after function [['A1', 'C2', 'B3'], ['B1', 'B3', 'A1'], ['C2', 'B1', 'C1']]

答案 1 :(得分:1)

您还可以将代码保持为现在的状态,并仅拆分结果。

例如,我假设你的输出是:

output = ['B3', 'B2', 'C3', 'C2', 'B3', 'A1', 'C2', 'B2', 'C1']

所以,你可以这样做:

expected_output = [output[i:i+3] for i in range(0, len(output), 3)]
print(expected_output)

输出:

[['B3', 'B2', 'C3'], ['C2', 'B3', 'A1'], ['C2', 'B2', 'C1']]

因此,为了编辑代码,您只需编辑一行:

self.results.extend(self.hand)self.results.extend(self.hand[i:i+3] for i in range(0, len(self.hand), 3))

所以你的simple_function()将是:

def simple_function(self):
        for counter in range(0, 3, 1):
            print("Loop", counter)
            self.draw(3)
            print("Hand", simple_cards.hand)
            # The edited line
            self.results.extend(self.hand[i:i+3] for i in range(0, len(self.hand), 3))
            print("Results before return", simple_cards.results)
            self.return_hand()
            print("Results after return", simple_cards.results)
            print("")

编辑后的输出:

Loop 0
Hand ['B1', 'C1', 'C3']
Results before return [['B1', 'C1', 'C3']]
Results after return [['B1', 'C1', 'C3']]

Loop 1
Hand ['C1', 'C3', 'A3']
Results before return [['B1', 'C1', 'C3'], ['C1', 'C3', 'A3']]
Results after return [['B1', 'C1', 'C3'], ['C1', 'C3', 'A3']]

Loop 2
Hand ['C2', 'A3', 'B3']
Results before return [['B1', 'C1', 'C3'], ['C1', 'C3', 'A3'], ['C2', 'A3', 'B3']]
Results after return [['B1', 'C1', 'C3'], ['C1', 'C3', 'A3'], ['C2', 'A3', 'B3']]

Results after function [['B1', 'C1', 'C3'], ['C1', 'C3', 'A3'], ['C2', 'A3', 'B3']]