0

テキスト ファイルを開き、すべての行をループして、それぞれを個人的に慣れた辞書に並べ替えます。

def load(fileName):
    file = open(fileName+'.txt')
    for line in file:
        row = line.split()
        id = int(row[0])
        number = int(row[2])
        values = [int(row[3]),int(row[4]),int(row[5]),int(row[6])]
        dict = {number:[id, values]}
        print(dict)

次の行にnumberandの値が重複しているかどうかを確認idし、それに従ってグループ化して並べ替えたいと思います。

すべての辞書を1つのリストに入れてから、特定の方法で操作するのが良い解決策だと確信していますが、それぞれdictを別のリストに入れるだけでは機能しないようです。

lineに似たもの、.nextLine()またはのindex=0反復ごとに増加するものを使用して、すべての重複をチェックするにはどうすればよいline in fileですか?

入力例:

1772 320 548 340 303 20 37 1
1772 320 551 337 306 22 37 1
1772 320 551 337 306 22 37 1
1772 320 551 337 306 22 37 1
1772 320 552 336 307 22 37 1
1772 320 553 335 308 22 37 1
1772 320 554 335 309 20 37 1
1783 345 438 31 436 40 36 1
1783 345 439 33 434 40 36 1
1783 345 440 35 432 40 36 1
1783 345 441 38 430 40 36 1
1783 345 442 39 431 40 36 1
1783 345 443 41 429 40 36 1
1783 345 444 44 428 40 36 1

出力例:

{548: [1772, [340, 303, 20, 37]]}
{551: [1772, [337, 306, 22, 37]]}
{551: [1772, [337, 306, 22, 37]]}
{551: [1772, [337, 306, 22, 37]]}
{552: [1772, [336, 307, 22, 37]]}
{553: [1772, [335, 308, 22, 37]]}
{554: [1772, [335, 309, 20, 37]]}
{438: [1783, [31, 436, 40, 36]]}
{439: [1783, [33, 434, 40, 36]]}
{440: [1783, [35, 432, 40, 36]]}
{441: [1783, [38, 430, 40, 36]]}
{442: [1783, [39, 431, 40, 36]]}
{443: [1783, [41, 429, 40, 36]]}
{444: [1783, [44, 428, 40, 36]]}
4

3 に答える 3

1
from collections import OrderedDict as od
with open("abc") as f:
   dic = od()
   for line in f:
       row  = map(int,line.split())
       idx, num  = row[2], row[0]
       val = [num] + [row[3:-1]]
       dic.setdefault(idx,[]).append(val)

for k,v in dic.items():
    for val in v:
        print k,val

出力:

548 [1772, [340, 303, 20, 37]]
551 [1772, [337, 306, 22, 37]]
551 [1772, [337, 306, 22, 37]]
551 [1772, [337, 306, 22, 37]]
552 [1772, [336, 307, 22, 37]]
553 [1772, [335, 308, 22, 37]]
554 [1772, [335, 309, 20, 37]]
438 [1783, [31, 436, 40, 36]]
439 [1783, [33, 434, 40, 36]]
440 [1783, [35, 432, 40, 36]]
441 [1783, [38, 430, 40, 36]]
442 [1783, [39, 431, 40, 36]]
443 [1783, [41, 429, 40, 36]]
444 [1783, [44, 428, 40, 36]]
于 2013-05-30T22:30:09.557 に答える
1
d = dict()
with open ("input") as f:
    for line in f:
        line = line.rstrip(" \n")
        row = line.split()
        if len(row) < 7: continue
        idx = int(row[0])
        number = int(row[2])
        values = [int(row[3]),int(row[4]),int(row[5]),int(row[6])]
        key = str(number) + ":" + str(idx)

        # add values corresponding to same number, idx pairs to ...
        # a list referenced by d[number:idx]

        if key not in d: d[key] = []
        d[key].append(values)

for key in d:
    n,i = key.split(":")
    # print out rows with number n and idx i
    for row in d[key]:
        print n, i, ",".join(str(x) for x in row)

出力:

551 1772 337,306,22,37
551 1772 337,306,22,37
551 1772 337,306,22,37
553 1772 335,308,22,37
552 1772 336,307,22,37
548 1772 340,303,20,37
554 1772 335,309,20,37
于 2013-05-31T00:03:57.927 に答える