求教如何取出重复N行中的最后一行
这么一段数据
10:00 1
10:30 2
11:00 3
12:00 2
12:30 1
13:00 2
14:00 1
15:00 3
16:00 3
第二列的规律是 有且只有3个值是相等的
如会有3个1 3个2 3个3类似这样的很多很多
第一列其实是时间 规律是从上到下依次递增 按时间顺序
现在想取出3个1中 3个2中 3个3中 时间最晚的那个
也就是位置最靠下的那个
结果应为
14:00 1
13:00 2
16:00 3
[解决办法]
#!/usr/bin/env perluse strict;use warnings;my %hash;while (<DATA>) { my ($time, $data) = split; my $old = $hash{$data}; if (defined $old) { $hash{$data} = $time if $time gt $old; } else { $hash{$data} = $time; }}for my $key (sort keys %hash) { print "$hash{$key} $key\n";} __DATA__10:00 110:30 211:00 312:00 212:30 113:00 214:00 115:00 316:00 3
[解决办法]
root@lake-pc:/var/awk# cat test.txt10:00 110:30 211:00 312:00 212:30 113:00 214:00 115:00 316:00 3root@lake-pc:/var/awk# awk '{a[$2]=$1};END{for(i in a) print a[i],i;}' test.txt14:00 113:00 216:00 3root@lake-pc:/var/awk#
[解决办法]
#!python# encoding: utf-8content = '''10:00 110:30 211:00 312:00 212:30 113:00 214:00 115:00 316:00 3'''store = {}for ln in content.splitlines(): tm, vl = ln.strip().split(' ') store.setdefault(vl, []).append(tm)for v in store: if len(store[v])==3: print store[v][-1], v