mdsk.net
当前位置:首页 >> Python怎么把一个DiCt外加的list的括号去掉 >>

Python怎么把一个DiCt外加的list的括号去掉

for k, v in d.items(): print(k, 'has these liver samples:') print('\n'.join(v))或 print('\n'.join('{} has these liver samples:\n{}'.format( k, '\n'.join(v)) for k, v in d.items())

>>> dTest = {1:"a",2:"b"}>>> lTest = [dTest]>>> lTest[{1: 'a', 2: 'b'}]>>> lTest[0]{1: 'a', 2: 'b'}是这个意思吗?如果不是请详细说明下需求是什么

tracB={} for item in B: traceB{item[0]:item[1]}

字典用values()函数转化成值的列表,用items转换成(key,value)的元组列表。 列表转换成字典,需要用2个列表转化成字典,一个是key,一个是value。比如: >>>dict(zip(['a','b','c'], range(5))) {'a': 0, 'c': 2, 'b': 1} >>> a=[1,2,3] >>> ...

#!/usr/bin/env python# -*- coding: utf-8 -*-list_ = [(1, 0.2,90,24,21),(2, 0.2,90,24,21),(3, 0.2,90,24,21)]dict_ ={i[0]:i[1:] for i in list_}#dict_ ={str(i[0]):i[1:] for i in list_}print dict_=============== RESTART: C:/Users/A...

map(lambda x:x.values(),你的list)

>>> dict = {}>>> dict['key'] = 'a'>>> print dict{'key': 'a'}>>>

只要不弄成集合就可以赋值,来给你举例 dict_1={'key_1'}这样得到的dict_1是set集合类型的,如果你想新建个字典dict_2让key的值为key_1,让value的值为value_1,则可以这样写 dict_2={}dict_2['key_1']='value_1'这是一种方法,更多方法自行了解

b = {}for dic in [each.items() for each in a]: for key,value in dic: b[key] = value

>>> a=['one','two','three']>>> b={}>>> [b.setdefault(x,y) for x,y in enumerate(a)]['one', 'two', 'three']>>> b{0: 'one', 1: 'two', 2: 'three'}>>>

网站首页 | 网站地图
All rights reserved Powered by www.mdsk.net
copyright ©right 2010-2021。
内容来自网络,如有侵犯请联系客服。zhit325@qq.com