【问题标题】:How to make list of lists based on particular character?如何根据特定字符制作列表列表?
【发布时间】:2015-10-01 03:10:49
【问题描述】:

我有一个文件有以下几行:

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)创建一个主列表,其中应包含三个子列表:

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')]

谢谢大家

【问题讨论】:

  • 为什么不使用字典,你需要列表吗?
  • 谢谢...因为下一步我想组合所有单个列表并从单个组合列表中读取所有偶数行。所以如果它是一个列表,那么这一步很容易做到。但如果您知道如何通过 dict 进行操作,您可以提出相同的建议。

标签: list python-2.7 sublist listproperty list-processing


【解决方案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']]

【讨论】:

    【解决方案2】:

    如果可以,请使用字典:

    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'],
    }
    

    如果您确实需要使用列表列表而不是字典,则可以将其转换回列表:

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

    如果您喜欢排序版本,可以使用 sorted(l) 对其进行排序。

    【讨论】:

    • 如果我不想在下一步中进行组合,这将很有用:我的下一个想法是获取每个子列表并从中进行组合。但在这种情况下,如果我进行组合,它将在相同的组合中使用这三个列 B99901,02,03
    • @user3805057 你打算做什么样的组合?您能否通过 pastebin.com 或其他方式提供更大的输入/输出示例?
    • 当然我明天会添加真实示例,感谢您的回复
    猜你喜欢
    • 1970-01-01
    • 2022-09-27
    • 1970-01-01
    • 2017-04-19
    • 2020-05-24
    • 2021-10-30
    • 2020-12-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多