是否可以将数据类型从列表更改为dict?

时间:2014-08-22 23:03:06

标签: python list dictionary key-value type-conversion

如果我有week = [{'Weekdays': 5, 'Weekends': 2}]之类的列表,是否可以从week = {'Weekdays': 5, 'Weekends': 2}的数据类型为week的同一位置获取dict?< / p>

2 个答案:

答案 0 :(得分:0)

索引列表week = week[0]并指定

In [26]: week = [{'Weekdays': 5, 'Weekends': 2}]

In [27]: week = week[0]

In [28]: week
Out[28]: {'Weekdays': 5, 'Weekends': 2}

答案 1 :(得分:0)

确保有一周,然后抓住它。你知道总会有一个星期吗?

if week: week = week[0]