如何添加2个列表python的int元素

时间:2015-09-28 22:13:36

标签: python list multidimensional-array

所以我对Python比较陌生。我有这段代码:

vel = [4, 3]

list = [[4, 5]
        [6, 4]
        [7, 5]
        [3, 4]
       ]

数字在一个范围内是随机的,但总是整数。 我想将vel列表添加到列表的每个子列表中,以便得到以下结果:

list = [[8, 8]
        [10, 7]
        [11, 8]
        [6, 8]
       ]

请告诉我'pythonic'这样做的方式。

编辑:我已经阅读了有关添加一维列表元素的其他主题,但我似乎无法使用二维列表。

2 个答案:

答案 0 :(得分:2)

看起来你想改变原始列表。

import UIKit
import AVFoundation

class ViewController: UIViewController {
@IBOutlet weak var PausePlay: UIButton!
var BackgroundAudio = AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("M.I.A.-DoubleBubbleTrouble", ofType: "mp3")!), error: nil)

override func viewDidLoad() {
    super.viewDidLoad()        
    BackgroundAudio.play()

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

@IBAction func Stop(sender: AnyObject) {
    if(BackgroundAudio.playing) {
    BackgroundAudio.stop()
    BackgroundAudio.currentTime = 0
    PausePlay.setTitle("Play", forState: .Normal)
    } else {
        BackgroundAudio.currentTime = 0
    }
}

@IBAction func Restart(sender: AnyObject) {
    if(BackgroundAudio.playing==false){
        PausePlay.setTitle("Pause", forState: .Normal)
    }
    BackgroundAudio.stop()
    BackgroundAudio.currentTime = 0
    BackgroundAudio.play()
}

@IBAction func PausePlay(sender: AnyObject) {
    if(BackgroundAudio.playing){
        BackgroundAudio.stop()
        PausePlay.setTitle("Play", forState: UIControlState.Normal)
    } else {
        BackgroundAudio.play()
        PausePlay.setTitle("Pause", forState: .Normal)
    }
}

if (BackgroundAudio.playing == false) {
PausePlay.setTitle("Play", forState: .Normal)
}   
}

答案 1 :(得分:2)

vel = [4, 3]
L = [[4, 5],
     [6, 4],
     [7, 5],
     [3, 4]]
answer = [[sum(s) for s in zip(vel, sub)] for sub in L]

PS:在数据类型(filelisttupleboolint等之后命名变量是一个糟糕的主意。 / p>