求教如何取出重复N行中的最后一行

ai527518 2012-06-01 03:00:24
这么一段数据
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
...全文
114 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
ai527518 2012-06-01
  • 打赏
  • 举报
回复
哇塞 bash都行啊。。 多谢
tim_spac_126 2012-06-01
  • 打赏
  • 举报
回复
or
#!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 -u "test.py"
14:00 1
16:00 3
13:00 2
>Exit code: 0 Time: 0.065
tim_spac_126 2012-06-01
  • 打赏
  • 举报
回复
#!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], v


>python -u "test.py"
14:00 1
16:00 3
13:00 2
>Exit code: 0 Time: 0.066
foolbirdflyfirst 2012-06-01
  • 打赏
  • 举报
回复
root@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#

改写一下,开个dic啥的应该很简单吧,
ai527518 2012-06-01
  • 打赏
  • 举报
回复
大牛 小弟想用python。。。
bugs2k 2012-06-01
  • 打赏
  • 举报
回复
#!/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

37,741

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • WuKongSecurity@BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧