2014-04-22 2 views
-1

친구, this 스레드에 도움을분할 정규식 결과

감사하지만 때문에 파이썬 내 제한된 지식, 나는 내 문제를 해결할 수없는입니다. 그래서, 여기 내 의도의 전체 버전입니다.

누군가가 나를 보여 주면 매우 기쁠 것입니다.

입력 파일 내가 코드를 사용한

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

결과 yeilds : 올바른

$ 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] 

. 하지만 그룹화 방법을 잘 모르겠습니다.

문제는 12 개의 원자 (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 그룹 = 중요하지 않습니다. 이것은 임의의 정수일 수 있습니다. 내 연습을 위해, 나는 보통 그것을 평등하게 두었다. 그것의 오류로, 첫 번째 그룹을 복용

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 
+0

이 문장을 다시 써 주실 수 있겠습니까? "문제는 20 개의 원자 (개수)가 있는데, 어떤 원자가 함께 있는지 보여줍니다." – ooga

+0

왜냐하면, 나는 그것을 사용하지 않고도 거대한 지식을 가질 수 없기 때문에 .... 나는 새로운 시작이고 학습이다. :) – BaRud

+0

3-6 그룹 5는 무엇이됩니까? – jonrsharpe

답변

1

귀하의 구조가 아마도 가장하지

는 njzk2의 대답 후 나는 같은 것을 impliment 시도 이 문제에 대해 최적화되었지만 다음과 같이 출력 형식을 지정할 수 있습니다.

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 
+0

더 도움이 필요하십니까? – BaRud

+0

이 저에게 효과적입니다. 귀하의 의견은 귀하가 알기 쉽도록 조금 더 복잡하다고 생각하며 귀하가 아직 언급하지 않은 다른 제약 사항이 있습니다. – njzk2

+0

전체 파일은 다음과 같습니다. http://paste.fedoraproject.org/96039/90807139/ 일부 오류가 발생 했습니까? 친절하게 봐 줄래? 그리고 내가 입력 한 3 개의 입력 줄 (사이트, 파일, 개수)이있는'python trial.py' – BaRud