如何在不添加任何值的情况下定义列表?

时间:2017-08-07 18:27:23

标签: python list

我写了一段代码,用于21张卡片的家庭作业:

def random_list():
        suits = ["D","S","C","h"]
        cardnumbers = ["A",2,3,4,5,6,7,8,9,"J","Q","K"]
        point1 = point = m = 0
        List1 = []
        List2 = []
        List3 = []
        listnames = [List1], [List2], [List3]
        while point < 3:
                y = listnames[m]
                m = m + 1
                while point1 < 7:
                        randomsuite = random.randint(0,3)
                        randomnumber = random.randint(0,11)
                        x = cardnumbers[randomnumber],suits[randomsuite];
                        y.append(x)
                        point1 = point1 + 1 
                point = point + 1 
                point1 = 0
        return listnames

打印:

list1 = [[], (6, 'h'), (4, 'S'), (4, 'D'), (7, 'h'), ('A', 'D'), ('Q', 'D'), (9, 'C')]
list2 = [[], ('J', 'C'), ('A', 'h'), ('K', 'S'), (7, 'D'), (9, 'h'), (7, 'C'), ('A', 'h')]
list3 = [[], (6, 'C'), (4, 'h'), (5, 'D'), ('J', 'D'), (2, 'S'), (4, 'h'), (8, 'S')]

每个列表的第一个值是“[]”,这会破坏其余的代码。我之前尝试从列表中删除该值但是,它有这个错误***''tuple'对象没有属性'删除'*** 感谢

3 个答案:

答案 0 :(得分:0)

你非常接近:只需将三个列表放入列表名

import random
def random_list():
        suits = ["D","S","C","h"]
        cardnumbers = ["A",2,3,4,5,6,7,8,9,"J","Q","K"]
        point1 = point = m = 0
        List1 = []
        List2 = []
        List3 = []
        listnames = [List1, List2, List3]
        while point < 3:
                y = listnames[m]
                m = m + 1
                while point1 < 7:
                        randomsuite = random.randint(0,3)
                        randomnumber = random.randint(0,11)
                        x = cardnumbers[randomnumber],suits[randomsuite];
                        y.append(x)
                        point1 = point1 + 1
                point = point + 1
                point1 = 0
        return listnames

list1, list2, list3 = random_list()
print "list1: ", list1
print "list2: ", list2
print "list3: ", list3
list1:  [('A', 'S'), ('Q', 'C'), (2, 'S'), ('K', 'D'), (7, 'h'), (4, 'C'), ('A', 'S')]
list2:  [(5, 'D'), (6, 'h'), ('J', 'h'), (8, 'h'), ('J', 'S'), ('A', 'D'), (2, 'h')]
list3:  [(7, 'S'), (7, 'C'), (8, 'D'), ('A', 'C'), (5, 'h'), (2, 'D'), (9, 'S')]

答案 1 :(得分:0)

您应该将listnames构建为包含List1List2以及List3的列表,这样listsnames = [List1, List2, List3]

但是(不知道为什么要制作三元素元组列表)如果你真的想要制作一个三元素元组列表,你最后可以删除[]。如果[]始终处于开始状态,那么只需在返回listnames之前进行清理

def random_list():
        suits = ["D","S","C","h"]
        cardnumbers = ["A",2,3,4,5,6,7,8,9,"J","Q","K"]
        point1 = point = m = 0
        List1 = []
        List2 = []
        List3 = []
        listnames = [List1], [List2], [List3]
        while point < 3:
                y = listnames[m]
                m = m + 1
                while point1 < 7:
                        randomsuite = random.randint(0,3)
                        randomnumber = random.randint(0,11)
                        x = cardnumbers[randomnumber],suits[randomsuite];
                        y.append(x)
                        point1 = point1 + 1 
                point = point + 1 
                point1 = 0
        for x in range(0, len(listnames)):  #clean up here
          listnames[x].pop(0)
        return listnames

打印出来:

print(random_list()[0])
print(random_list()[1])
print(random_list()[2])

结果

[(9, 'D'), (8, 'C'), ('J', 'h'), (9, 'h'), (2, 'D'), (3, 'C'), (3, 'S')]
[(3, 'S'), (2, 'h'), (2, 'S'), (2, 'D'), (4, 'D'), (9, 'D'), ('Q', 'C')]
[('Q', 'C'), ('K', 'S'), (4, 'h'), (3, 'D'), ('Q', 'S'), (6, 'C'), ('J', 'S')]

答案 2 :(得分:0)

这个循环是一个问题:

> df1 * df2
  factor1
1      NA
2      NA
3      NA
Warning message:
In Ops.factor(left, right) : ‘*’ not meaningful for factors

因为它可以让你两次同一张牌(在友情游戏中不好看。)你可以在 while point1 < 7: randomsuite = random.randint(0,3) randomnumber = random.randint(0,11) x = cardnumbers[randomnumber],suits[randomsuite]; y.append(x) point1 = point1 + 1 输出中看到这个,其中list2出现两次。让我们解决这个问题,以及您的原始问题,并在此过程中简化您的代码:

('A', 'h')