【问题标题】:splitting regex result in python3在python3中拆分正则表达式结果
【发布时间】:2014-04-22 14:32:10
【问题描述】:

朋友们,

感谢您在this 线程中的帮助,但由于我对python 的了解有限,我无法解决我的问题。所以,这是我意图的完整版本。

如果有人给我指路,我会很高兴。

输入文件

--------------------------------- potentials ----------------------------------
-------------------------------------------------------------------------------
 1.  Ni  type=1   np=1001 r1=1.0E-05  rnp=-1.35602175  pfile=Ni1.pot
 2.  Ni  type=2   np=1001 r1=1.0E-05  rnp=-1.35602175  pfile=Ni2.pot
 3.  Ni  type=3   np=1001 r1=1.0E-05  rnp=-1.35602175  pfile=Ni3.pot
 4.  Ni  type=4   np=1001 r1=1.0E-05  rnp=-1.35602175  pfile=Ni4.pot
 5.  Mn  type=5   np=1001 r1=1.0E-05  rnp=-1.68149622  pfile=Mn1.pot
 6.  Mn  type=6   np=1001 r1=1.0E-05  rnp=-1.68149622  pfile=Mn2.pot
 7.  Mn  type=7   np=1001 r1=1.0E-05  rnp=-1.68149622  pfile=Mn3.pot
 8.  Mn  type=8   np=1001 r1=1.0E-05  rnp=-1.68149622  pfile=Mn4.pot
 9.  Mn  type=9   np=1001 r1=1.0E-05  rnp=-1.68149622  pfile=Mn5.pot
 10. Mn  type=10  np=1001 r1=1.0E-05  rnp=-1.68149622  pfile=Mn6.pot
 11. Mn  type=11  np=1001 r1=1.0E-05  rnp=-1.68149622  pfile=Mn7.pot
 12. Mn  type=12  np=1001 r1=1.0E-05  rnp=-1.68149622  pfile=Mn8.pot
 13. Ge  type=13  np=1001 r1=1.0E-05  rnp=-1.35602175  pfile=Ge1.pot
 14. Si  type=14  np=1001 r1=1.0E-05  rnp=-1.35602175  pfile=Si1.pot
 15. Ge  type=15  np=1001 r1=1.0E-05  rnp=-1.35602175  pfile=Ge2.pot
 16. Si  type=16  np=1001 r1=1.0E-05  rnp=-1.35602175  pfile=Si2.pot
 17. Ge  type=17  np=1001 r1=1.0E-05  rnp=-1.35602175  pfile=Ge3.pot
 18. Si  type=18  np=1001 r1=1.0E-05  rnp=-1.35602175  pfile=Si3.pot
 19. Ge  type=19  np=1001 r1=1.0E-05  rnp=-1.35602175  pfile=Ge4.pot
 20. Si  type=20  np=1001 r1=1.0E-05  rnp=-1.35602175  pfile=Si4.pot
-------------------------------------------------------------------------------
------------------------------------- CPA -------------------------------------
-------------------------------------------------------------------------------
 1.  cpasite=5 nsubl=4 cpatypes=3,4,5,6
 2.  cpasite=6 nsubl=2 cpatypes=7,8
 3.  cpasite=7 nsubl=2 cpatypes=9,10
 4.  cpasite=8 nsubl=2 cpatypes=11,12
 5.  cpasite=9 nsubl=2 cpatypes=13,14
 6.  cpasite=12 nsubl=6 cpatypes=15,16,17,18,19,20

我已经使用了代码:

#!/usr/bin/python3

import re
f1=open("file.str","r")
pattern3=r'(\d+)\.\s*(.*)\s+ type=(\d+).* pfile=(.*)'
pattern4=r'(\d+)\. \s* cpasite=(.*)\s* nsubl=(.*)\s* cpatypes=(.*)'
count=[]; atype=[]; apots=[]; files=[]
xx=[];ckomp=[]; csubl=[]; sites=[];xx2=[]
slist=[]
for line in f1:
  match3=re.search(pattern3,line)
  match4=re.search(pattern4,line)
  if match3:
    count.append(int(match3.group(1)))
    atype.append((match3.group(2)))
    apots.append((match3.group(3)))
    files.append(match3.group(4))
  if match4:
    xx.append(match4.group(1))
    xx2.append(match4.group(2))
    ckomp.append(match4.group(3))
    sites.append(match4.group(4))

print(sites)
print(files)
print(count)

产生结果:

$ python tryeos.py 
['3,4,5,6', '7,8', '9,10', '11,12', '13,14', '15,16,17,18,19,20']
['Ni1.pot', 'Ni2.pot', 'Ni3.pot', 'Ni4.pot', 'Mn1.pot', 'Mn2.pot', 'Mn3.pot', 'Mn4.pot', 'Mn5.pot', 'Mn6.pot', 'Mn7.pot', 'Mn8.pot', 'Ge1.pot', 'Si1.pot', 'Ge2.pot', 'Si2.pot', 'Ge3.pot', 'Si3.pot', 'Ge4.pot', 'Si4.pot']
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]

这是正确的。 但我不确定如何将它们分组。

问题是,我有 20 个原子(count),sites 显示哪些原子在一起(例如 3、4、5 和 6 在一起,7 和 8 也在一起,还有 '15, 16、17、18、19、20')。 files 是原子的名称。

所以,预期的输出应该是:

#count 1-2 are not grouped in sites, so, they are alone
   group=1
   atom=Ni1.pot

   group=2
   atom=Ni2.pot

#count 3-6 are grouped together
   group=5
   atom=Ni3.pot, Ni4.pot, Mn1.pot, Mn2.pot

#count 7 &8 is grouped
   group=6
   atom=Mn3.pot, Mn4.pot

等等。

我能得到一些帮助吗?

NB group= 并不重要。这可以是任何整数。对于我的实践,我通常把它等于。

在 njzk2 的回答之后 我试图暗示这一点,如:

  for indices in sites:
    indices = map(int, indices.split(','))
    atoms = []
    for cnt in indices:
      i = count.index(cnt)
      atoms.append(files[i])
      del files[i]
      del count[i]
    print str(atoms)
    for f in files:
      print f 

只取第一组,报错:

$ python tryeos.py 
['Ni3.pot', 'Ni4.pot', 'Mn1.pot', 'Mn2.pot']
Ni1.pot
Ni2.pot
Mn3.pot
Mn4.pot
Mn5.pot
Mn6.pot
Mn7.pot
Mn8.pot
Ge1.pot
Si1.pot
Ge2.pot
Si2.pot
Ge3.pot
Si3.pot
Ge4.pot
Si4.pot
Traceback (most recent call last):
  File "tryeos.py", line 28, in <module>
    i = count.index(cnt)
ValueError: 3 is not in list

【问题讨论】:

  • 你能改写这句话吗:“问题是,我有 20 个原子(计数),站点显示哪些原子是在一起的。”
  • 因为,如果不使用它,我不会有大量的知识......我是一个新的初学者和学习......:)
  • 是什么让 3-6 第 5 组?
  • @jonrsharpe: cpasite=5 nsubl=4 cpatypes=3,4,5,6 其中 3,4,5,6 是要写入的文件的索引,与预期输出中一样。
  • 输出与输入不一致。输入中没有 cpatypes 19,20,21,22,23,24

标签: python regex


【解决方案1】:

您的结构可能不是针对这个问题最优化的,但您仍然可以通过这种方式格式化输出:

for indices in sites:
    indices = map(int, indices.split(','))
    atoms = []
    for cnt in indices:
        i = count.index(cnt)
        atoms.append(files[i])
        del files[i]
        del count[i]
    # A group of atoms
    print str(atoms)
for f in files:
    # A single file
    print f

【讨论】:

  • 有什么进一步的帮助吗?
  • 为我工作。我怀疑您的输入比您知道的要复杂一些,并且还有其他一些您尚未提及的限制。
  • 这里是完整的文件:paste.fedoraproject.org/96039/90807139 可能是我犯了一些错误?你能看看吗?我以python trial.py 的身份运行
  • 使用您提供的 3 个输入行(站点、文件、计数),我获得了预期值。
  • 试试paste.fedoraproject.org/96046/98191809(注意我的代码的缩进)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-06
相关资源
最近更新 更多