相关疑难解决方法(0)

使用python从列表中创建字典

我需要将列表转换为字典,如下所示.奇数元素具有键,偶数元素具有值.

x = (1,'a',2,'b',3,'c') - > {1: 'a', 2: 'b', 3: 'c'}

def set(self, val_): 
    i = 0 
    for val in val_: 
        if i == 0: 
            i = 1 
            key = val 
        else: 
            i = 0 
            self.dict[key] = val 
Run Code Online (Sandbox Code Playgroud)

获得相同结果的更好方法是什么?

添加

i = iter(k)
print dict(zip(i,i))
Run Code Online (Sandbox Code Playgroud)

好像在起作用

python dictionary list

27
推荐指数
4
解决办法
8万
查看次数

标签 统计

dictionary ×1

list ×1

python ×1