以下代码的复杂性是什么?
set<int> S1, S2, ans;
set_intersection(S1.begin(), S1.end(), S2.begin(), S2.end(), inserter(ans, ans.begin()))
Run Code Online (Sandbox Code Playgroud)
where S1和S2是一些non_empty集,ans是一个空集.
我知道将一个排序范围插入一个集合是线性的; 但是也使用插入线性插入?
该包的当前版本gmp不支持集合操作,例如intersect、setdiff等。我正在使用数字序列进行一些工作(有关示例,请参阅OEIS)并且需要处理大整数的大集合。我目前一直坚持使用各种循环来生成所需的差异或交叉点;虽然我可能可以生成编译的(Rccp 等)代码,但我希望在现有的函数和包中找到一种方法R。
假设我们有一个data1和data2.我该如何与他们相交std::set_intersect()?
struct pID
{
int ID;
unsigned int IDf;// postition in the file
pID(int id,const unsigned int idf):ID(id),IDf(idf){}
bool operator<(const pID& p)const { return ID<p.ID;}
};
struct ID{};
struct IDf{};
typedef multi_index_container<
pID,
indexed_by<
ordered_unique<
tag<IDf>, BOOST_MULTI_INDEX_MEMBER(pID,unsigned int,IDf)>,
ordered_non_unique<
tag<ID>,BOOST_MULTI_INDEX_MEMBER(pID,int,ID)> >
> pID_set;
ID_set data1, data2;
Load(data1); Load(data2);
pID_set::index<ID>::type& L1_ID_index=L1.data.get<ID>();
pID_set::index<ID>::type& L2_ID_index=L2.data.get<ID>();
// How do I use set_intersect?
Run Code Online (Sandbox Code Playgroud) 很抱歉在hadoop用户邮件列表和此处交叉发布此内容,但这对我来说非常紧迫.
我的问题如下:我有两个输入文件,我想确定
例:
File 1:
a
b
c
File 2:
a
d
Run Code Online (Sandbox Code Playgroud)
每种情况的期望输出:
lines_only_in_1: 2 (b, c)
lines_only_in_2: 1 (d)
lines_in_both: 1 (a)
Run Code Online (Sandbox Code Playgroud)
基本上我的方法如下:我编写了自己的LineRecordReader,以便映射器接收一对由行(文本)和一个指示源文件(0或1)的字节组成的对.映射器只返回该对,所以它实际上什么都不做.然而,副作用是,组合器接收到
Map<Line, Iterable<SourceId>>
Run Code Online (Sandbox Code Playgroud)
(其中SourceId为0或1).
现在,对于每一行,我可以得到它出现的源集.因此,我可以编写一个组合器,计算每种情况(a,b,c)的行数(清单1)
然后组合器仅在清理时输出"摘要"(这是安全吗?).所以这个总结如下:
lines_only_in_1 2531
lines_only_in_2 3190
lines_in_both 901
Run Code Online (Sandbox Code Playgroud)
在减速器中,我只总结了这些摘要的值.(因此减速器的输出看起来与组合器的输出一样).
但是,主要问题是,我需要将两个源文件视为单个虚拟文件,该文件生成表单(line,sourceId)// sourceId 0或1的记录
而且我不确定如何实现这一目标.所以问题是我是否可以事先避免预处理和合并文件,并使用像虚拟合并文件阅读器和自定义记录阅读器这样的东西即时执行.任何代码示例都非常感谢.
最好的问候,克劳斯
清单1:
public static class SourceCombiner
extends Reducer<Text, ByteWritable, Text, LongWritable> {
private long countA = 0;
private long countB = 0;
private long countC = 0; // C = lines (c)ommon to both sources
@Override
public …Run Code Online (Sandbox Code Playgroud) 因此,存在一种通过set.intersection()计算两个集合的交集的简单方法.但是,我有以下问题:
class Person(Object):
def __init__(self, name, age):
self.name = name
self.age = age
l1 = [Person("Foo", 21), Person("Bar", 22)]
l2 = [Person("Foo", 21), Person("Bar", 24)]
union_list = list(set(l1).union(l2))
# [Person("Foo", 21), Person("Bar", 22), Person("Bar", 24)]
Run Code Online (Sandbox Code Playgroud)
(Object是我的ORM提供的基类,它实现了基本__hash__和__eq__功能,它基本上将类的每个成员添加到哈希.换句话说,__hash__返回的将是类的每个元素的哈希)
在这个阶段,我想只运行一个集合交叉运算.name,比如说Person('Bar', -1).intersection(union_list) #= [Person("Bar", -1), Person("Bar", 22), Person("Bar", 24)].(典型的.intersection()在这一点上不会给我什么,我不能重写__hash__或者__eq__对Person类,因为这会覆盖原有的并集(我认为)
在Python 2.x中执行此操作的最佳方法是什么?
编辑:请注意,该解决方案不具有依靠set.但是,我需要找到工会然后交叉点,所以感觉这对于一套是合适的(但我愿意接受使用你认为值得的任何魔法的解决方案,只要它能解决我的问题!)
首先,我的目的是随机获取两个已知集合中的一个元素.所以我的原始方法首先交叉两组.然后从相交集中随机拾取一个元素.但这是愚蠢的,因为我只需要一个元素而不是相交集.
所以我需要找到set.intersection()的算法.
我比较了'set.intersection()'和'for {for {}}'方法之间的成本时间.Set.intersection()比其他(100次)更快.所以使用'for {for {}}'来获取随机元素并不是一个明智的想法.
python中set.intersection()背后的算法是什么?
给定N排序的整数数组(没有重复),我想计算limit它们交集中的第一个整数.
例如,给定以下数组:
[2, 5, 7, 8, 10, 12, 13, 15, 20, 24]
[3, 4, 5, 6, 9, 10, 11, 17, 20]
[1, 2, 3, 5, 6, 10, 12, 20, 23, 29]
Run Code Online (Sandbox Code Playgroud)
交叉点是[5, 10, 20],所以如果limit = 2结果应该是[5, 10].
不应该改变给定的数组.
我的尝试如下.这里的游乐场.
是否有更有效(更快)的方法来实现这一目标?
会欣赏一个jsperf比较.
function intersection(sortedArrays, limit) {
var arraysCount = sortedArrays.length;
var indices = sortedArrays.map(function(array) { return 0; });
var values, maxValue, valuesAreSame, reachedEnd, i, result = [];
while …Run Code Online (Sandbox Code Playgroud) 我试图在 Python(使用 Matplotlib)中找到两个圆圈之间的交点,但无法取回任何值。
我通过为每个单独的圆创建 X 和 Y 的列表来做到这一点(Matplotlib 在绘制圆时将第一个参数作为 X 值,第二个作为 Y 值),然后相应地将列表相交(例如, circle1 x 值与 circle2 x 值)。
import numpy
import math
import matplotlib.pyplot as plt
import random
def origin_circle():
global x_points
global y_points
global r
global n
r=1
n=2**16
x_points=[(r*math.cos(t)) for t in numpy.linspace(0, 2*numpy.pi*r, n+1)]
y_points=[(r*math.sin(t)) for t in numpy.linspace(0, 2*numpy.pi*r, n+1)]
def new_circle(x_offset, y_offset):
global x_points1
global y_points1
x_points1=[x_offset+(r*math.cos(t)) for t in numpy.linspace(0, 2*numpy.pi*r, n+1)]
y_points1=[y_offset+(r*math.sin(t)) for t in numpy.linspace(0, 2*numpy.pi*r, n+1)]
origin_circle()
new_center= random.randint(0, len(x_points)) …Run Code Online (Sandbox Code Playgroud) 我需要快速找出两个给定集合中存在多少个整数。这些集合只被写入一次,但这个操作将使用不同的集合对执行多次。这些集合包含 5-30 个整数,这些整数中最大的一个是 840000。
我最初尝试迭代一个Vec元素,并为每个元素检查它是否存在于另一个Vec. 然后我决定BTreeSet改用它,因为它在检查集合中是否存在整数时应该明显更快,但情况似乎并非如此。Vec在稳定的 Rust 1.34 下,在稳定的 Rust 1.34 下以发布模式调用数千个集合时,该实现需要 ~72ms 和 BTreeSet ~96ms,并且在夜间使用时具有相同的性能。
这是Vec实现:
use std::cmp;
fn main() {
let mut sets = Vec::with_capacity(1000);
for i in 1..1000 {
let mut set = Vec::new();
for j in 1..i % 30 {
set.push(i * j % 50000);
}
sets.push(set);
}
for left_set in sets.iter() {
for right_set in sets.iter() {
calculate_waste(left_set, right_set);
}
}
}
fn calculate_waste(left_nums: &Vec<usize>, …Run Code Online (Sandbox Code Playgroud) 我想计算集合之间的重叠系数。我的数据是一个 2 列表,例如:
\ndf_example <- \n tibble::tribble(~my_group, ~cities,\n "foo", "london",\n "foo", "paris", \n "foo", "rome", \n "foo", "tokyo",\n "foo", "oslo",\n "bar", "paris", \n "bar", "nyc",\n "bar", "rome", \n "bar", "munich",\n "bar", "warsaw",\n "bar", "sf", \n "baz", "milano",\n "baz", "oslo",\n "baz", "sf", \n "baz", "paris")\nRun Code Online (Sandbox Code Playgroud)\n在 中df_example,我有 3 个集合(即 、foo、bar)baz,每个集合的成员在cities。
我希望最终得到一个与所有可能的集合对相交的表,并指定每对中较小集合的大小。这将导致计算重叠系数每对集合的
\n(重叠系数=共同成员数/较小集合的大小)
\n所需输出
\n## # A tibble: 3 \xc3\x97 4\n## combination n_instersected_members size_of_smaller_set …Run Code Online (Sandbox Code Playgroud) set-intersection ×10
algorithm ×3
python ×3
set ×3
c++ ×2
intersection ×2
r ×2
arrays ×1
b-tree ×1
boost ×1
dplyr ×1
gmp ×1
hadoop ×1
java ×1
javascript ×1
multi-index ×1
python-2.x ×1
rust ×1
vector ×1