如何在Python中的数组中创建数组?

时间:2017-06-28 11:19:11

标签: python python-3.x

我试图在python中创建UNO,并且我已经将所有卡分类到单独的数组中,但是为了绘制卡,我需要创建一个包含所有不同卡阵列的数组。

cards = [yellowcards = [Cards.yellow0, Cards.yellow1, Cards.yellow2, Cards.yellow3, Cards.yellow4, Cards.yellow5, Cards.yellow6,
         Cards.yellow7, Cards.yellow8, Cards.yellow9, Cards.yellowmiss, Cards.yellowreverse, Cards.yellow_2,
         Cards.yyellow1, Cards.yyellow2, Cards.yyellow3, Cards.yyellow4, Cards.yyellow5, Cards.yyellow6,
         Cards.yyellow7, Cards.yyellow8, Cards.yyellow9, Cards.yyellowmiss, Cards.yyellowreverse, Cards.yyellow_2],
         greencards = [Cards.green0, Cards.green1, Cards.green2, Cards.green3, Cards.green4, Cards.green5, Cards.green6,
         Cards.green7, Cards.green8, Cards.green9, Cards.greenmiss, Cards.greenreverse, Cards.green_2,
         Cards.ggreen1, Cards.ggreen2, Cards.ggreen3, Cards.ggreen4, Cards.ggreen5, Cards.ggreen6,
         Cards.ggreen7, Cards.ggreen8, Cards.ggreen9, Cards.ggreenmiss, Cards.ggreenreverse, Cards.ggreen_2],
         bluecards = [Cards.blue0, Cards.blue1, Cards.blue2, Cards.blue3, Cards.blue4, Cards.blue5, Cards.blue6,
         Cards.blue7, Cards.blue8, Cards.blue9, Cards.bluemiss, Cards.bluereverse, Cards.blue_2,
         Cards.bblue1, Cards.bblue2, Cards.bblue3, Cards.bblue4, Cards.bblue5, Cards.bblue6,
         Cards.bblue7, Cards.bblue8, Cards.bblue9, Cards.bbluemiss, Cards.bbluereverse, Cards.bblue_2],
         redcards = [Cards.red0, Cards.red1, Cards.red2, Cards.red3, Cards.red4, Cards.red5, Cards.red6,
         Cards.red7, Cards.red8, Cards.red9, Cards.redmiss, Cards.redreverse, Cards.red_2,
         Cards.rred1, Cards.rred2, Cards.rred3, Cards.rred4, Cards.rred5, Cards.rred6,
         Cards.rred7, Cards.rred8, Cards.rred9, Cards.rredmiss, Cards.rredreverse, Cards.rred_2],
         wildcards = [Cards.wild1, Cards.wild2, Cards.wild3, Cards.wild4, Cards.wilddraw1, Cards.wilddraw2,
         Cards.wilddraw3, Cards.wilddraw4]]

4 个答案:

答案 0 :(得分:1)

基本上这就是你在做什么,

>>> [a=[34,34],b=[23,34]]
SyntaxError: invalid syntax

这种分配不起作用。

然而这很有效,

>>> a=[34,34]
>>> b=[23,34]
>>> [a,b]
[[34, 34], [23, 34]]

所以在你的情况下这样做,

yellowcards = [Cards.yellow0, Cards.yellow1, Cards.yellow2, Cards.yellow3, Cards.yellow4, Cards.yellow5, Cards.yellow6,
     Cards.yellow7, Cards.yellow8, Cards.yellow9, Cards.yellowmiss, Cards.yellowreverse, Cards.yellow_2,
     Cards.yyellow1, Cards.yyellow2, Cards.yyellow3, Cards.yyellow4, Cards.yyellow5, Cards.yyellow6,
     Cards.yyellow7, Cards.yyellow8, Cards.yyellow9, Cards.yyellowmiss, Cards.yyellowreverse, Cards.yyellow_2]

greencards = [Cards.green0, Cards.green1, Cards.green2, Cards.green3, Cards.green4, Cards.green5, Cards.green6,
     Cards.green7, Cards.green8, Cards.green9, Cards.greenmiss, Cards.greenreverse, Cards.green_2,
     Cards.ggreen1, Cards.ggreen2, Cards.ggreen3, Cards.ggreen4, Cards.ggreen5, Cards.ggreen6,
     Cards.ggreen7, Cards.ggreen8, Cards.ggreen9, Cards.ggreenmiss, Cards.ggreenreverse, Cards.ggreen_2]

bluecards = [Cards.blue0, Cards.blue1, Cards.blue2, Cards.blue3, Cards.blue4, Cards.blue5, Cards.blue6,
     Cards.blue7, Cards.blue8, Cards.blue9, Cards.bluemiss, Cards.bluereverse, Cards.blue_2,
     Cards.bblue1, Cards.bblue2, Cards.bblue3, Cards.bblue4, Cards.bblue5, Cards.bblue6,
     Cards.bblue7, Cards.bblue8, Cards.bblue9, Cards.bbluemiss, Cards.bbluereverse, Cards.bblue_2]

redcards = [Cards.red0, Cards.red1, Cards.red2, Cards.red3, Cards.red4, Cards.red5, Cards.red6,
     Cards.red7, Cards.red8, Cards.red9, Cards.redmiss, Cards.redreverse, Cards.red_2,
     Cards.rred1, Cards.rred2, Cards.rred3, Cards.rred4, Cards.rred5, Cards.rred6,
     Cards.rred7, Cards.rred8, Cards.rred9, Cards.rredmiss, Cards.rredreverse, Cards.rred_2]

wildcards = [Cards.wild1, Cards.wild2, Cards.wild3, Cards.wild4, Cards.wilddraw1, Cards.wilddraw2,
     Cards.wilddraw3, Cards.wilddraw4]

cards = [yellowcards,greencards,bluecards, redcards, wildcards]

然而正如其他用户所说。尝试使用dictionary代替列表。

同样在旁注:

Arrays 的一般定义是:

  

在编程中,一系列对象都是相同的大小和类型。数组中的每个对象称为数组元素。

然而在python中你有lists。这些列表可以具有不同类型的不同数据类型。像

a_list = [1,2,3] #all int
b = ['s','a','d'] #all strings [Note there is no char here just str type with length 1
c = [1,2,3,'a','s',a_list] #mixed list

一切都有效。记得这个。

答案 1 :(得分:1)

您应该使用dictionary

cards = {"yellowcards" : [Cards.yellow0, Cards.yellow1, Cards.yellow2, Cards.yellow3, Cards.yellow4, Cards.yellow5, Cards.yellow6,
         Cards.yellow7, Cards.yellow8, Cards.yellow9, Cards.yellowmiss, Cards.yellowreverse, Cards.yellow_2,
         Cards.yyellow1, Cards.yyellow2, Cards.yyellow3, Cards.yyellow4, Cards.yyellow5, Cards.yyellow6,
         Cards.yyellow7, Cards.yyellow8, Cards.yyellow9, Cards.yyellowmiss, Cards.yyellowreverse, Cards.yyellow_2],
         "greencards" : [Cards.green0, Cards.green1, Cards.green2, Cards.green3, Cards.green4, Cards.green5, Cards.green6,
         Cards.green7, Cards.green8, Cards.green9, Cards.greenmiss, Cards.greenreverse, Cards.green_2,
         Cards.ggreen1, Cards.ggreen2, Cards.ggreen3, Cards.ggreen4, Cards.ggreen5, Cards.ggreen6,
         Cards.ggreen7, Cards.ggreen8, Cards.ggreen9, Cards.ggreenmiss, Cards.ggreenreverse, Cards.ggreen_2],
         "bluecards" : [Cards.blue0, Cards.blue1, Cards.blue2, Cards.blue3, Cards.blue4, Cards.blue5, Cards.blue6,
         Cards.blue7, Cards.blue8, Cards.blue9, Cards.bluemiss, Cards.bluereverse, Cards.blue_2,
         Cards.bblue1, Cards.bblue2, Cards.bblue3, Cards.bblue4, Cards.bblue5, Cards.bblue6,
         Cards.bblue7, Cards.bblue8, Cards.bblue9, Cards.bbluemiss, Cards.bbluereverse, Cards.bblue_2],
         "redcards" : [Cards.red0, Cards.red1, Cards.red2, Cards.red3, Cards.red4, Cards.red5, Cards.red6,
         Cards.red7, Cards.red8, Cards.red9, Cards.redmiss, Cards.redreverse, Cards.red_2,
         Cards.rred1, Cards.rred2, Cards.rred3, Cards.rred4, Cards.rred5, Cards.rred6,
         Cards.rred7, Cards.rred8, Cards.rred9, Cards.rredmiss, Cards.rredreverse, Cards.rred_2],
         "wildcards" : [Cards.wild1, Cards.wild2, Cards.wild3, Cards.wild4, Cards.wilddraw1, Cards.wilddraw2,
         Cards.wilddraw3, Cards.wilddraw4]}

所以你可以像这样访问每一种颜色。

cars["yellowcards"]

输出

[Cards.yellow0, Cards.yellow1, Cards.yellow2, Cards.yellow3, Cards.yellow4, Cards.yellow5, Cards.yellow6,
         Cards.yellow7, Cards.yellow8, Cards.yellow9, Cards.yellowmiss, Cards.yellowreverse, Cards.yellow_2,
         Cards.yyellow1, Cards.yyellow2, Cards.yyellow3, Cards.yyellow4, Cards.yyellow5, Cards.yyellow6,
         Cards.yyellow7, Cards.yyellow8, Cards.yyellow9, Cards.yyellowmiss, Cards.yyellowreverse, Cards.yyellow_2]

答案 2 :(得分:0)

你不能像你一样在另一个数组中声明你的数组。你必须在外面声明:

yellowcards = [Cards.yellow0, Cards.yellow1, Cards.yellow2, Cards.yellow3, Cards.yellow4, Cards.yellow5, Cards.yellow6,
     Cards.yellow7, Cards.yellow8, Cards.yellow9, Cards.yellowmiss, Cards.yellowreverse, Cards.yellow_2,
     Cards.yyellow1, Cards.yyellow2, Cards.yyellow3, Cards.yyellow4, Cards.yyellow5, Cards.yyellow6,
     Cards.yyellow7, Cards.yyellow8, Cards.yyellow9, Cards.yyellowmiss, Cards.yyellowreverse, Cards.yyellow_2]
greencards = [Cards.green0, Cards.green1, Cards.green2, Cards.green3, Cards.green4, Cards.green5, Cards.green6,
     Cards.green7, Cards.green8, Cards.green9, Cards.greenmiss, Cards.greenreverse, Cards.green_2,
     Cards.ggreen1, Cards.ggreen2, Cards.ggreen3, Cards.ggreen4, Cards.ggreen5, Cards.ggreen6,
     Cards.ggreen7, Cards.ggreen8, Cards.ggreen9, Cards.ggreenmiss, Cards.ggreenreverse, Cards.ggreen_2]
bluecards = [Cards.blue0, Cards.blue1, Cards.blue2, Cards.blue3, Cards.blue4, Cards.blue5, Cards.blue6,
     Cards.blue7, Cards.blue8, Cards.blue9, Cards.bluemiss, Cards.bluereverse, Cards.blue_2,
     Cards.bblue1, Cards.bblue2, Cards.bblue3, Cards.bblue4, Cards.bblue5, Cards.bblue6,
     Cards.bblue7, Cards.bblue8, Cards.bblue9, Cards.bbluemiss, Cards.bbluereverse, Cards.bblue_2]
redcards = [Cards.red0, Cards.red1, Cards.red2, Cards.red3, Cards.red4, Cards.red5, Cards.red6,
     Cards.red7, Cards.red8, Cards.red9, Cards.redmiss, Cards.redreverse, Cards.red_2,
     Cards.rred1, Cards.rred2, Cards.rred3, Cards.rred4, Cards.rred5, Cards.rred6,
     Cards.rred7, Cards.rred8, Cards.rred9, Cards.rredmiss, Cards.rredreverse, Cards.rred_2]
wildcards = [Cards.wild1, Cards.wild2, Cards.wild3, Cards.wild4, Cards.wilddraw1, Cards.wilddraw2,
     Cards.wilddraw3, Cards.wilddraw4]
cards = [yellowcards, greencards, bluecards, redcards, wildcards]

答案 3 :(得分:0)

所以从你的问题我发现你已经有了这些清单:

yellowcards = [Cards.yellow0, Cards.yellow1, Cards.yellow2, Cards.yellow3, Cards.yellow4, Cards.yellow5, Cards.yellow6, Cards.yellow7, Cards.yellow8, Cards.yellow9, Cards.yellowmiss, Cards.yellowreverse, Cards.yellow_2, Cards.yyellow1, Cards.yyellow2, Cards.yyellow3, Cards.yyellow4, Cards.yyellow5, Cards.yyellow6, Cards.yyellow7, Cards.yyellow8, Cards.yyellow9, Cards.yyellowmiss, Cards.yyellowreverse, Cards.yyellow_2],

greencards = [Cards.green0, Cards.green1, Cards.green2, Cards.green3, Cards.green4, Cards.green5, Cards.green6, Cards.green7, Cards.green8, Cards.green9, Cards.greenmiss, Cards.greenreverse, Cards.green_2, Cards.ggreen1, Cards.ggreen2, Cards.ggreen3, Cards.ggreen4, Cards.ggreen5, Cards.ggreen6, Cards.ggreen7, Cards.ggreen8, Cards.ggreen9, Cards.ggreenmiss, Cards.ggreenreverse, Cards.ggreen_2]

bluecards = [Cards.blue0, Cards.blue1, Cards.blue2, Cards.blue3,Cards.blue4, Cards.blue5, Cards.blue6, Cards.blue7, Cards.blue8, Cards.blue9, Cards.bluemiss, Cards.bluereverse, Cards.blue_2, Cards.bblue1, Cards.bblue2, Cards.bblue3, Cards.bblue4, Cards.bblue5, Cards.bblue6, Cards.bblue7, Cards.bblue8, Cards.bblue9, Cards.bbluemiss, Cards.bbluereverse, Cards.bblue_2]

redcards = [Cards.red0, Cards.red1, Cards.red2, Cards.red3, Cards.red4, Cards.red5, Cards.red6, Cards.red7, Cards.red8, Cards.red9, Cards.redmiss, Cards.redreverse, Cards.red_2, Cards.rred1, Cards.rred2, Cards.rred3, Cards.rred4, Cards.rred5, Cards.rred6, Cards.rred7, Cards.rred8, Cards.rred9, Cards.rredmiss, Cards.rredreverse, Cards.rred_2]

wildcards = [Cards.wild1, Cards.wild2, Cards.wild3, Cards.wild4, Cards.wilddraw1, Cards.wilddraw2, Cards.wilddraw3, Cards.wilddraw4]

如果您要创建列表列表,请执行以下操作:

cards = [yellowcards, greencards, bluecards, redcards, wildcards]

这将产生一个包含5个元素的列表,这些元素是您的原始列表。您可以使用cards[1][2]访问绿色的两个 - cards的第二个元素是包含所有绿卡的列表,该列表的第三个元素是绿色的两个。

但是,您的原始方法看起来不仅仅是dict

cards = {"yellowcards" : yellowcards,
         "greencards" : greencards,
         "bluecards" : bluecards,
         "redcards" : redcards,
         "wildcards" : wildcards}

这样您就可以使用cards["greencards"][2]访问绿色的两个。