【问题标题】:In python how can I find all the values in an array that are in between two specific values在 python 中,如何找到数组中两个特定值之间的所有值
【发布时间】:2016-08-08 14:59:12
【问题描述】:

嗨,我想遍历数组的某些值让我们说

spikeTimes = [1,2,3,4,5,6,7,8]
stimTimes =[0.5, 4.5, 7.5]
WIN_SIZE=2

我希望stimTimes 的每个元素都能够获得介于stimTimes[indx]stimTimes[indx]+WIN_SIZE 之间的值

我应该能够得到第一个 peakTimes(0.5) - 1,2(0.5-2.5 之间)的值 对于第二个值 (4.5) - 5,6(介于 4.5- 4.5+WIN_SIZE=6.5 之间的值) 第三个值(7.5) - 8

【问题讨论】:

  • 你为什么大喊大叫?
  • @idjaw 也许他正在以 SQL 查询的形式编写它;)。
  • @PeterDavidCarter 也许我应该添加 sql 标签:P

标签: python arrays loops find


【解决方案1】:

这可以通过嵌套列表推导来完成

spikeTimes = [1, 2, 3, 4, 5, 6, 7, 8]

stimTimes = [0.5, 4.5, 7.5]

WIN_SIZE = 2

partitions = [[spike for spike in spikeTimes if stim <= spike <= stim + WIN_SIZE] for stim in stimTimes]

print(partitions)

生产:

[[1, 2], [5, 6], [8]]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 2013-02-02
    • 1970-01-01
    • 1970-01-01
    • 2022-01-16
    • 2020-09-23
    • 2011-10-28
    相关资源
    最近更新 更多