python 对象获取完整列表
python object taking full list
我正在尝试编写我的第一个面向对象程序。
我想出的代码是:
class Lattice:
'Commomn base for all sublattice'
latc = 0
def __init__(self, conc, name, pot):
self.atcon = atcon
self.name =name
self.pot=[]
Lattice.latc += 1
atcon=[]
with open(inpf, "r") as f:
for line in f:
match = re.search(reall, line)
if match:
if (match.group(1).strip() == "atcon"):
atcon.append(match.group(2).split())
print("CON =>"+str(atcon))
print("CON[0] =>"+str(atcon[0]))
lat0=Lattice(atcon[0],pot[0],name[0])
print("lat0 =>"+str(lat0.atcon))
我原以为 lat0.atcon
会是 atcon[0]
但是最后 3 条打印语句的结果是:
CON =>[['0.5d0', '0.50d0'], ['0.98d0', '0.02d0'], ['0.98d0', '0.02d0'], ['0.98d0', '0.02d0']]
CON[0] =>['0.5d0', '0.50d0']
lat0 =>[['0.5d0', '0.50d0'], ['0.98d0', '0.02d0'], ['0.98d0', '0.02d0'], ['0.98d0', '0.02d0']]
我不明白为什么。我是一个绝对的初学者,没有接受过正式的 python 培训(使用 net 和 SO 学习),所以请耐心等待。
更新:
在接受回复后,这是我目前正在使用的代码。问题是,我正在阅读 list
中的所有内容,然后将这些列表插入到 lat0
即
#if match found
atcon.append(match.group(2).split())
# after getting all match, in global scope
lat0=Lattice(atcon[0],name[0],pot[0])
所以,我想我要么在浪费 list
,要么在浪费对象 lat0
。找到匹配项后我是否可以直接填充 lat0
?
例如
#if match found for <name>
lat0=Lattice(name)
mini.py:
#!/usr/bin/env python3
import sys
import re
class Lattice:
'Commomn base for all sublattice'
latc = 0
def __init__(self, conc, names, pots):
self.atcon = conc
self.name =names
self.pot=pots
Lattice.latc += 1
reall='(.*)\s*=\s*(.*)'
inpf = sys.argv[1]
print(inpf)
with open(inpf, "r") as f:
pot=[]
name=[]
atcon=[]
for line in f:
match = re.search(reall, line)
if match:
if (match.group(1).strip() == "atcon"):
atcon.append(match.group(2).split())
if (match.group(1).strip() == "pot"):
pot.append(match.group(2).split())
if (match.group(1).strip() == "name"):
name.append(match.group(2).split())
lat0=Lattice(atcon[0],name[0],pot[0])
print("POT =>"+str(pot))
print("NAME =>"+str(name))
print("CON =>"+str(atcon))
print("CON[0] =>"+str(atcon[0]))
print("lat0 =>"+str(lat0.pot))
典型输入
pot=Rh1.pot Rh2.pot Fe1a.pot Fe2a.pot
name=Rh-up Fe-up
atcon=0.5d0 0.50d0
pot=Rh3.pot Rh4.pot Fe3a.pot Fe4a.pot
name=Rh-up Fe-up
atcon=0.98d0 0.02d0
我敢打赌您是在 IDLE 中编写或测试此 class 的。在这一点上,我确定它真的很混乱,但错误非常简单。当您实例化 class 时,通常建议使用您发送给 __init__
的值而不是引用其他值。
class Lattice:
'Commomn base for all sublattice'
latc = 0
def __init__(self, conc, name, pot):
self.atcon = conc
self.name =name
self.pot=pot
Lattice.latc += 1
发生的事情是 atcon
、pon
和 name
是在全局范围内定义的,您在下面的示例中引用了它们:
atcon=[1, 2, 3, 4, 5, 6]
pot = [7,8,9]
name = ["foo", "bar"]
class globs:
def __init__(self):
self.atcon = atcon
self.pot = pot
self.name = name
给出了以下输出:
>>> g = globs()
>>> g.atcon
[1, 2, 3, 4, 5, 6]
>>> g.pot
[7, 8, 9]
>>> g.name
['foo', 'bar']
编辑 对原始问题的扩展编辑的回答。
我想我明白了。有两件事仍然让我感到困惑:
- 如果我按照代码进行操作,您似乎只想将文件中的第一个命中作为 lat0。但是你没有解释你是只想要文件中的第一个命中,还是所有命中的对象列表。
- 你做了一个拆分,但根据你的样本输入,仍然会 return 一个列表,即
["Rh1.pot", "Rh2.pot", "Fe1a.pot", "Fe2a.pot"]
,我可能是冒昧的,但我在后面添加了一个 [0]
拆分以仅检索第一次命中。如果我错过了重点,请将其删除。
这是在找到第一个命中后将停止循环的代码。我将 atcon
、pot
和 name
声明为列表,因为 .split()
将 return 一个列表,但我不附加结果以免浪费内存。我还 return Lattice
反对退出函数并避免浪费时间解析其余行。
此外,最后的 if atcon and pot and name
是为了避免 returning,以防有一段匹配但不包含所有重要信息的文本。在 python 中 if
的空列表将是 False
。您可以保留其余代码(打印语句除外)。
inpf = sys.argv[1]
print(inpf)
def parse(inpf):
atcon, pot, name = [], [], []
reall='(.*)\s*=\s*(.*)'
with open(inpf, "r") as f:
for line in f:
print(line)
if match:
if (match.group(1).strip() == "atcon"):
atcon = match.group(2).split()[0]
if (match.group(1).strip() == "pot"):
pot = match.group(2).split()[0]
if (match.group(1).strip() == "name"):
name = match.group(2).split()[0]
if atcon and pot and name:
return Lattice(atcon, name, pot)
lat0 = parse("test.txt")
print("lat0 =>"+str(lat0.pot)+" "+str(lat0.name)+" "+str(lat0.atcon))
测试于
atcon=0.5d0 0.50d0
atcon=0.5d0 0.50d0
atcon=0.5d0 0.50d0
pot=Rh1.pot Rh2.pot Fe1a.pot Fe2a.pot
name=Rh-up Fe-up
atcon=0.5d0 0.50d0
pot=Rh3.pot Rh4.pot Fe3a.pot Fe4a.pot
name=Rh-up Fe-up
atcon=0.98d0 0.02d0
输出:
lat0 =>Rh1.pot Rh-up 0.5d0
我正在尝试编写我的第一个面向对象程序。 我想出的代码是:
class Lattice:
'Commomn base for all sublattice'
latc = 0
def __init__(self, conc, name, pot):
self.atcon = atcon
self.name =name
self.pot=[]
Lattice.latc += 1
atcon=[]
with open(inpf, "r") as f:
for line in f:
match = re.search(reall, line)
if match:
if (match.group(1).strip() == "atcon"):
atcon.append(match.group(2).split())
print("CON =>"+str(atcon))
print("CON[0] =>"+str(atcon[0]))
lat0=Lattice(atcon[0],pot[0],name[0])
print("lat0 =>"+str(lat0.atcon))
我原以为 lat0.atcon
会是 atcon[0]
但是最后 3 条打印语句的结果是:
CON =>[['0.5d0', '0.50d0'], ['0.98d0', '0.02d0'], ['0.98d0', '0.02d0'], ['0.98d0', '0.02d0']]
CON[0] =>['0.5d0', '0.50d0']
lat0 =>[['0.5d0', '0.50d0'], ['0.98d0', '0.02d0'], ['0.98d0', '0.02d0'], ['0.98d0', '0.02d0']]
我不明白为什么。我是一个绝对的初学者,没有接受过正式的 python 培训(使用 net 和 SO 学习),所以请耐心等待。
更新:
在接受回复后,这是我目前正在使用的代码。问题是,我正在阅读 list
中的所有内容,然后将这些列表插入到 lat0
即
#if match found
atcon.append(match.group(2).split())
# after getting all match, in global scope
lat0=Lattice(atcon[0],name[0],pot[0])
所以,我想我要么在浪费 list
,要么在浪费对象 lat0
。找到匹配项后我是否可以直接填充 lat0
?
例如
#if match found for <name>
lat0=Lattice(name)
mini.py:
#!/usr/bin/env python3
import sys
import re
class Lattice:
'Commomn base for all sublattice'
latc = 0
def __init__(self, conc, names, pots):
self.atcon = conc
self.name =names
self.pot=pots
Lattice.latc += 1
reall='(.*)\s*=\s*(.*)'
inpf = sys.argv[1]
print(inpf)
with open(inpf, "r") as f:
pot=[]
name=[]
atcon=[]
for line in f:
match = re.search(reall, line)
if match:
if (match.group(1).strip() == "atcon"):
atcon.append(match.group(2).split())
if (match.group(1).strip() == "pot"):
pot.append(match.group(2).split())
if (match.group(1).strip() == "name"):
name.append(match.group(2).split())
lat0=Lattice(atcon[0],name[0],pot[0])
print("POT =>"+str(pot))
print("NAME =>"+str(name))
print("CON =>"+str(atcon))
print("CON[0] =>"+str(atcon[0]))
print("lat0 =>"+str(lat0.pot))
典型输入
pot=Rh1.pot Rh2.pot Fe1a.pot Fe2a.pot
name=Rh-up Fe-up
atcon=0.5d0 0.50d0
pot=Rh3.pot Rh4.pot Fe3a.pot Fe4a.pot
name=Rh-up Fe-up
atcon=0.98d0 0.02d0
我敢打赌您是在 IDLE 中编写或测试此 class 的。在这一点上,我确定它真的很混乱,但错误非常简单。当您实例化 class 时,通常建议使用您发送给 __init__
的值而不是引用其他值。
class Lattice:
'Commomn base for all sublattice'
latc = 0
def __init__(self, conc, name, pot):
self.atcon = conc
self.name =name
self.pot=pot
Lattice.latc += 1
发生的事情是 atcon
、pon
和 name
是在全局范围内定义的,您在下面的示例中引用了它们:
atcon=[1, 2, 3, 4, 5, 6]
pot = [7,8,9]
name = ["foo", "bar"]
class globs:
def __init__(self):
self.atcon = atcon
self.pot = pot
self.name = name
给出了以下输出:
>>> g = globs()
>>> g.atcon
[1, 2, 3, 4, 5, 6]
>>> g.pot
[7, 8, 9]
>>> g.name
['foo', 'bar']
编辑 对原始问题的扩展编辑的回答。
我想我明白了。有两件事仍然让我感到困惑:
- 如果我按照代码进行操作,您似乎只想将文件中的第一个命中作为 lat0。但是你没有解释你是只想要文件中的第一个命中,还是所有命中的对象列表。
- 你做了一个拆分,但根据你的样本输入,仍然会 return 一个列表,即
["Rh1.pot", "Rh2.pot", "Fe1a.pot", "Fe2a.pot"]
,我可能是冒昧的,但我在后面添加了一个[0]
拆分以仅检索第一次命中。如果我错过了重点,请将其删除。
这是在找到第一个命中后将停止循环的代码。我将 atcon
、pot
和 name
声明为列表,因为 .split()
将 return 一个列表,但我不附加结果以免浪费内存。我还 return Lattice
反对退出函数并避免浪费时间解析其余行。
此外,最后的 if atcon and pot and name
是为了避免 returning,以防有一段匹配但不包含所有重要信息的文本。在 python 中 if
的空列表将是 False
。您可以保留其余代码(打印语句除外)。
inpf = sys.argv[1]
print(inpf)
def parse(inpf):
atcon, pot, name = [], [], []
reall='(.*)\s*=\s*(.*)'
with open(inpf, "r") as f:
for line in f:
print(line)
if match:
if (match.group(1).strip() == "atcon"):
atcon = match.group(2).split()[0]
if (match.group(1).strip() == "pot"):
pot = match.group(2).split()[0]
if (match.group(1).strip() == "name"):
name = match.group(2).split()[0]
if atcon and pot and name:
return Lattice(atcon, name, pot)
lat0 = parse("test.txt")
print("lat0 =>"+str(lat0.pot)+" "+str(lat0.name)+" "+str(lat0.atcon))
测试于
atcon=0.5d0 0.50d0
atcon=0.5d0 0.50d0
atcon=0.5d0 0.50d0
pot=Rh1.pot Rh2.pot Fe1a.pot Fe2a.pot
name=Rh-up Fe-up
atcon=0.5d0 0.50d0
pot=Rh3.pot Rh4.pot Fe3a.pot Fe4a.pot
name=Rh-up Fe-up
atcon=0.98d0 0.02d0
输出:
lat0 =>Rh1.pot Rh-up 0.5d0