这是两个清单:
a= [[1,2,3], [4,5,6]]
b= ["Python", "Java"]
所以我想要这个结果:
X = [[1,2,3], "python"]
Y = [[4,5,6], "Java"]
它必须成为一种功能:例如:
def zipp (a,b):
for same in zip(a,b):
return (i)
print (zipp(a,b))
但是,当我用print(i)替换函数中的return(i)时,它打印出我想要的东西......但遗憾的是它只返回2中的1个。我怎样才能返回两者?
答案 0 :(得分:2)
你可以这样做:
extension LobbyViewController: UITableViewDataSource {
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return categoryChunks.count
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cellType: GameCollectionCellType!
if let type = cellTypes[indexPath.item] {
cellType = type
} else {
var isNot: GameCollectionCellType? = nil
if let lastType = cellTypes[indexPath.item] {
isNot = lastType
}
cellType = GameCollectionCellType.randomItem(isNot)
cellTypes[indexPath.item] = cellType
}
let chunk = categoryChunks[indexPath.section][indexPath.row]
let cell = tableView.dequeueReusableCellWithIdentifier(GameCollectionViewCell.classIdentifier, forIndexPath: indexPath) as! GameCollectionViewCell
cell.configureWithGames(chunk)
let color = waveColors[indexPath.section % 2]
cell.fillColor = color
cell.clipsToBounds = true
return cell
}
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let lh = tableView.dequeueReusableCellWithIdentifier(LobbyHeader.classIdentifier) as! LobbyHeader
lh.fillColor = waveColors[section % 2]
lh.contentView.clipsToBounds = true
if categories.count > 0 {
let cat = categories[section]
lh.title.text = cat.name
lh.showMore.setTitle("%@ items".localizedStringWithParameters(["\(cat.games.count)"]), forState: .Normal)
lh.tag = section
} else {
lh.title.text = ""
}
lh.sizeToFit()
return lh
}
func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
let lf = tableView.dequeueReusableCellWithIdentifier(LobbyFooter.classIdentifier) as! LobbyFooter
let i = section % 2 == 0 ? 1 : 0
let color = waveColors[i]
lf.waveView.fillColor = color
lf.waveView.clipsToBounds = true
lf.contentView.clipsToBounds = true
let bgColor = waveColors[section%2]
lf.contentView.backgroundColor = bgColor
return lf
}
func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 30
}
func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 10
}
}
它必须成为一种功能......
请注意,您直接使用class LobbyHeader: UITableViewCell {
@IBOutlet weak var title: UILabel!
@IBOutlet weak var showMore: UIButton!
var fillColor: UIColor = UIColor.clearColor() {
didSet {
contentView.backgroundColor = fillColor
}
}
var showGames: (()->())?
class func nibForView() -> UINib {
return UINib(nibName: classIdentifier, bundle: nil)
}
override func prepareForReuse() {
super.prepareForReuse()
title.text = ""
showMore.setTitle("", forState: .Normal)
}
override func awakeFromNib() {
super.awakeFromNib()
backgroundColor = BackgroundColors.defaultBackgroundColor
}
@IBAction func showMoreAction(sender: UIButton) {
if let showGames = showGames {
showGames()
}
}
}
,而不定义任何额外的功能:
>>> def zipp (a,b):
... return [[x,y] for x, y in zip(a, b)]
>>> zipp(a, b)
[[[1, 2, 3], 'Python'], [[4, 5, 6], 'Java']]
答案 1 :(得分:0)
我认为你不明白这个功能是如何运作的。
调用函数时,如果函数返回值,则循环将不会继续。 i
不会同时返回。