[英]Python Multiple variables has one Value
我怎样才能使这段代码漂亮?:
AantalCores = "NULL"
CPUSSpecNumber = "NULL"
Snelheid = "NULL"
MaximaleTurboFrequentie = "NULL"
GeheugenSpecificatie = "NULL"
BusSnelheid = "NULL"
Procestechnologie = "NULL"
ThermalDesignPower = "NULL"
GeïntegreerdeGraphics = "NULL"
Gpu = "NULL"
NominaleSnelheidVideochip = "NULL"
MaximaleSnelheidVideochip = "NULL"
CPUCacheLevel1 = "NULL"
CPUCacheLevel2 = "NULL"
CPUCacheLevel3 = "NULL"
Threads = "NULL"
Virtualisatie = "NULL"
VirtualisatieType = "NULL"
CPUMultiplier = "NULL"
CPUstepping = "NULL"
CPUInstructieset = "NULL"
TypeKoeling = "NULL"
改变了整个事情。 我已经看过你们的答案了,谢谢! 我快完成了,只需要适当地进行迭代即可。 你们能告诉我一种更好的工作方式吗?
if componentTitle == 'Processoren':
properties = {'AantalCores': 'NULL', 'CPUSSpecNumber': 'NULL', 'Snelheid': 'NULL', 'MaximaleTurboFrequentie': 'NULL', 'GeheugenSpecificatie': 'NULL', 'BusSnelheid': 'NULL', 'Procestechnologie': 'NULL', 'ThermalDesignPower': 'NULL', 'GeïntegreerdeGraphics': 'NULL', 'Gpu': 'NULL', 'NominaleSnelheidVideochip': 'NULL', 'MaximaleSnelheidVideochip': 'NULL', 'CPUCacheLevel1': 'NULL', 'CPUCacheLevel2': 'NULL', 'CPUCacheLevel3': 'NULL', 'Threads': 'NULL', 'Virtualisatie': 'NULL', 'VirtualisatieType': 'NULL', 'CPUMultiplier': 'NULL', 'CPUstepping': 'NULL', 'CPUInstructieset': 'NULL', 'TypeKoeling': 'NULL'}
if spec.get_text(strip=True) == 'Processorkernen': properties['AantalCores'] = value.text.strip()
elif spec.get_text(strip=True) == 'Kloksnelheid': properties['Snelheid'] = value.text.strip()
elif spec.get_text(strip=True) == 'Threads': properties['Threads'] = value.text.strip()
# I NEED TO ITERATE AL THE KEYS/VALUES INTO THIS NODE (product)
for key in properties:
product = Node("Component", 'CPU', key=properties[key])
所以我必须先声明值。 否则,这些值不会保存到Neo4J数据库中。 同样,没有没有没有。 它跳过它。 定义属性后,我仅更改其交叉的值。 某些页面没有相同的键/值。 因此,在编辑之后,我需要将其设置为节点对象,以便可以与关系进行保存:
# CREATE RELATIONSHIP NODE OBJECT
rel = relationNode.rel('Relationship', product, store, price, stock)
db.create(rel)
使用字典可能比一堆单独的变量更好:
FIELDS = {k: "NULL" for k in (
"AantalCores",
"CPUSSpecNumber",
"Snelheid",
# ... etc.
)}
然后,您可以使用FIELDS["AantalCores"]
来访问它们,或者如果您稍后需要在代码中处理它们,则可以使用以下代码:
for name, value in FIELDS.items():
# do something with name and value
我不知道我是否可以称其为更好 ,但这是您可以进行多重分配的一种方法...
AantalCores = CPUSSpecNumber = Snelheid = MaximaleTurboFrequentie = "NULL"
GeheugenSpecificatie = BusSnelheid = Procestechnologie = ThermalDesignPower = "NULL"
等等...
更新:
跟进下面的评论...
通常,这样做不是一个好主意...但是,如果您有大量名称需要一次分配所有名称,则可以在字典中以它们开头,例如:
d = {'AantalCores': 'NULL', 'CPUSSSpecNumber': 'NULL'}
然后,当您完成了在dict中对其进行操作并需要将其命名为值时,可以将其添加到您的本地变量中,从而:
import sys
this_module = sys.modules[__name__]
for key, value in d.items():
setattr(this_module, key, value)
但是,确实,在通常情况下,这不是您应使用的名称管理方式,而dict-only方法应是您在任何实际可行的情况下的首选方法。
您可以将所有这些值保存在单个数组中,如下所示:
something = {
"ID": 1280,
"Another ID": "NULL",
}
您可以通过一个循环或一个接一个地更改所有值:
for val in something:
something[val] = "null or your value"
something["ID"] = 900000
我希望这对您有帮助,对不起,英语不好。
您尝试过的东西
AantalCores,CPUSSpecNumber,Snelheid,MaxureTurboFrequentie,GeheugenSpecificatie,BusSnelheid,Procestechnologie,ThermalDesignPower,GeïntegreerdeGraphics,Gpu,NominaleSnelheidVideochip,MaximaleSnelheidVideochip,CPU * Level,CPUType,CPUType,CPUCacheLevel,CacheInCache,Cache [Level], 22
print AantalCores
print CPUSSpecNumber
print Snelheid
输出
null
null
null
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.