兩個目的:
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"; } }