English 中文(简体)
在列表列表列表中的字符串中查找和分割字符
原标题:Finding and splitting at a character from a string inside a list of lists

我参加这个活动已有几天了,尝试了各种不同的方法,检查了至少50个不同的堆积流/平通图书馆/平通新闻组问题,但没有任何人提供了很大的帮助。 (尽管我并不感到惊讶,如果它在外面,我错过了它。 )

无论如何!

我有一份清单 里面有字符串 如下:

[[ CAA46951&Homeobox domain&192:248&F&#CDC1C5&NULL&PFAM&Y&433& ],
 [ CAA46951&Homeodomain-like&165:252&S&#CD5B45&NULL&SCOP&Y&433& ],
 [ 5330400P12&WD domain, G-beta repeat&131:168&F&#FF8C69&NULL&PFAM&Y&296& ],
 [ 5330400P12&WD domain, G-beta repeat&173:210&F&#FF8C69&NULL&PFAM&Y&296& ],
 [ 5330400P12&WD40-repeat&1:296&S&#00FF7F&NULL&SCOP&Y&296& ],
 [ AAH62206&Cell division protein&38:311&S&#00CED1&NULL&PFAM&Y&425& ],
 [ AAH62206&P-loop containing nucleoside triphosphate hydrolases&36:279&S&#00FFFF&NULL&SCOP&Y&425& ]]

我想将每个字符串分割成一个分隔字符串, 在一个列表中( 所以 [ [ a, b, c], [ a2, b2, c2 ] 排序列表, 而不是 [ [ a& b& c], [ a2& b2& c2] ]

我尝试了从循环、循环函数到循环函数的统计等一切方法,但是我无法让它发挥作用。我知道这是一个非常无意义的问题,但请帮助。

(值得注意的是,清单作为txt.文件通过,并转换成字符串清单。

CAA46951&Homeobox domain&192:248&F&#CDC1C5&NULL&PFAM&Y&433& CAA46951&Homeodomain-like&165:252&S&#CD5B45&NULL&SCOP&Y&433&)

最佳回答
LofL=[[ CAA46951&Homeobox domain&192:248&F&#CDC1C5&NULL&PFAM&Y&433& ], 
      [ CAA46951&Homeodomain-like&165:252&S&#CD5B45&NULL&SCOP&Y&433& ], 
      [ 5330400P12&WD domain, G-beta repeat&131:168&F&#FF8C69&NULL&PFAM&Y&296& ], 
      [ 5330400P12&WD domain, G-beta repeat&173:210&F&#FF8C69&NULL&PFAM&Y&296& ], 
      [ 5330400P12&WD40-repeat&1:296&S&#00FF7F&NULL&SCOP&Y&296& ], 
      [ AAH62206&Cell division protein&38:311&S&#00CED1&NULL&PFAM&Y&425& ], 
      [ AAH62206&P-loop containing nucleoside triphosphate hydrolases&36:279&S&#00FFFF&NULL&SCOP&Y&425& ]]

newL=[]      
for L in LofL:
    newSubL=[]
    for e in L:
        for s in e.split( & ):
            if s:
                newSubL.append(s)
    newL.append(newSubL)

产出:

[[ CAA46951 ,  Homeobox domain ,  192:248 ,  F ,  #CDC1C5 ,  NULL ,  PFAM ,  Y ,  433 ], [ CAA46951 ,  Homeodomain-like ,  165:252 ,  S ,  #CD5B45 ,  NULL ,  SCOP ,  Y ,  433 ], [ 5330400P12 ,  WD domain, G-beta repeat ,  131:168 ,  F ,  #FF8C69 ,  NULL ,  PFAM ,  Y ,  296 ], [ 5330400P12 ,  WD domain, G-beta repeat ,  173:210 ,  F ,  #FF8C69 ,  NULL ,  PFAM ,  Y ,  296 ], [ 5330400P12 ,  WD40-repeat ,  1:296 ,  S ,  #00FF7F ,  NULL ,  SCOP ,  Y ,  296 ], [ AAH62206 ,  Cell division protein ,  38:311 ,  S ,  #00CED1 ,  NULL ,  PFAM ,  Y ,  425 ], [ AAH62206 ,  P-loop containing nucleoside triphosphate hydrolases ,  36:279 ,  S ,  #00FFFF ,  NULL ,  SCOP ,  Y ,  425 ]]

如果您想进一步减少,您可以这样做:

newL=[filter(len, e.split( & )) for l in LofL for e in l] 
问题回答
>>> oldList = [[ a&b&c ], [ d&e&f ]]
>>> newList = [item[0].split( & ) for item in oldList]
>>> newList
[[ a ,  b ,  c ], [ d ,  e ,  f ]]




相关问题
Simple JAVA: Password Verifier problem

I have a simple problem that says: A password for xyz corporation is supposed to be 6 characters long and made up of a combination of letters and digits. Write a program fragment to read in a string ...

Case insensitive comparison of strings in shell script

The == operator is used to compare two strings in shell script. However, I want to compare two strings ignoring case, how can it be done? Is there any standard command for this?

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

String initialization with pair of iterators

I m trying to initialize string with iterators and something like this works: ifstream fin("tmp.txt"); istream_iterator<char> in_i(fin), eos; //here eos is 1 over the end string s(in_i, ...

break a string in parts

I have a string "pc1|pc2|pc3|" I want to get each word on different line like: pc1 pc2 pc3 I need to do this in C#... any suggestions??

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签