For in loop over dict gives TypeError: string indices must be integers

前端 未结 2 591
栀梦
栀梦 2021-01-25 23:02

I have a dict with 11 items in it, here\'s a scree shot from Spyder variable explorer:

def buildDF(result_set):
    master_dm = []
    for p in result_s         


        
2条回答
  •  无人共我
    2021-01-25 23:57

    When iterating over the dictionary try calling the dict.items() method:

    sessions1 = {10000:'val1',100000:'val2',2000:'val3',3000:'val4',4000:'val5',5000:'val6',6000:'val7',7000:'val8',8000:'val9',9000:'val10',10000:'val11'}
    def buildDF(result_set):
        master_dm = []
        for k,v in result_set.items():
            master_dm.append(v)
        return(master_dm)
    
    sessions1DF = buildDF(sessions1)
    
    print(sessions1DF)
    # ['val11', 'val2', 'val6', 'val5', 'val3', 'val8', 'val4', 'val9', 'val7', 'val10']
    

提交回复
热议问题