我有以下字符串列表:
diabo_faves = [
{"Description": "Local-Standard-Office"},
{"Description": "Local-Standard-Extended},
{"Description": "Local-Standard-247"},
{"Description": "Direct-Standard-Office"},
{"Description": "Direct-Standard-Extended"},
{"Description": "Direct-Standard-247"},
{"Description": "Premium-Standard-Office"},
{"Description": "Premium-Standard-Extended"},
{"Description": "Premium-Standard-247"},
];
我希望在JSON(保持顺序)中得到以下结果:
Filters: [
[
"Local",
"Direct",
"Premium"
],
[
"Standard"
],
[
"Office",
"Extended",
"247"
]
]
但是,当我使用这种方法时:
def get_filters(self, favorites, seperator = "-", ignores = []):
favorites = self.prepare(favorites, ignores)
steps = []
for fave in favorites:
filters = fave.split(seperator)
for index, filter in enumerate(filters):
try:
steps[index].add(filter)
except IndexError:
steps.append(set([filter]))
return [list(step) for step in steps]
我得到的结果是错误的顺序如下:
Filters: [
[
"Premium",
"Local",
"Direct"
],
[
"Standard"
],
[
"247",
"Extended",
"Office"
]
]
如何更改方法以保留指定的原始订单? “本地”,“直接”,“高级”等。
答案 0 :(得分:1)
您可以使用collections.OrderedDict
和itertools.izip
执行此操作:
from collections import OrderedDict
from itertools import izip
splitted_values = izip(*(x['Description'].split('-') for x in diabo_faves))
print [list(OrderedDict.fromkeys(x)) for x in splitted_values]
#[['Local', 'Direct', 'Premium'], ['Standard'], ['Office', 'Extended', '247']]
此处splitted_values
包含所有列值:
[
('Local', 'Local', 'Local', 'Direct', 'Direct', 'Direct', 'Premium', 'Premium', 'Premium'),
('Standard', 'Standard', 'Standard', 'Standard', 'Standard', 'Standard', 'Standard', 'Standard', 'Standard'),
('Office', 'Extended', '247', 'Office', 'Extended', '247', 'Office', 'Extended', '247')
]
然后我们将其中的每一个传递给OrderedDict.fromkeys
,以便按照首先看到的顺序获取唯一的项目。如果您无法使用OrderedDict
(在Python 2.7中引入),那么您可以使用itertoools中的unique_everseen
recipe。