37,741
社区成员
发帖
与我相关
我的任务
分享#!python
# encoding: utf-8
content = '''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'''
store = {}
for ln in content.splitlines():
tm, vl = ln.strip().split(' ')
store[vl] = tm
for vl, tm in store.items():
print tm, vl
#!python
# encoding: utf-8
content = '''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'''
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], vroot@lake-pc:/var/awk# cat test.txt
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
root@lake-pc:/var/awk# awk '{a[$2]=$1};END{for(i in a) print a[i],i;}' test.txt
14:00 1
13:00 2
16:00 3
root@lake-pc:/var/awk# 

#!/usr/bin/env perl
use 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 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