两个或多个list的交集,并集等的对比(List::Compare)

用法:

my @Llist = qw(abel abel baker camera delta edward fargo gofler); 
my @Rlist = qw(baker camera delta delta fargo hilton); 

my $lc = List::Compare->new(\@Llist,\@Rlist); 

my @intersection = $lc->get_intersection; 
my @union = $lc->get_union; 

print @intersection; 
print "\n"; 
print @union;

主要常用的方法:

     get_intersection()   #两个或者多个list的交集。 

      get_union()          # 两个或者多个list的并集。 

      get_unique()         #出现在第一个(at least once 指的是集合中可能会有重复的元素,在整个系列 
                             #中,都需要注意这样的case)集合,但不出现在第二个集合的list.LeftOrphan      

     get_complement()     # 出现在第二个集合,但不出现在第一个集合的list ( RightOrphan) 

     get_symmetric_difference() # LeftOrphan+RightOrphan的并集。 

      get_bag()                  #将会尽可能多的保存重复的元素,不像get_union,只保留一份copy.

new(),在将list组合在一起时,会自动的按照顺序来排序

$lc = List::Compare->new(\@Llist, \@Rlist); 
$lc = List::Compare->new('-u', \@Llist, \@Rlist);


参数-u的作用,每次compare, List::Compare都会按照Perl's default sort mode: ASCII-betical来进行排序,如果你不需要排序,而需要效率,则使用-u参数。
或者你可以使用

  #很多时候,我们并不需要知道里面每个元素,我们只需要一个判断。 

   is_LsubsetR(), Left list is a subset of Right list. 直接返回true. 

   is_ RsubsetL(),    Right List is a subset of Left list. 直接返回true. 

   is_LequivalentR(), 二者含有一样的元素,这里的相等,和perl中的相等不一样。下面两个集合也相等。
原文地址:https://www.cnblogs.com/tjxwg/p/2964275.html