Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python groupby没有';I don’我没有按预期工作_Python_Itertools - Fatal编程技术网

Python groupby没有';I don’我没有按预期工作

Python groupby没有';I don’我没有按预期工作,python,itertools,Python,Itertools,我正在尝试阅读excel电子表格,其中包含以下格式的一些列: column1__ column1__AccountName column1__SomeOtherFeature column2__blabla column2_SecondFeat 我已经将一行的值保存为元组列表,其中是变量x中的tuple is(column\u name,column\u value) 现在我想将其分组如下: result = { 'column__1': [list of (k,v) tuples,

我正在尝试阅读excel电子表格,其中包含以下格式的一些列:

column1__
column1__AccountName
column1__SomeOtherFeature
column2__blabla
column2_SecondFeat
我已经将一行的值保存为元组列表,其中是变量
x
中的tuple is(column\u name,column\u value)

现在我想将其分组如下:

result = { 
    'column__1': [list of (k,v) tuples, which keys start with 'column__1'],
    'column__2': [list of (k,v) tuples, which keys start with 'column__2']
}
但它并没有给出预期的结果:

>>> from itertools import groupby

>>> x
[(u'My field one__AccountName', u'Lorem ipsum bla bla bla'),
 (u'My field one__AccountNumber', u'1111111222255555'),
 (u'My field two__Num', u'Num: 612312345'),
 (u'My field two', u'asdasdafassda'),
 (u'My field three__Beneficiary International Bank Account Number IBAN',
  u'IE111111111111111111111'),
 (u'My field one__BIC', u'BLEAHBLA1'),
 (u'My field three__company name', u'Company XYZ'),
 (u'My field two__BIC', u'ASDF333')]

>>> groups = groupby(x ,lambda (field, val): field.split('__')[0])

>>> grouped_fields = {key: list(val) for key, val in groups}

>>> grouped_fields

{u'My field one': [(u'My field one__BIC', u'BLEAHBLA1')],
 u'My field three': [(u'My field three__company name', u'Company XYZ')],
 u'My field two': [(u'My field two__BIC', u'ASDF333')]}

 >>> x[0]
(u'My field one__AccountName', u'Lorem ipsum bla bla bla')

>>> x[1]
(u'My field one__AccountNumber', u'1111111222255555')

>>> x[0][0].split('__')[0] == x[1][0].split('__')[0]
True
但是,它似乎适用于初始列表的另一个实例:

>>> y = [(u'x a b__2', 3), (u'x a b__', 1), (u'x a b__1', 2), (u'y a__1', 1), (u'y a__2', 2)]

>>> y
[(u'x__2', 3), (u'x__', 1), (u'x__1', 2), (u'y__1', 1), (u'y__2', 2)]

>>> groupes_y = groupby(y, lambda (k,v): k.split('__')[0])

>>> grouped_y = {key:list(val) for key, val in groupes_y}

>>> grouped_y

{u'x': [(u'x__2', 3), (u'x__', 1), (u'x__1', 2)],
 u'y': [(u'y__1', 1), (u'y__2', 2)]}
不知道我做错了什么。

因为,您应该将
groupby
应用到已使用与
groupby
本身相同的
键进行排序的列表:

key = lambda fv: fv[0].split('__')[0]
groups = groupby(sorted(x, key=key), key=key)
然后,分组的字段是:

{u'My field one': [(u'My field one__AccountName', u'Lorem ipsum bla bla bla'),
  (u'My field one__AccountNumber', u'1111111222255555'),
  (u'My field one__BIC', u'BLEAHBLA1')],
 u'My field three': [(u'My field three__Beneficiary International Bank Account Number IBAN',
   u'IE111111111111111111111'),
  (u'My field three__company name', u'Company XYZ')],
 u'My field two': [(u'My field two__Num', u'Num: 612312345'),
  (u'My field two', u'asdasdafassda'),
  (u'My field two__BIC', u'ASDF333')]}
在第二个示例中,
y
已排序:

>>> y == sorted(y, key=key)
True