3

按第一个索引遍历排序列表的最优雅的方法是什么?输入:

Meni22   xxxx xxxx
Meni32_2 xxxx xxxx
Meni32_2 xxxx xxxx
Meni45_1 xxxx xxxx
Meni45_1 xxxx xxxx
Meni45   xxxx xxxx

是不是一行一行地走下去:

list1 = []
list2 = []
for line in input:
    if line[0] not in list1:
    list.append(line)
else:
    list2.append(line)

示例显然行不通。它添加 line[0] 的第一个匹配项并继续。我宁愿让它通过列表,添加到 list1 中它只找到一次的行,然后休息到 list2。

脚本后:

List1:

Meni22   xxxx xxxx
Meni45   xxxx xxxx

List2: 

Meni45_1 xxxx xxxx
Meni45_1 xxxx xxxx
Meni32_2 xxxx xxxx
Meni32_2 xxxx xxxx
4

3 回答 3

3

由于文件已排序,您可以使用groupby

from itertools import groupby
list1, list2 = res = [], []
with open('file1.txt', 'rb') as fin:
    for k,g in groupby(fin, key=lambda x:x.partition(' ')[0]):
        g = list(g)
        res[len(g) > 1] += g

或者,如果您更喜欢这个更长的版本

from itertools import groupby
list1, list2 = [], []
with open('file1.txt', 'rb') as fin:
    for k,g in groupby(fin, key=lambda x:x.partition(' ')[0]):
        g = list(g)
        if len(g) > 1:
            list2 += g
        else:
            list1 += g
于 2013-05-14T10:36:48.780 回答
2

您可以使用collections.Counter

from collections import Counter
lis1 = []
lis2 = []
with open("abc") as f:
    c = Counter(line.split()[0] for line in f)

for key,val in c.items():
    if val == 1:
        lis1.append(key)
    else:
        lis2.extend([key]*val)
print lis1
print lis2

输出:

['Meni45', 'Meni22']
['Meni32_2', 'Meni32_2', 'Meni45_1', 'Meni45_1']

编辑:

from collections import defaultdict
lis1 = []
lis2 = []

with open("abc") as f:
    dic = defaultdict(list)
    for line in f:
        spl =line.split()
        dic[spl[0]].append(spl[1:])

for key,val in dic.items():
    if len(val) == 1:
        lis1.append(key)
    else:
        lis2.append(key)
print lis1
print lis2

print dic["Meni32_2"]  #access columns related to any key from the the dict

输出:

['Meni45', 'Meni22']
['Meni32_2', 'Meni45_1']
[['xxxx', 'xxxx'], ['xxxx', 'xxxx']]
于 2013-05-14T10:38:58.820 回答
1

考虑使用difflib

import difflib

d = difflib.Differ()
fa = open('a.txt'); fb = open('b.txt')

diff = d.compare("".join(fa.readlines()), "".join(fb.readlines()))
print ''.join(list(diff))

fa.close(); fb.close()
于 2013-05-14T10:33:07.020 回答