-1

次の行を持つファイルが1つあります。

B99990001 1 2 3 4
B99990001 1 3 3 4
B99990002 1 2 3 4
B99990002 1 3 3 4
B99990003 1 2 3 4
B99990003 1 3 3 4

したがって、ここでの目的は、行の最初の列 (B99990001、B99990002、B99990003) に基づいて 3 つのサブリストを持つメイン リストを作成することです。

Mainlist=[ 
          ['B99990001 1 2 3 4','B99990001 1 3 3 4'],#sublist1 has B99990001
          ['B99990002 1 2 3 4','B99990002 1 3 3 4'],#sublist2 has B99990002
          ['B99990002 1 2 3 4','B99990002 1 3 3 4'] #sublist3 has B99990002
                                                                                ]

私の質問が理解できることを願っています。だから誰かが知っているなら、あなたはこれから私を助けてくれます.

よろしくお願いします

ここで私の実際の例を見てください:

import os
import re
pdbPathAndName = ['/Users/Mahesh/Documents/MAHESH_INTERNSHIP_2014  /ENZOWP2/2WC5_090715_170128/E3P/E3P.B99990001.pdb','/Users/Mahesh/Documents/MAHESH_INTERNSHIP_2014/ENZOWP2/2WC5_090715_170128/E3P/E3P.B99990002.pdb']

''' /Users/Mahesh/Documents/MAHESH_INTERNSHIP_2014/ENZOWP2/2WC5_090715_170128/E3P/E3P.B99990001.pdb=[
                    'ATOM    138  SG  CYS    19       4.499   4.286   8.260  1.00 71.96           S',
                    'ATOM    397  SG  CYS    50      14.897   3.238   9.338  1.00 34.60           S',
                    'ATOM    424  SG  CYS    54       5.649   5.914   8.639  1.00 42.68           S',
                    'ATOM    774  SG  CYS    97      12.114  -6.864  23.897  1.00 62.23           S',
                    'ATOM    865  SG  CYS   108      15.200   3.910  11.227  1.00 54.49           S'    ]

/Users/Mahesh/Documents/MAHESH_INTERNSHIP_2014/ENZOWP2/2WC5_090715_170128/E3P/E3P.B99990002.pdb=[
                    'ATOM    929  SG  CYS   117      13.649  -6.894  22.589  1.00106.90           S',
                    'ATOM    138  SG  CYS    19       4.499   4.286   8.260  1.00 71.96           S',
                    'ATOM    397  SG  CYS    50      14.897   3.238   9.338  1.00 34.60           S',
                    'ATOM    424  SG  CYS    54       5.649   5.914   8.639  1.00 42.68           S',
                    'ATOM    774  SG  CYS    97      12.114  -6.864  23.897  1.00 62.23           S',
                    'ATOM    865  SG  CYS   108      15.200   3.910  11.227  1.00 54.49           S',
                    'ATOM    929  SG  CYS   117      13.649  -6.894  22.589  1.00106.90           S'    ] '''


for path in pdbPathAndName:
    f = open(path, 'r').readlines()
    f = map(lambda x: x.strip(), f)
    for line in f:
        if "SG" in line and line.endswith("S"):
             print (path.split("/")[-1] + "_" + re.split('\s+', line)[1] + ":" + re.split('\s+', line)[5] + ":" +re.split('\s+', line)[6] + ":" + re.split('\s+', line)[7])

#PRINTED OUTPUT
'''E3P.B99990001.pdb_138:6.923:0.241:6.116
   E3P.B99990001.pdb_397:15.856:3.506:8.144
   E3P.B99990001.pdb_424:8.558:1.315:6.627
   E3P.B99990001.pdb_774:14.204:-5.490:24.812
   E3P.B99990001.pdb_865:15.545:4.258:10.007
   E3P.B99990001.pdb_929:16.146:-6.081:24.770

   E3P.B99990002.pdb_138:4.499:4.286:8.260
   E3P.B99990002.pdb_397:14.897:3.238:9.338
   E3P.B99990002.pdb_424:5.649:5.914:8.639
   E3P.B99990002.pdb_774:12.114:-6.864:23.897
   E3P.B99990002.pdb_865:15.200:3.910:11.227
   E3P.B99990002.pdb_929:13.649:-6.894:22.589'''

  #MY EXPECTED OUTPUT 
''' MainlIst=[
            ['E3P.B99990001.pdb_138:6.923:0.241:6.116'
            'E3P.B99990001.pdb_397:15.856:3.506:8.144'
            'E3P.B99990001.pdb_424:8.558:1.315:6.627'
            'E3P.B99990001.pdb_774:14.204:-5.490:24.812'
            'E3P.B99990001.pdb_865:15.545:4.258:10.007'
            'E3P.B99990001.pdb_929:16.146:-6.081:24.770']#sublist1

            ['E3P.B99990002.pdb_138:4.499:4.286:8.260'
            'E3P.B99990002.pdb_397:14.897:3.238:9.338'
            'E3P.B99990002.pdb_424:5.649:5.914:8.639'
            'E3P.B99990002.pdb_774:12.114:-6.864:23.897'
            'E3P.B99990002.pdb_929:13.649:-6.894:22.589']#sublist2
                                                            ]'''
#then use thes sublists to make combinations
    for sublists in mainlist:
         Combinatedlist=map(dict,itertools.combinations(sublists.iteritems(), 2))
#since it is sublist there wont be any crossing between sublist1 and  sublist2 while doing combinations

#それでも適切な結果が得られなかった場合は、あなたの方法を提案してください

こんにちは皆さん、各ブログ間に特定のパターンを含め、同じパターンに基づいて吐き出してサブリストを作成し、それから組み合わせを作成することで、これに対する答えを得ました

My code:

import fileinput
import os
import re
import itertools
import math
import sys

pdbPathAndName = ['/Users/Mahesh/Documents/MAHESH_INTERNSHIP_2014/ENZOWP2/2WC5_090715_170128/E3P/E3P.B99990001.pdb','/Users/Mahesh/Documents/MAHESH_INTERNSHIP_2014/ENZOWP2/2WC5_090715_170128/E3P/E3P.B99990002.pdb']

ATOM_COORDINATE=[]
for path in pdbPathAndName:
    f = open(path, 'r').readlines()
    f = map(lambda x: x.strip(), f)
    for line in f:
        if "SG" in line and line.endswith("S"):
        ATOM_COORDINATE.append(path.split("/")[-1] + "_" + re.split('\s+', line)[1] + ":" + re.split('\s+', line)[5] + ":" +re.split('\s+', line)[6] + ":" + re.split('\s+', line)[7])
ATOM_COORDINATE.append("foo")

#Making Mainlist with sublists by splitting "foo" pattern
sub = []
for item in ATOM_COORDINATE:
    if item == 'foo':
         ATOM_COORDINATE.append(sub)
         sub = []
    else:
        sub.append(item)
 #Making combinations out of sublists
 COMBINATION=[]
 for sublists in sub:
     for L in range(2, len(sublists), 4):
        for subset in itertools.combinations(sublists, L):
            COMBINATION.append(subset)

OUTPUT:
MainlistWithSublists:
[['E3P.B99990001.pdb_138:6.923:0.241:6.116', 'E3P.B99990001.pdb_397:15.856:3.506:8.144', 'E3P.B99990001.pdb_424:8.558:1.315:6.627', 'E3P.B99990001.pdb_774:14.204:-5.490:24.812', 'E3P.B99990001.pdb_865:15.545:4.258:10.007', 'E3P.B99990001.pdb_929:16.146:-6.081:24.770'], ['E3P.B99990002.pdb_138:4.499:4.286:8.260', 'E3P.B99990002.pdb_397:14.897:3.238:9.338', 'E3P.B99990002.pdb_424:5.649:5.914:8.639', 'E3P.B99990002.pdb_774:12.114:-6.864:23.897', 'E3P.B99990002.pdb_865:15.200:3.910:11.227', 'E3P.B99990002.pdb_929:13.649:-6.894:22.589']]
Combination out of sublists:
[('E3P.B99990001.pdb_138:6.923:0.241:6.116', 'E3P.B99990001.pdb_397:15.856:3.506:8.144'), ('E3P.B99990001.pdb_138:6.923:0.241:6.116', 'E3P.B99990001.pdb_424:8.558:1.315:6.627'), ('E3P.B99990001.pdb_138:6.923:0.241:6.116', 'E3P.B99990001.pdb_774:14.204:-5.490:24.812'), ('E3P.B99990001.pdb_138:6.923:0.241:6.116', 'E3P.B99990001.pdb_865:15.545:4.258:10.007'), ('E3P.B99990001.pdb_138:6.923:0.241:6.116', 'E3P.B99990001.pdb_929:16.146:-6.081:24.770'), ('E3P.B99990001.pdb_397:15.856:3.506:8.144', 'E3P.B99990001.pdb_424:8.558:1.315:6.627'), ('E3P.B99990001.pdb_397:15.856:3.506:8.144', 'E3P.B99990001.pdb_774:14.204:-5.490:24.812'), ('E3P.B99990001.pdb_397:15.856:3.506:8.144', 'E3P.B99990001.pdb_865:15.545:4.258:10.007'), ('E3P.B99990001.pdb_397:15.856:3.506:8.144', 'E3P.B99990001.pdb_929:16.146:-6.081:24.770'), ('E3P.B99990001.pdb_424:8.558:1.315:6.627', 'E3P.B99990001.pdb_774:14.204:-5.490:24.812'), ('E3P.B99990001.pdb_424:8.558:1.315:6.627', 'E3P.B99990001.pdb_865:15.545:4.258:10.007'), ('E3P.B99990001.pdb_424:8.558:1.315:6.627', 'E3P.B99990001.pdb_929:16.146:-6.081:24.770'), ('E3P.B99990001.pdb_774:14.204:-5.490:24.812', 'E3P.B99990001.pdb_865:15.545:4.258:10.007'), ('E3P.B99990001.pdb_774:14.204:-5.490:24.812', 'E3P.B99990001.pdb_929:16.146:-6.081:24.770'), ('E3P.B99990001.pdb_865:15.545:4.258:10.007', 'E3P.B99990001.pdb_929:16.146:-6.081:24.770'), ('E3P.B99990002.pdb_138:4.499:4.286:8.260', 'E3P.B99990002.pdb_397:14.897:3.238:9.338'), ('E3P.B99990002.pdb_138:4.499:4.286:8.260', 'E3P.B99990002.pdb_424:5.649:5.914:8.639'), ('E3P.B99990002.pdb_138:4.499:4.286:8.260', 'E3P.B99990002.pdb_774:12.114:-6.864:23.897'), ('E3P.B99990002.pdb_138:4.499:4.286:8.260', 'E3P.B99990002.pdb_865:15.200:3.910:11.227'), ('E3P.B99990002.pdb_138:4.499:4.286:8.260', 'E3P.B99990002.pdb_929:13.649:-6.894:22.589'), ('E3P.B99990002.pdb_397:14.897:3.238:9.338', 'E3P.B99990002.pdb_424:5.649:5.914:8.639'), ('E3P.B99990002.pdb_397:14.897:3.238:9.338', 'E3P.B99990002.pdb_774:12.114:-6.864:23.897'), ('E3P.B99990002.pdb_397:14.897:3.238:9.338', 'E3P.B99990002.pdb_865:15.200:3.910:11.227'), ('E3P.B99990002.pdb_397:14.897:3.238:9.338', 'E3P.B99990002.pdb_929:13.649:-6.894:22.589'), ('E3P.B99990002.pdb_424:5.649:5.914:8.639', 'E3P.B99990002.pdb_774:12.114:-6.864:23.897'), ('E3P.B99990002.pdb_424:5.649:5.914:8.639', 'E3P.B99990002.pdb_865:15.200:3.910:11.227'), ('E3P.B99990002.pdb_424:5.649:5.914:8.639', 'E3P.B99990002.pdb_929:13.649:-6.894:22.589'), ('E3P.B99990002.pdb_774:12.114:-6.864:23.897', 'E3P.B99990002.pdb_865:15.200:3.910:11.227'), ('E3P.B99990002.pdb_774:12.114:-6.864:23.897', 'E3P.B99990002.pdb_929:13.649:-6.894:22.589'), ('E3P.B99990002.pdb_865:15.200:3.910:11.227', 'E3P.B99990002.pdb_929:13.649:-6.894:22.589')]

ありがとうございます

4

2 に答える 2

1

可能であれば、辞書を使用してください。

from collections import defaultdict

s = """B99990001 1 2 3 4
B99990001 1 3 3 4
B99990002 1 2 3 4
B99990002 1 3 3 4
B99990003 1 2 3 4
B99990003 1 3 3 4"""

d = defaultdict(list)
for line in s.split('\n'):
    index, values = line.split(maxsplit=1)
    d[index].append(values)

出力 (辞書d):

d = {
    'B99990003': ['1 2 3 4', '1 3 3 4'],
    'B99990001': ['1 2 3 4', '1 3 3 4'],
    'B99990002': ['1 2 3 4', '1 3 3 4'],
}

dict の代わりにリストのリストを本当に使用する必要がある場合は、これをリストに戻すことができます。

l = [['%s %s' % (index, value) for value in d[index]] for index in d]

ソートされたバージョンが必要な場合は、 を使用してソートできますsorted(l)

于 2015-07-12T23:20:35.193 に答える
1

まったく同じ出力が必要な場合:

from collections import OrderedDict

d = OrderedDict()
with open('file.txt') as f:
    for line in f:
        splitted = line.strip().split()
        key = splitted[0]
        if key not in d:
            d[key] = []
        d[key].append(' '.join( splitted[1:] ))

mainList = [ [key + ' ' + item for item in d[key] ] for key in d ]
print mainList

出力:

[['B99990001 1 2 3 4', 'B99990001 1 3 3 4'],
 ['B99990002 1 2 3 4', 'B99990002 1 3 3 4'],
 ['B99990003 1 2 3 4', 'B99990003 1 3 3 4']]
于 2015-07-12T23:24:50.897 に答える