将相似值分组

2024-05-03 11:30:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个字符串,它有多个记录,每个值都在自己的dic中。你知道吗

x = [{'bsName': 'AC SLEEPER'}, {'bsType': 'ACS'}, {'ischildconcession': 'N'}, {'isseatlayot': 'N'}, {'isseatnmber': 'N'}, {'bsName': 'AC-JANRATH'}, {'bsType': 'JNR'}, {'ischildconcession': 'N'}, {'isseatlayot': 'Y'}, {'isseatnmber': 'Y'}]

数据库中只有5列。你知道吗

bsName , bsType , ischildconcession , isseatlayot , isseatnmber 

所以结果应该是这样的。。。你知道吗

y = [{'bsName': 'AC SLEEPER', 'bsType': 'ACS', 'ischildconcession': 'N', 'isseatlayot': 'N', 'isseatnmber': 'N'}, {'bsName': 'AC-JANRATH', 'bsType': 'JNR', 'ischildconcession': 'N', 'isseatlayot': 'Y', 'isseatnmber': 'Y'}]

10个阀门分为2排,每排5个。 这只是一个示例数据,像这样序列化的值太多了。你知道吗

我尝试过简单for循环,但不知道如何将这5个值组合在一起。你知道吗

mylist=[]
for id in x:
    for i in id:
        mylist.append(i, id[i])

Tags: 字符串inidforacacsmylistsleeper
3条回答

只需使用^{}切分dict列表

>>> dics = [x[i:i+5] for i in range(0,len(x),5)]
>>> dics
[[{'bsName': 'AC SLEEPER'}, {'bsType': 'ACS'}, {'ischildconcession': 'N'}, {'isseatlayot': 'N'}, {'isseatnmber': 'N'}], [{'bsName': 'AC-JANRATH'}, {'bsType': 'JNR'}, {'ischildconcession': 'N'}, {'isseatlayot': 'Y'}, {'isseatnmber': 'Y'}]]
>>> [{i.keys()[0]:i.values()[0] for i in j} for j in dics]
[{'isseatnmber': 'N', 'bsName': 'AC SLEEPER', 'bsType': 'ACS', 'ischildconcession': 'N', 'isseatlayot': 'N'}, {'isseatnmber': 'Y', 'bsName': 'AC-JANRATH', 'bsType': 'JNR', 'ischildconcession': 'N', 'isseatlayot': 'Y'}]

步骤是5,因为您想分成5行

您可以使用^{}文档和^{}中的grouper配方将属性分组为可与^{} constructor一起使用的键/值元组:

>>> [dict(chain(*[item.items() for item in group])) for group in grouper(x, 5, {})]
[{'isseatnmber': 'N', 'bsName': 'AC SLEEPER', 'bsType': 'ACS', 'ischildconcession': 'N', 'isseatlayot': 'N'},
 {'isseatnmber': 'Y', 'bsName': 'AC-JANRATH', 'bsType': 'JNR', 'ischildconcession': 'N', 'isseatlayot': 'Y'}]

说明

来自^{}文档的grouper配方允许您将键/值dict分成5组:

>>> from itertools import izip_longest
>>>
>>> def grouper(iterable, n, fillvalue=None):
...     "Collect data into fixed-length chunks or blocks"
...     # grouper('ABCDEFG', 3, 'x')  > ABC DEF Gxx
...     args = [iter(iterable)] * n
...     return izip_longest(fillvalue=fillvalue, *args)
...
>>> groups = list(grouper(x, 5, {}))
>>> groups
[({'bsName': 'AC SLEEPER'}, {'bsType': 'ACS'}, {'ischildconcession': 'N'}, {'isseatlayot': 'N'}, {'isseatnmber': 'N'}),
 ({'bsName': 'AC-JANRATH'}, {'bsType': 'JNR'}, {'ischildconcession': 'N'}, {'isseatlayot': 'Y'}, {'isseatnmber': 'Y'})]
>>>

然后使用列表理解和dict.items()将单个键/值dict的列表转换为键/值元组:

>>> pairs = [[kv.items() for kv in group] for group in groups]
>>> pairs

[[[('bsName', 'AC SLEEPER')], [('bsType', 'ACS')], [('ischildconcession', 'N')], [('isseatlayot', 'N')], [('isseatnmber', 'N')]],
 [[('bsName', 'AC-JANRATH')], [('bsType', 'JNR')], [('ischildconcession', 'N')], [('isseatlayot', 'Y')], [('isseatnmber', 'Y')]]]

现在使用^{}list unpacking将成对列表展平为适合dict构造函数的键/值元组列表:

>>> itemgroups = [list(chain(*pair)) for pair in pairs]
>>> itemgroups[0]
[('bsName', 'AC SLEEPER'), ('bsType', 'ACS'), ('ischildconcession', 'N'), ('isseatlayot', 'N'), ('isseatnmber', 'N')]

最后一步,使用^{} constructor将这些键/值元组对转换为字典:

>>> [dict(items) for items in itemgroups]
[{'isseatnmber': 'N', 'bsName': 'AC SLEEPER', 'bsType': 'ACS', 'ischildconcession': 'N', 'isseatlayot': 'N'},
 {'isseatnmber': 'Y', 'bsName': 'AC-JANRATH', 'bsType': 'JNR', 'ischildconcession': 'N', 'isseatlayot': 'Y'}]

注意:所有这些中间的list()调用都是需要的,因为其中一些函数生成的生成器将在迭代一次后使用,否则在交互式解释器中重新使用结果进行演示是行不通的。如果将所有这些步骤结合起来,则不需要中间列表。你知道吗

将所有内容放在一起,没有中间列表:

>>> [dict(chain(*[item.items() for item in group])) for group in grouper(x, 5, {})]
[{'isseatnmber': 'N', 'bsName': 'AC SLEEPER', 'bsType': 'ACS', 'ischildconcession': 'N', 'isseatlayot': 'N'},
 {'isseatnmber': 'Y', 'bsName': 'AC-JANRATH', 'bsType': 'JNR', 'ischildconcession': 'N', 'isseatlayot': 'Y'}]

首先,我要定义一个生成器,它一次将列表分成n个项目:

def generate_groups(lst, n):
    for i in xrange(0, len(lst), n):
        yield lst[i:i+n]

然后简单地建立你的字典:

result = []
for group in generate_groups(x, 5):
    dct = {}
    for item in group:
        dct.update(item)
    result.append(dct)

以便:

>>> result
[{'bsName': 'AC SLEEPER',
  'bsType': 'ACS',
  'ischildconcession': 'N',
  'isseatlayot': 'N',
  'isseatnmber': 'N'},
 {'bsName': 'AC-JANRATH',
  'bsType': 'JNR',
  'ischildconcession': 'N',
  'isseatlayot': 'Y',
  'isseatnmber': 'Y'}]

相关问题 更多 >