python的列表综合list-comprehension示例,及两列表取补集

两个目的:

1. 了解了python的list comprehesion的用法

2. 了解了两个列表取交集和补集的方法

 

R语言取交集和补集更简单,直接有函数。

 

perl 稍麻烦一些, 关键是用hash!

#!/usr/bin/perl -w
use strict;

my @a = (1, 2, 3, 4);
my @b = (3, 4, 5);

my %hash;
for(@b)
{
    $hash{$_} = 1;
}

## complementary set
print "complentary set:\n";
for(@a)
{
    if(!defined $hash{$_})
    {
        print "$_\n";
    }
}

## intersect
print "intersect:\n";
for(@a)
{
    if(defined $hash{$_})
    {
        print "$_\n";
    }
}

 

posted on 2016-07-17 12:31  wangshaobin07  阅读(2449)  评论(0编辑  收藏  举报