您应该能够使用SQL Join获得您想要的东西。示例 SQLite 代码:
.headers on
.separator "\t"
create temporary table 'buyers' (
buyer integer primary key,
wants char(1)
);
insert into buyers(wants) values('a');
insert into buyers(wants) values('b');
insert into buyers(wants) values('c');
insert into buyers(wants) values('d');
insert into buyers(wants) values('e');
create temporary table 'sellers' (
seller integer primary key,
offers char(1)
);
insert into sellers(offers) values ('a');
insert into sellers(offers) values ('b');
insert into sellers(offers) values ('b');
insert into sellers(offers) values ('e');
select
buyers.buyer as buyer,
sellers.seller as seller,
buyers.wants as good
from
buyers left outer join sellers
on
buyers.wants = sellers.offers
;
输出:
买家卖家好
1 1个
2 2 乙
2 3 乙
3℃
4天
5 4 e
在 Perl 中做类似的事情:
#!/usr/bin/env perl
use strict;
use warnings;
use Graph::Undirected;
my $g = Graph::Undirected->new(unionfind => 1);
# buyers
$g->add_edge(b1 => 'a');
$g->add_edge(b2 => 'b');
$g->add_edge(b3 => 'c');
$g->add_edge(b4 => 'd');
$g->add_edge(b5 => 'e');
# sellers
$g->add_edge(s1 => 'a');
$g->add_edge(s2 => 'b');
$g->add_edge(s3 => 'b');
$g->add_edge(s4 => 'e');
use YAML;
print Dump [ $g->connected_components ];
输出:
-
- 一个
- b1
- s1
-
- s2
- b
- s3
- b2
-
- b4
-d
-
-e
- b5
- s4
-
- b3
- C