首页 > 很难?第一列或第二列相同的行合并?

很难?第一列或第二列相同的行合并?

某一行中的字母,如果有在其他行也出现,则将他们合并为一行。
例如

A B
C A
D C
E F
N G
C N

结果

A B C D N G
E F

#!/usr/bin/env python
# -*- coding: utf-8 -*-
result = {}
with open('5.1.txt', 'r') as f:
    alist = []
    blist = []
    lines = f.readlines()
    for line in lines:
        line1 = line.strip().split()

        for i in line1:
            blist.append(i)
            if i not in alist:
                alist.append(i)
    for a in alist:
        print a, blist.count(a)

也不是說很容易拉,先寫一個粗糙的版本(代碼又醜順序又完全不管XD),再來慢慢改進吧:

char2id = {}
id2charset = {}

def getcharset(c):
    try:
        return id2charset[char2id[c]]
    except:
        return None

def newcharset(chars):
    newset = set(chars)
    return newset

def merge(charset1, charset2):
    if id(charset1)==id(charset2):
        return
    charset1 |= charset
    for c in charset2:
        char2id[c] = id(charset1)
    id2charset.pop(id(charset2))

with open('test2') as reader:
    for line in reader:
        chars = line.strip().split()
        newset = newcharset(chars)
        id2charset[id(newset)] =newset

        for c in chars:
            charset = getcharset(c)
            if charset:
                merge(newset, charset)
            else:
                char2id[c] = id(newset)

with open('report', 'w') as writer:
    for id, charset in id2charset.items():
        print(' '.join(charset), file=writer)

資料 test:

A B
C A
D C
E F
N G
C N
X Y
F P
P Q
X Z

結果:

P E Q F
X Y Z
B D C G N A

建議你檢查的 script 這樣寫就好:

from collections import Counter

with open('report', 'r') as reader:
    ct = Counter()
    for line in reader:
        ct += Counter(line.strip().split())

for item, count in ct.most_common():
    if count <= 1:
        break
    print(item, count)

我回答過的問題: Python-QA

【热门文章】
【热门文章】