试图在Swift中复制Python列表

时间:2016-10-31 00:04:19

标签: python swift swift2.3

在Python中,我能够做到这一点:

day0 = 0
day1 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 
        11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
        21, 22, 23, 24, 25]

day2 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 
        11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
        21, 22, 23, 24, 25]

day3 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 
        11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
        21, 22, 23, 24, 25]

day4 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 
        11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
        21, 22, 23, 24, 25]

month0 = 0
january = [day0, day1, day2, day3, day4] 
february = [day0, day1, day2, day3, day4] 
march = [day0, day1, day2, day3, day4] 
april = [day0, day1, day2, day3, day4] 
may = [day0, day1, day2, day3, day4]
june = [day0, day1, day2, day3, day4] 
july = [day0, day1, day2, day3, day4] 
august = [day0, day1, day2, day3, day4] 
september = [day0, day1, day2, day3, day4]
october = [day0, day1, day2, day3, day4] 
november = [day0, day1, day2, day3, day4]
december = [day0, day1, day2, day3, day4]

calendar = [month0, january, february, march, april, may, june, july,       august, september,
            october, november, december]

然后这个:

print calendar[2][3[0][3]]

我会把3号回复给我。我很难弄清楚如何在Swift中做到这一点。

我可以在Swift中以基本相同的方式创建列表

var day0 = 0
var day1 = [0, 8, 2, 3, 4]
var day2 = [0, 1, 2, 3, 4]
var day3 = [0, 1, 2, 3, 4]
var day4 = [0, 1, 2, 3, 4]

var month0 = 0
var january = [[day0, day1, day2, day3, day4]]
var february = [day0, day1, day2, day3, day4]
var march = [day0, day1, day2, day3, day4]
var april = [day0, day1, day2, day3, day4]
var may = [day0, day1, day2, day3, day4]
var june = [day0, day1, day2, day3, day4]
var july = [day0, day1, day2, day3, day4]
var august = [day0, day1, day2, day3, day4]
var september = [day0, day1, day2, day3, day4]
var october = [day0, day1, day2, day3, day4]
var november = [day0, day1, day2, day3, day4]
var december = [day0, day1, day2, day3, day4]

var calendar = [month0, january, february, march, april, may, june, july, august, september, october, december]

但是当我尝试调用一个元素时,我收到错误

 BedTimeLabel.text = String(january[1],][1])
 *** Expected expression in list of expressions ***

或者当我这样尝试时

BedTimeLabel.text = String(format: january[1],arguments: [1])

我收到此错误

Cannot convert value of type 'NSArray' to expected argument type 'String'

1 个答案:

答案 0 :(得分:1)

我对Python一无所知,所以我并不完全确定我理解你的问题。

根据我的理解,你有一个多维数组,在Swift中看起来像这样:

let day1 = [0, 1, 2, 3, 4, 5, 6, 7]
let day2 = [0, 1, 2, 3, 4, 5, 6, 7]
let day3 = [0, 1, 2, 3, 4, 5, 6, 7]
let day4 = [0, 1, 2, 3, 4, 5, 6, 7]

let jan = [day1, day2, day3, day4]
let feb = [day1, day2, day3, day4]
let mar = [day1, day2, day3, day4]
let apr = [day1, day2, day3, day4]

let calendar = [jan, feb, mar, apr]

let result = calendar[2][3][3]        // 3