标签:utf-8 nbsp class 有序 style key val order odi
把字典转化为有序列表:
# -*- coding: utf-8 -*- import collections z={‘b‘:2,‘a‘:1,‘c‘:3,‘r‘:5,‘j‘:4} z = collections.OrderedDict(z) #字典中的key转换为列表 key_value = list(z.keys()) print(‘字典中的key转换为列表:‘, key_value) #字典中的value转换为列表 value_list = list(z.values()) print(‘字典中的value转换为列表:‘, value_list) ‘‘‘ 运行结果: 字典中的key转换为列表: [‘b‘, ‘a‘, ‘c‘, ‘r‘, ‘j‘] 字典中的value转换为列表: [2, 1, 3, 5, 4] ‘‘‘
但我实验了一下,好像正常的转化也是有序的:
# -*- coding: utf-8 -*- import collections z={‘b‘:2,‘a‘:1,‘c‘:3,‘r‘:5,‘j‘:4} #z = collections.OrderedDict(z) #字典中的key转换为列表 key_value = list(z.keys()) print(‘字典中的key转换为列表:‘, key_value) #字典中的value转换为列表 value_list = list(z.values()) print(‘字典中的value转换为列表:‘, value_list) ‘‘‘ 运行结果: 字典中的key转换为列表: [‘b‘, ‘a‘, ‘c‘, ‘r‘, ‘j‘] 字典中的value转换为列表: [2, 1, 3, 5, 4] ‘‘‘
另一例:
# -*- coding: utf-8 -*- d= {} d[‘Jim‘] = (1,35) d[‘Leo‘] = (2,37) d[‘Bob‘] = (3,40) for k in d.items(): print(k) print(‘=================‘) from collections import OrderedDict d = OrderedDict() d[‘Jim‘] = (1, 35) d[‘Leo‘] = (2, 37) d[‘Bob‘] = (3, 40) for k in d.items(): print(k) ‘‘‘ (‘Jim‘, (1, 35)) (‘Leo‘, (2, 37)) (‘Bob‘, (3, 40)) ================= (‘Jim‘, (1, 35)) (‘Leo‘, (2, 37)) (‘Bob‘, (3, 40)) ‘‘‘
例子来源:https://blog.csdn.net/qq_33936481/article/details/70255201
标签:utf-8 nbsp class 有序 style key val order odi
原文地址:https://www.cnblogs.com/yibeimingyue/p/9977029.html