0

我一直在尝试打印最常见的行并删除关于在第一个字段中有很多不同值的大文件中由制表符分隔的键值的重复项;

示例输入:

a|gofortheeyeboo    0.61
a|gofortheeyeboo    0.81
a|gofortheeyeboo    0.81
a|gofortheeyeboo    0.81
a|gofortheeyeboo    0.81
a|gofortheeyeboo    0.81
a|gofortheeyeboo    0.91
a|gofortheeyeboo-gone   0.07
a|gofortheeyeboo-gone   0.07
a|gofortheeyeboo-abouttogone    0.61
a|gofortheeyeboo-abouttogone    0.12
b|attaack-attack        0.07

不同键所需的输出:

a|gofortheeyeboo    0.81
a|gofortheeyeboo-gone   0.07
a|gofortheeyeboo-abouttogone    0.61
a|gofortheeyeboo-abouttogone    0.12
b|attaack-attack        0.07

到目前为止,管理在第二个制表符分隔的字段中获取最大值的输出,删除重复项;

awk -F '\t' '{ if (l[$1] <= $2) l[$1] = $2} END {for (i in l) print i"\t"l[i];}'

上面不需要的命令的输出;

a|gofortheeyeboo        0.91
a|gofortheeyeboo-abouttogone    0.61
b|attaack-attack        0.07
a|gofortheeyeboo-gone   0.07
4

2 回答 2

1
sort input | uniq -c | sort -nr | \
       awk 's[$2] == $1 { print $2,$3} !s[$2] { print $2,$3; s[$2]=$1; }'
于 2013-08-07T01:11:55.733 回答
-1
keys = {}

for line in sys.stdin:
    line = line.strip()

    k, v = line.split('\t')

    if k in keys:
        if v in keys[k]:
            keys[k][v] += 1
        else:
            keys[k][v] = 1
    else:
        keys[k] = {v: 1}

for k in keys:

    items = keys[k].items()

    # Some pair emerged more than once
    if any(map(lambda x: x[1] > 1, items)):
        # Calucalte max frequence
        freq = reduce(
            lambda acc, e: acc if acc[1] > e[1] else e, 
            items
            )[0]
        print '{0}\t{1}'.format(k, freq)
    # None pair emereged more than once
    else:
        # Print every pair
        for v in items:
            print '{0}\t{1}'.format(k, v[0])
于 2013-08-12T10:54:26.447 回答