我有一个数据结构,其输出如下所示:
['409597', ['You'], '409597', ['matter'], '409597', ['manager'], '809558', ['metro'], '809558', ['station'], '829258', ['bucket'], '829258', ['water'], '867297'..........]
我想找到一种方法,将其分成若干个数组,这些数组根据ID号进行合并。我想要的格式如下:
['You','matter','manager'],
['metro','station'],
['bucket','water'],
...........
如何基于原始输出的相似ID(在Python中)获得这样的数组列表?
答案 0 :(得分:1)
使用#include <iostream>
struct batonik
{
char name[20];
double weight;
int calories;
};
int main()
{
batonik *ps = new batonik[3]
{
{ "snickers",2.2,350 },
{ "mars",2.6,420 },
{ "bounty",2.1,210 }
};
for (int i = 0; i < 3; i++)
{
std::cout << "Nazwa batonika : " << (*(ps+i)).name << std::endl;
std::cout << "Waga batonika : " << (ps+i)->weight << std::endl;
std::cout << "Ilosc kalorii : " << (ps+i)->calories<< std::endl;
std::cout << i;
}
delete [] ps;
return 0;
}
创建元素的键值对,然后使用zip_longest
根据键将它们分组在一起。
itertools.groupby
实际上,使用原始字典列表要容易一些。
>>> from itertools import groupby
>>> l = ['409597', ['You'], '409597', ['matter'], '409597', ['manager'], '809558', ['metro'], '809558', ['station'], '829258', ['bucket'], '829258', ['water']]
>>> [[e[1][0] for e in list(v)] for k,v in groupby(zip_longest(*([iter(l)]*2)), lambda x: x[0])]
[['You', 'matter', 'manager'], ['metro', 'station'], ['bucket', 'water']]
答案 1 :(得分:0)
来自您的来源。
from itertools import groupby
l = [[{'Instant_ID': 409597}, {'Token': 'You'}], [{'Instant_ID': 409597}, {'Token': 'matter'}], [{'Instant_ID': 409597}, {'Token': 'manager'}], [{'Instant_ID': 809558}, {'Token': 'metro'}], [{'Instant_ID': 809558}, {'Token': 'station'}], [{'Instant_ID': 829258}, {'Token': 'bucket'}], [{'Instant_ID': 829258}, {'Token': 'water'}]]
print([[i[1]["Token"] for i in value] for key, value in groupby(l, lambda x: x[0]["Instant_ID"])])
输出:
[['You', 'matter', 'manager'], ['metro', 'station'], ['bucket', 'water']]
答案 2 :(得分:0)
您也可以使用纯Python来做到这一点:
inList = ['409597', ['You'], '409597', ['matter'], '409597', ['manager'], '809558', ['metro'], '809558', ['station'], '829258', ['bucket'], '829258', ['water']]
#Find all number strings
numbers = sorted(set([inList[i] for i in range(0,len(inList),2)]))
#Create lists based on these number strings
newLists = [[inList[i][0] for i in range(1,len(inList),2) if inList[i-1]==number] for number in numbers]
print(newLists)
输出:
[['You', 'matter', 'manager'], ['metro', 'station'], ['bucket', 'water']]