Python字典迭代正确的方法

时间:2015-01-12 23:26:25

标签: python dictionary neo4j iterator

我需要先通过迭代产品页来保存节点。我需要存储的值并将其放入Neo4J图形数据库的节点中。这是一个示例节点:

我需要将其保存为:

n = Node('Component', 'CPU',
         AantalCores=AantalCores,
         CPUSSpecNumber=CPUSSpecNumber,
         Snelheid=Snelheid,
         MaximaleTurboFrequentie=MaximaleTurboFrequentie,
         GeheugenSpecificatie=GeheugenSpecificatie,
         BusSnelheid=BusSnelheid,
         Procestechnologie=Procestechnologie,
         ThermalDesignPower=ThermalDesignPower,
         GeïntegreerdeGraphics=GeïntegreerdeGraphics,
         Gpu=Gpu,
         NominaleSnelheidVideochip=NominaleSnelheidVideochip,
         MaximaleSnelheidVideochip=MaximaleSnelheidVideochip,
         CPUCacheLevel1=CPUCacheLevel1,
         CPUCacheLevel2=CPUCacheLevel2,
         CPUCacheLevel3=CPUCacheLevel3,
         Threads=Threads,
         Virtualisatie=Virtualisatie,
         VirtualisatieType=VirtualisatieType,
         CPUMultiplier=CPUMultiplier,
         CPUstepping=CPUstepping,
         CPUInstructieset=CPUInstructieset,
         TypeKoeling=TypeKoeling)

我有这个代码:

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

我需要通过属性的键/值进行迭代并在此处保存:

for key in range(0, len(properties)):
    product = Node("Component", 'CPU', key=properties[key])

所以它应该是这样的:

product = Node("Component", 'CPU',
               AantalCores='Quad-Core',
               CPUSSpecNumber= 'NULL',
               Snelheid= '3400 MHz',
               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': '8',
               'Virtualisatie': 'NULL',
               'VirtualisatieType': 'NULL',
               'CPUMultiplier': 'NULL',
               CPUstepping= 'NULL',
               CPUInstructieset= 'NULL',
               TypeKoeling= 'NULL'

2 个答案:

答案 0 :(得分:1)

properties dict - 与任何dict一样 - 具有固定顺序。如果您不关心订单,那么:

product = Node('Component', 'CPU', **properties)

将按指定的方式工作。如果您关心订单,则需要指定以某种方式 - 例如列表,而不是字典!

答案 1 :(得分:0)

我明白了。有一个OBJECT

class productNode():

def nodeCPU(self, AantalCores, CPUSSpecNumber, Snelheid, MaximaleTurboFrequentie, GeheugenSpecificatie, BusSnelheid, Procestechnologie, ThermalDesignPower, GeïntegreerdeGraphics, Gpu, NominaleSnelheidVideochip, MaximaleSnelheidVideochip, CPUCacheLevel1, CPUCacheLevel2, CPUCacheLevel3, Threads, Virtualisatie, VirtualisatieType, CPUMultiplier, CPUstepping, CPUInstructieset, TypeKoeling):
    n = Node('Component', 'CPU', AantalCores=AantalCores, CPUSSpecNumber=CPUSSpecNumber, Snelheid=Snelheid, MaximaleTurboFrequentie=MaximaleTurboFrequentie, GeheugenSpecificatie=GeheugenSpecificatie, BusSnelheid=BusSnelheid, Procestechnologie=Procestechnologie, ThermalDesignPower=ThermalDesignPower, GeïntegreerdeGraphics=GeïntegreerdeGraphics, Gpu=Gpu, NominaleSnelheidVideochip=NominaleSnelheidVideochip, MaximaleSnelheidVideochip=MaximaleSnelheidVideochip, CPUCacheLevel1=CPUCacheLevel1, CPUCacheLevel2=CPUCacheLevel2, CPUCacheLevel3=CPUCacheLevel3, Threads=Threads, Virtualisatie=Virtualisatie, VirtualisatieType=VirtualisatieType, CPUMultiplier=CPUMultiplier, CPUstepping=CPUstepping, CPUInstructieset=CPUInstructieset, TypeKoeling=TypeKoeling)
    return n

并发送所有值,如下所示:

        product = productNode.nodeCPU('CPU', p['AantalCores'],p['CPUSSpecNumber'],p['Snelheid'],p['MaximaleTurboFrequentie'],p['GeheugenSpecificatie'] ......

它有效:)