- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个表示年份范围的整数对列表,需要计算连续(1 年内)对的联合范围。
输入示例:
ts_list = [[1777, 1777], [1778, 1783], [1786, 1791], [1792, 1795]]
所需输出
[[1777, 1781], [1786, 1795]]
我已经尝试过 for 和 while 循环,并且可以在第一次不相交之前获得并集,但我对如何正确迭代感到困惑 - 例如这会产生一个新列表
[[1777, 1783], [1778, 1783], [1786, 1795]]
然后返回类型错误:'int'对象不可下标”
。第一对和第三对是正确的,但第二对是无关的
ts_list = [[1777, 1777], [1778, 1781], [1786, 1791], [1792, 1795]]
newlist=[]
last = ts_list[len(ts_list)-1][1]
for x in range(len(ts_list)):
ts=ts_list[x]
start = ts[0]
end = ts[1]
ts_next = ts_list[x+1] if x<len(ts_list)-1 else last
if ts_next[0]-end > 1:
# next is disjoint, break out
newlist.append([start,end])
else:
# next is contiguous (within 1 year)
newlist.append([start,ts_next[1]])
最佳答案
你可以这样做:
ts_list = [[1777, 1777], [1778, 1781], [1786, 1791], [1792, 1795]]
# We start with the first range
out = [ts_list[0]]
for start, end in ts_list[1:]:
if start <= out[-1][1] + 1:
# if the new range starts at most one year
# after the end of the previous one, we extend it:
out[-1][1] = end
else:
# otherwise, we append this new range to the output
out.append([start, end])
print(out)
# [[1777, 1781], [1786, 1795]]
关于python - 如何迭代整数对列表,计算新的 'union pairs',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55434497/
我正在学习使用 STL 的排序函数,方法是将它用于一些复杂的对 vector 。 我有以下 vector : vector > > > > 我需要先根据对中的第一个整数对元素进行排序,如果发现有 2
我想初始化: pair>,vector>> pvp; 所以对于所有的我: pvp.first[i].first = true; 和 pvp.second[i].first = false; 我知道您可
那为什么我们能实例化Pair却不能实例化Pair Pair p=new Pair(); 对比 Pair p=new Pair(); 我知道 意思是未知类型 --> 但不是 意思是一样的---> 有
这个问题在这里已经有了答案: How to have an unordered_map where the value type is the class it's in? (1 个回答) `std
我正在编写一个代码来处理warehouse[item[batch, qty]]的组合,然后将基于[batch, qty]的batch与qty的总和分组。我的代码是: package main impo
我想知道最好的类 java 容器是什么 > 我有一个作为键的 object1 以及一个 t1 和 t2。 我的类具有以下属性: public class Patient implements Exte
所以我有一些使用 Java 8 流的代码,而且它可以工作。它做的正是我需要它做的,而且清晰易读(这在函数式编程中很少见)。在子例程结束时,代码遍历自定义对类型的列表: // All names Hun
我正在声明一个字符串映射到一对对,如下所示: std::map, std::pair>> reference; 我将其初始化为: reference.insert
例如,镜像 pair 至 pair ,我可以像这样创建一个模板函数: template void mirror(const AB& ab,BA& ba){ ba.first=ab.secon
我正在使用 PyZMQ 创建一个简单的 PAIR/PAIR 通信原型(prototype)消息传递模式。 配对服务器 import zmq import random import sys impor
我正在尝试习惯 Kotlin 中的习语和快捷方式,我想知道是否有任何方法可以做到这一点。 val pairList = listOf(Pair(1, 2), Pair(5, 10), Pair(12,
我定义的 map 是这样的 map > hmap; 如果有一个pair(2,pair(3,4))如何得到2 3 4个值,itr->first, itr->第二个不工作 最佳答案 If there is
我希望能够对以下 vector 进行排序 -vector>> 基于 pair 的第一个元素,如果它们相等,则根据它们的第二个元素对它们进行排序,我如何使用 STL 在 C++ 中做到这一点构建? 这种
通过 PHP_PDO: fetchAssoc echo 从 MySql SELECT 查询得到一个 $.getJSON 结果使用 json_encode() 编辑,在 firebug 控制台中的输出如
假设我有以下功能: (defun f (v1 v2) ...) 我想简化以下代码: (lambda (pair) (apply #'f pair)) 此处的目标是创建一个函数,该函数接受两个值的列表并
对不起,我真的不知道怎么写这个标题。我可以想到这样做的代价高昂的方法,但我想看看是否有人可以指出一个优雅的解决方案,这里是: 我有很多成对关联的元素; 每个元素都有一个与其自身关联的数值,该数值表示它
我有一个使用以下方法用 Java 编写的 PreferencesManager: public void insert(Pair keyValue, boolean async) { 我正尝试从 k
这是 assigning-of-unordered-map-to-pair-of-objects 的后续问题.这是一个关于编译器错误解释的问题(而不是一个重复的问题,因为该问题已经得到了完整的回答)。
这是我的 map : map, pair > matchMap; 这是函数: void Schedule::studentSchedule() { string s, c; cout
#include #include #include using namespace std; int main() { map,pair> items; items.inser
我是一名优秀的程序员,十分优秀!