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