替换python中的列表

时间:2014-09-22 14:17:37

标签: python list

请考虑这段代码:

print(ofiles)
Atoms=[[] for x in range(len(ofiles))]
files=list(ofiles)
for nums in ofiles:
    i = ofiles.index(nums)
    Atoms[i].append(ofiles[i])
    for indices in sites:
        indices = map(int, indices.split(','))
        atoms = []
        concs = []
        moms = []
        names = []
        for cnt in indices:
            j = ocount.index(cnt)
            atoms.append(files[j])
            names.append(os.path.splitext(files[j])[0])
        if j==i:
            Atoms[i].append(atoms)
print(Atoms)

我正在尝试替换位置[i]的字符串。和追加一样,我得到了:

ofiles=['Ni1.pot', 'Ni2.pot', 'Ni3.pot', 'Ni4.pot', 'Mn1.pot', 'Mn2.pot', 'Mn3.pot', 'Mn4.pot']
Atoms(from the print statement at the end)=[['Ni1.pot'], ['Ni2.pot'], ['Ni3.pot'], ['Ni4.pot'], ['Mn1.pot'], ['Mn2.pot'], ['Mn3.pot'], ['Mn4.pot', ['Mn1.pot', 'Mn2.pot', 'Mn3.pot', 'Mn4.pot']]]

我正在努力实现:

Atoms:[['Ni1.pot'], ['Ni2.pot'], ['Ni3.pot'], ['Ni4.pot'],  ['Mn1.pot', 'Mn2.pot', 'Mn3.pot', 'Mn4.pot']]

PS 我试图删除该元素,然后将其插入,如:

   if j==i:
        Atoms[i].remove(atoms)
        Atoms[i].insert(i, atoms)

以错误结束:

   Atoms[i].remove(atoms)
ValueError: list.remove(x): x not in list

我是python的新手。所以,在结束之前,我真的很感激回答/评论。

sites=['5,6,7,8']

是将哪些ofiles写在一起的索引。 ofiles是我的意见。它正在填充Atoms。使用i循环取值5,这将满足i==j,我试图将原子列表5,6,7,8放在该站点中,如我预期的输出所示

完成代码和输入

代码:

#!/usr/bin/python3
import re
import os.path
# import shutil
# import sys

f1 = open('fileini', 'r')
pattern3 = r'(\d+)\.\s*(.*)\s+ type=(\d+).* pfile=(.*)'
pattern4 = r'(\d+)\. \s* cpasite=(.*)\s* nsubl=(.*)\s* cpatypes=(.*)'

ocount = []
atype = []
apots = []
ofiles = []
xx = []
ckomp = []
sites = []
xx2 = []

for line in f1:
    match3 = re.search(pattern3, line)
    match4 = re.search(pattern4, line)
    if match3:
        ocount.append(int(match3.group(1)))
        atype.append((match3.group(2)))
        apots.append((match3.group(3)))
        ofiles.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))
f1.close()
print(sites)
print(ofiles)
Atoms=[[] for x in range(len(ofiles))]
files=list(ofiles)
for nums in ofiles:
    i = ofiles.index(nums)
    Atoms[i].append(ofiles[i])
    for indices in sites:
        indices = map(int, indices.split(','))
        atoms = []
        concs = []
        moms = []
        names = []
        for cnt in indices:
            j = ocount.index(cnt)
            atoms.append(files[j])
            names.append(os.path.splitext(files[j])[0])
        if j==i:
            Atoms[i].append(atoms)
print(Atoms)

输入fileini:

 1.  Ni  type=1   np=1001 r1=1.0E-05  rnp=-2.01299308  pfile=Ni1.pot
 2.  Ni  type=2   np=1001 r1=1.0E-05  rnp=-2.01299308  pfile=Ni2.pot
 3.  Ni  type=3   np=1001 r1=1.0E-05  rnp=-2.01299308  pfile=Ni3.pot
 4.  Ni  type=4   np=1001 r1=1.0E-05  rnp=-2.01299308  pfile=Ni4.pot
 5.  Mn  type=5   np=1001 r1=1.0E-05  rnp=-2.41572103  pfile=Mn1.pot
 6.  Mn  type=6   np=1001 r1=1.0E-05  rnp=-2.41572103  pfile=Mn2.pot
 7.  Mn  type=7   np=1001 r1=1.0E-05  rnp=-2.41572103  pfile=Mn3.pot
 8.  Mn  type=8   np=1001 r1=1.0E-05  rnp=-2.41572103  pfile=Mn4.pot
 9.  Mn  type=9   np=1001 r1=1.0E-05  rnp=-2.41572103  pfile=Mn5.pot
 10. Mn  type=10  np=1001 r1=1.0E-05  rnp=-2.41572103  pfile=Mn6.pot
 11. Mn  type=11  np=1001 r1=1.0E-05  rnp=-2.41572103  pfile=Mn7.pot
 12. Mn  type=12  np=1001 r1=1.0E-05  rnp=-2.41572103  pfile=Mn8.pot
 13. Ge  type=13  np=1001 r1=1.0E-05  rnp=-2.01299308  pfile=Ge1.pot
 14. Ge  type=14  np=1001 r1=1.0E-05  rnp=-2.01299308  pfile=Ge2.pot
 15. Ge  type=15  np=1001 r1=1.0E-05  rnp=-2.01299308  pfile=Ge3.pot
 16. Ge  type=16  np=1001 r1=1.0E-05  rnp=-2.01299308  pfile=Ge4.pot
-------------------------------------------------------------------------------
------------------------------------- CPA -------------------------------------
-------------------------------------------------------------------------------
 1.  cpasite=5 nsubl=4 cpatypes=5,6,7,8
-------------------------------------------------------------------------------

Atoms的实际输出是:

[['Ni1.pot'], ['Ni2.pot'], ['Ni3.pot'], ['Ni4.pot'], ['Mn1.pot'], ['Mn2.pot'], ['Mn3.pot'], ['Mn4.pot', ['Mn1.pot', 'Mn2.pot', 'Mn3.pot', 'Mn4.pot']], ['Mn5.pot'], ['Mn6.pot'], ['Mn7.pot'], ['Mn8.pot'], ['Ge1.pot'], ['Ge2.pot'], ['Ge3.pot'], ['Ge4.pot']]

虽然我期待它:

[['Ni1.pot'], ['Ni2.pot'], ['Ni3.pot'], ['Ni4.pot'], [['Mn1.pot', 'Mn2.pot', 'Mn3.pot', 'Mn4.pot']],  ['Ge1.pot'], ['Ge2.pot'], ['Ge3.pot'], ['Ge4.pot']]

即。站点5,6,7,8中的那些将用Mn1.pot

替换[['Mn1.pot', 'Mn2.pot', 'Mn3.pot', 'Mn4.pot']]

1 个答案:

答案 0 :(得分:1)

ofiles=['Ni1.pot', 'Ni2.pot', 'Ni3.pot', 'Ni4.pot', 'Mn1.pot', 'Mn2.pot', 'Mn3.pot', 'Mn4.pot']
sites = [4,5,6,7]
atom = []


for i in range(len(ofiles)):
     if i not in site:
         atom.append([ofiles[i]])
     else:
        site.sort() 
        if i == site[0]:
            atom.append([[ofiles[ii] for ii in site]])
print(atom)

这应该有效。试试吧!! 干杯!!