在 Swift 中,如何对数组中的值求和
In Swift, how to sum values from an array
这是我的代码:
import Foundation
struct Movie {
var title = ""
var year = 0
var isImportant: Bool
var isFinished: Bool
init(title: String, year: Int, isImportant: Bool, isFinished: Bool) {
self.title = title
self.year = year
self.isImportant = isImportant
self.isFinished = isFinished
}
static let list1 = [
Movie(title: "The Shawshank Redemption", year: 1994, isImportant: false, isFinished: false),
Movie(title: "The Godfather", year: 1972, isImportant: false, isFinished: false),
Movie(title: "The Dark Knight", year: 2008, isImportant: false, isFinished: false),
Movie(title: "The Godfather: Part II", year: 1974, isImportant: false, isFinished: false),
Movie(title: "Avengers: Infinity War", year: 2018, isImportant: false, isFinished: false),
Movie(title: "Schindler's List", year: 1993, isImportant: false, isFinished: false)
]
static let list2 = [
Movie(title: "The Lord of the Rings: The Fellowship of the Ring", year: 2001, isImportant: false, isFinished: false),
Movie(title: "Inception", year: 2010, isImportant: false, isFinished: false),
Movie(title: "Forrest Gump", year: 1994, isImportant: false, isFinished: false),
Movie(title: "Fight Club", year: 1999, isImportant: false, isFinished: false)
]
static let list3 = [list1, list2]
static let nomsDesRepas: [String] = ["Petit Déjeuner", "Collation 11h"]
}
var list2Dim = Movie.list3
var list1 = Movie.list1
我必须从“list1”字典中总结年份,而不是直接从“list1”中总结。我需要通过“list2dim”词典。
我的意思是 (1994+1972+ ... +1993)
I don't know how to navigate in it! Could you help me please?
索引是相同的,无论数组中有什么,包括其他数组,所以“导航”它只是重复索引。你知道如何从你的问题中索引 .
在 playground 中尝试以下操作并确保您理解每一行输出:
let list1 = [1, 2]
let list2 = [3, 4, 5]
let list3 = [6]
let listlist1 = [list1, list2]
let listlist2 = [list3]
let listlistlist = [listlist1, listlist2]
// print what we have produced
print(listlistlist)
// "navigate" into the structure, one step at a time
let result1 = listlistlist[0]
print(result1)
let result2 = result1[1]
print(result2)
let result3 = result2[2]
print(result3)
// indexing is left-associative, navigate in one step
let result012 = listlistlist[0][1][2]
print(result012)
如果您不确定 left-associative 读的是什么 Operator associativity。
在上面的例子中,最终结果是一个数字,从你之前的 如果最终结果是一个对象,你知道你可以访问它的属性。
在您的问题 的答案中,您了解了如何使用 map
通过对现有数组的每个元素应用函数来生成新数组。
如果像 map
这样按照某种模式处理整个数组的函数存在于库中,库中可能没有其他函数也按照其他模式处理整个数组吗?因此,请查找 map
的文档,然后通读文档的那个区域,看看您找到了什么——您可能不会失望,并且离您的年值求和目标更近了一步。
如果您在旅程的中途遇到困难,请提出一个新问题,包括您到那时为止设计的代码,描述您遇到的错误或您无法弄清楚的步骤,并包括一个link 回到这个问题,以便读者可以阅读您的问答。下一步肯定会有人帮助你。
继续学习、阅读和实践! HTH
我终于找到了问题的答案:
struct Movie {
var title = ""
var year = 0
var isImportant: Bool
var isFinished: Bool
init(title: String, year: Int, isImportant: Bool, isFinished: Bool) {
self.title = title
self.year = year
self.isImportant = isImportant
self.isFinished = isFinished
}
static let list1 = [
Movie(title: "The Shawshank Redemption", year: 1994, isImportant: false, isFinished: false),
Movie(title: "The Godfather", year: 1972, isImportant: false, isFinished: false),
Movie(title: "The Dark Knight", year: 2008, isImportant: false, isFinished: false),
Movie(title: "The Godfather: Part II", year: 1974, isImportant: false, isFinished: false),
Movie(title: "Avengers: Infinity War", year: 2018, isImportant: false, isFinished: false),
Movie(title: "Schindler's List", year: 1993, isImportant: false, isFinished: false)
]
static let list2 = [
Movie(title: "The Lord of the Rings: The Fellowship of the Ring", year: 2001, isImportant: false, isFinished: false),
Movie(title: "Inception", year: 2010, isImportant: false, isFinished: false),
Movie(title: "Forrest Gump", year: 1994, isImportant: false, isFinished: false),
Movie(title: "Fight Club", year: 1999, isImportant: false, isFinished: false)
]
static let list3 = [list1, list2]
static let nomsDesRepas: [String] = ["Petit Déjeuner", "Collation 11h"]
}
var list2Dim = Movie.list3
func sumSectionCalorie(Liste liste: Int) -> Int {
var total = 0
let number = list2Dim[liste].count
for xxx in 0 ..< number {
total += list2Dim[liste][xxx].year
}
return total
}
let total = sumSectionCalorie(Liste: 0)
print(total)
谢谢CARD给我提示
这是我的代码:
import Foundation
struct Movie {
var title = ""
var year = 0
var isImportant: Bool
var isFinished: Bool
init(title: String, year: Int, isImportant: Bool, isFinished: Bool) {
self.title = title
self.year = year
self.isImportant = isImportant
self.isFinished = isFinished
}
static let list1 = [
Movie(title: "The Shawshank Redemption", year: 1994, isImportant: false, isFinished: false),
Movie(title: "The Godfather", year: 1972, isImportant: false, isFinished: false),
Movie(title: "The Dark Knight", year: 2008, isImportant: false, isFinished: false),
Movie(title: "The Godfather: Part II", year: 1974, isImportant: false, isFinished: false),
Movie(title: "Avengers: Infinity War", year: 2018, isImportant: false, isFinished: false),
Movie(title: "Schindler's List", year: 1993, isImportant: false, isFinished: false)
]
static let list2 = [
Movie(title: "The Lord of the Rings: The Fellowship of the Ring", year: 2001, isImportant: false, isFinished: false),
Movie(title: "Inception", year: 2010, isImportant: false, isFinished: false),
Movie(title: "Forrest Gump", year: 1994, isImportant: false, isFinished: false),
Movie(title: "Fight Club", year: 1999, isImportant: false, isFinished: false)
]
static let list3 = [list1, list2]
static let nomsDesRepas: [String] = ["Petit Déjeuner", "Collation 11h"]
}
var list2Dim = Movie.list3
var list1 = Movie.list1
我必须从“list1”字典中总结年份,而不是直接从“list1”中总结。我需要通过“list2dim”词典。 我的意思是 (1994+1972+ ... +1993)
I don't know how to navigate in it! Could you help me please?
索引是相同的,无论数组中有什么,包括其他数组,所以“导航”它只是重复索引。你知道如何从你的问题中索引
在 playground 中尝试以下操作并确保您理解每一行输出:
let list1 = [1, 2]
let list2 = [3, 4, 5]
let list3 = [6]
let listlist1 = [list1, list2]
let listlist2 = [list3]
let listlistlist = [listlist1, listlist2]
// print what we have produced
print(listlistlist)
// "navigate" into the structure, one step at a time
let result1 = listlistlist[0]
print(result1)
let result2 = result1[1]
print(result2)
let result3 = result2[2]
print(result3)
// indexing is left-associative, navigate in one step
let result012 = listlistlist[0][1][2]
print(result012)
如果您不确定 left-associative 读的是什么 Operator associativity。
在上面的例子中,最终结果是一个数字,从你之前的
在您的问题 map
通过对现有数组的每个元素应用函数来生成新数组。
如果像 map
这样按照某种模式处理整个数组的函数存在于库中,库中可能没有其他函数也按照其他模式处理整个数组吗?因此,请查找 map
的文档,然后通读文档的那个区域,看看您找到了什么——您可能不会失望,并且离您的年值求和目标更近了一步。
如果您在旅程的中途遇到困难,请提出一个新问题,包括您到那时为止设计的代码,描述您遇到的错误或您无法弄清楚的步骤,并包括一个link 回到这个问题,以便读者可以阅读您的问答。下一步肯定会有人帮助你。
继续学习、阅读和实践! HTH
我终于找到了问题的答案:
struct Movie {
var title = ""
var year = 0
var isImportant: Bool
var isFinished: Bool
init(title: String, year: Int, isImportant: Bool, isFinished: Bool) {
self.title = title
self.year = year
self.isImportant = isImportant
self.isFinished = isFinished
}
static let list1 = [
Movie(title: "The Shawshank Redemption", year: 1994, isImportant: false, isFinished: false),
Movie(title: "The Godfather", year: 1972, isImportant: false, isFinished: false),
Movie(title: "The Dark Knight", year: 2008, isImportant: false, isFinished: false),
Movie(title: "The Godfather: Part II", year: 1974, isImportant: false, isFinished: false),
Movie(title: "Avengers: Infinity War", year: 2018, isImportant: false, isFinished: false),
Movie(title: "Schindler's List", year: 1993, isImportant: false, isFinished: false)
]
static let list2 = [
Movie(title: "The Lord of the Rings: The Fellowship of the Ring", year: 2001, isImportant: false, isFinished: false),
Movie(title: "Inception", year: 2010, isImportant: false, isFinished: false),
Movie(title: "Forrest Gump", year: 1994, isImportant: false, isFinished: false),
Movie(title: "Fight Club", year: 1999, isImportant: false, isFinished: false)
]
static let list3 = [list1, list2]
static let nomsDesRepas: [String] = ["Petit Déjeuner", "Collation 11h"]
}
var list2Dim = Movie.list3
func sumSectionCalorie(Liste liste: Int) -> Int {
var total = 0
let number = list2Dim[liste].count
for xxx in 0 ..< number {
total += list2Dim[liste][xxx].year
}
return total
}
let total = sumSectionCalorie(Liste: 0)
print(total)
谢谢CARD给我提示