Python多个变量有一个值

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 数据库中。另外 None 也不行。它跳过它。定义属性后,我只更改它交叉的值。一些主页没有相同的 Key/Values。所以在编辑之后,我需要把它变成一个节点对象,这样我就可以用一个关系来保存它:

# CREATE RELATIONSHIP NODE OBJECT
rel = relationNode.rel('Relationship', product, store, price, stock)
db.create(rel)

我不知道我是否会称其为更好,但这是您可以进行多重分配的一种方式...

AantalCores = CPUSSpecNumber = Snelheid = MaximaleTurboFrequentie = "NULL"
GeheugenSpecificatie = BusSnelheid = Procestechnologie = ThermalDesignPower = "NULL"

等等...

更新:

正在跟进以下评论...

一般来说,这样做不是一个好主意...但是如果您有 大量 需要一次分配的名称,您可以从它们开始在字典中,例如:

d = {'AantalCores': 'NULL', 'CPUSSSpecNumber': 'NULL'}

然后当你在字典中完成操作并需要将它们命名为值时,你可以将它们添加到你的局部变量中:

import sys
this_module = sys.modules[__name__]
for key, value in d.items():
    setattr(this_module, key, value)

但实际上,在普通情况下,这不是您管理姓名的方式,在实际可行的任何情况下,仅 dict 方法应该是您的首选方法。

使用字典可能比使用一堆单独的变量更好:

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,GeheugenSpecificatie,BusSnelheid,Procestechnologie,ThermalDesignPower,GeïntegreerdeGraphics,Gpu,NominaleSnelheidVideochip,MaximaleSnelheidVideochip,CPUCacheLevel1,CPUCacheLevel2,CPUCacheLevel3,Threads,Virtualisatie,VirtualisatieType,CPUMultiplier,CPUstepping,CPUInstructieset,TypeKoeling = ["NULL"]*22

print AantalCores
print CPUSSpecNumber
print Snelheid

输出

null
null
null

您可以像这样将所有这些值保存在一个数组中:

something = {
        "ID": 1280,
        "Another ID": "NULL",
    }

您可以通过一个循环或一个一个地更改所有值:

for val in something:
    something[val] = "null or your value"

something["ID"] = 900000

希望对你有所帮助,抱歉英语不好。