迭代时附加到数组

时间:2016-11-18 04:16:43

标签: python python-2.7 python-3.x

我试图创建一个循环遍历条目数组的python脚本,并添加一个带有日期的新日对象,如果该日期不在日期列表中。

entryList = [aDate,anotherDate,fooDate]
history = [aDate]

for entry in entryList:
  for day in history[:]:
    if day.date == entry.date:
      break
    else:
      dayEntry = dayEntry()
      dayEntry.date = entry.date
      history.insert(0,dayEntry)
      break

根据这个:https://docs.python.org/3/tutorial/controlflow.html它应该有用,但我错过了一些东西。

感谢 -

2 个答案:

答案 0 :(得分:2)

因此,您想要history entryListnot any()的条目,但仅限于给定日期的第一个条目?

我认为这是for entry in entryList: if not any(day.date == entry.date for day in history): dayEntry = dayEntry() dayEntry.date = entry.date history.insert(0,dayEntry) 的一个案例。

not any(day.date == entry.date for day in history)

for entry in entryList: if entry.date not in history: dayEntry = dayEntry() dayEntry.date = entry.date history[dayEntry.date] = dayEntry

读作:历史记录中没有这个条目的日期。

如果允许历史记录是字典,其中键是条目的日期,而不是列表:

dates = set()
for entry in history:
   dates.add(entry.date)

for entry in entryList:
    if entry.date not in dates:
        dayEntry = dayEntry()
        dayEntry.date = entry.date
        history.insert(0,dayEntry)
        dates.add(entry.date)

另一个选择是使用一个集合以及两个列表:

select id,max(time) from test1 where depth<>0 group by id 

答案 1 :(得分:0)

我认为您的代码应该按原样运行:

import Foundation
import SpriteKit
import AVFoundation

/**Manages a shared instance of JKAudioPlayer.*/
private let JKAudioInstance = JKAudioPlayer()

/**Provides an easy way to play sounds and music. Use sharedInstance method to access
   a single object for the entire game to manage the sound and music.*/
open class JKAudioPlayer {

    /**Used to access music.*/
    var musicPlayer: AVAudioPlayer!
    var soundPlayer: AVAudioPlayer!

    /** Allows the audio to be shared with other music (such as music being played from your music app).
     If this setting is false, music you play from your music player will stop when this app's music starts.
     Default set by Apple is false. */
    static var canShareAudio = false {
        didSet {
            canShareAudio ? try! AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient)
                : try! AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategorySoloAmbient)
        }
    }

    /**Creates an instance of the JAAudio class so the user doesn't have to make
       their own instance and allows use of the functions. */
    open class func sharedInstance() -> JKAudioPlayer {
        return JKAudioInstance
    }

    /**Plays music. You can ignore the "type" property if you include the full name with extension
       in the "filename" property. Set "canShareAudio" to true if you want other music to be able
       to play at the same time (default by Apple is false).*/
    open func playMusic(_ fileName: String, withExtension type: String = "") {
        if let url = Bundle.main.url(forResource: fileName, withExtension: type) {
            musicPlayer = try? AVAudioPlayer(contentsOf: url)
            musicPlayer.numberOfLoops = -1
            musicPlayer.prepareToPlay()
            musicPlayer.play()
        }
    }

    /**Stops the music. Use the "resumeMusic" method to turn it back on. */
    open func stopMusic() {
        if musicPlayer != nil && musicPlayer!.isPlaying {
            musicPlayer.currentTime = 0
            musicPlayer.stop()
        }
    }

    /**Pauses the music. Use the "resumeMusic" method to turn it back on. */
    open func pauseMusic() {
        if musicPlayer != nil && musicPlayer!.isPlaying {
            musicPlayer.pause()
        }
    }

    /**Resumes the music after being stopped or paused. */
    open func resumeMusic() {
        if musicPlayer != nil && !musicPlayer!.isPlaying {
            musicPlayer.play()
        }
    }

    /**Plays a single sound.*/
    open func playSoundEffect(named fileName: String) {
        if let url = Bundle.main.url(forResource: fileName, withExtension: "") {
            soundPlayer = try? AVAudioPlayer(contentsOf: url)
            soundPlayer.stop()
            soundPlayer.numberOfLoops = 1
            soundPlayer.prepareToPlay()
            soundPlayer.play()
        }
    }
}