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";
    }
}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM