如何在python中组合多个集合的元素?
我正在研究如何将两个收藏组合在一起。 在我的代码中,我有一个包含一些信息的列表(2D),数组如下所示:如何在python中组合多个集合的元素?,python,Python,我正在研究如何将两个收藏组合在一起。 在我的代码中,我有一个包含一些信息的列表(2D),数组如下所示: array = [ ["peter", "peter.com"], ["jake, "jake.com"], ["simon, "simon.com"] ] 我还有多本字典。每个字典都是由函数创建的,其输入为URL,例如:function(URL)。对于数组中的第一项,让我们将结果设置为: def function(url) released = { "burger" :
array = [
["peter", "peter.com"],
["jake, "jake.com"],
["simon, "simon.com"]
]
我还有多本字典。每个字典都是由函数创建的,其输入为URL,例如:function(URL)
。对于数组中的第一项,让我们将结果设置为:
def function(url)
released = {
"burger" : 200,
"pasta" : 120,
"thai" : 70,
}
return(relased)
我挣扎的地方是,我想把这两个系列组合在一个列表中,如下所示:
new_list = [
[peter, peter.com, burger, 200],
[peter, peter.com, pasta, 120],
[peter, peter.com, thai, 70],
[jake, jake.com, item_1, price_1],
[jake, jake.com, item_2, price_2]
....
]
new_list = []
for name, URL in array:
for item, price in function(URL).items():
new_list.append([name, URL, item, price])
我还添加了可视化,以防有帮助:
哪种方法才是实现这一目标的正确途径?
我试过使用,但显然它会导致故障:
for index, row in enumerate(array):
new_list.append([])
new_list[index].append(row[0])
new_list[index].append(row[1])
for x, y in function(row[0]).items():
new_list[index].append(x)
new_list[index].append(y)
产生
new_list = [
[name_1, URL_1, item_1, price_1, item_2, price_2, item_3, price_3],
[name_2, URL_2, item_1, price_1, item_2, price_2]
]
您应该看到以下内容:
new_list = [
[peter, peter.com, burger, 200],
[peter, peter.com, pasta, 120],
[peter, peter.com, thai, 70],
[jake, jake.com, item_1, price_1],
[jake, jake.com, item_2, price_2]
....
]
new_list = []
for name, URL in array:
for item, price in function(URL).items():
new_list.append([name, URL, item, price])
或者您可以在一个列表中完成:
new_list = [[name, URL, item, price] for name, URL in array for item, price in function(URL).items()]
测试:
输出:
[['name1', 'URL1', 'item1', 'price1'],
['name1', 'URL1', 'item2', 'price2'],
['name1', 'URL1', 'item3', 'price3'],
['name2', 'URL2', 'item1', 'price1'],
['name2', 'URL2', 'item2', 'price2']]
您可以在理解中结合使用
itertools.groupby
、itertools.chain
和operator.itemgetter
from itertools import groupby, chain
from operator import itemgetter
new_list = [
['name_1', 'URL_1', 'item_1', 'price_1'],
['name_1', 'URL_1', 'item_2', 'price_2'],
['name_1', 'URL_1', 'item_3', 'price_3'],
['name_2', 'URL_2', 'item_1', 'price_1'],
['name_2', 'URL_2', 'item_2', 'price_2']]
key = itemgetter(slice(None, 2))
result = [list(chain(k, *(i[2:] for i in g))) for k, g in groupby(new_list, key)]
print(result)
结果:
[['name_1', 'URL_1', 'item_1', 'price_1', 'item_2', 'price_2', 'item_3', 'price_3'],
['name_2', 'URL_2', 'item_1', 'price_1', 'item_2', 'price_2']]
我建议使用pandas数据帧并将它们连接在一起,为此,您必须从列表中创建一个数据帧,如下所示:
df1 = pd.DataFrame(my_list, columns=['name', 'URL'])
df2 = pd.DataFrame.from_dict(my_dict)
另外,根据字典创建数据框,如下所示:
df1 = pd.DataFrame(my_list, columns=['name', 'URL'])
df2 = pd.DataFrame.from_dict(my_dict)
然后,如果两个数据帧中都有URL列(我从评论中了解到),则必须将它们连接起来:
result = df1.join(df2, on='URL')
你怎么知道哪个项目/价格和哪个名称/url一起?关于url,我可能没有说得那么清楚,但每个字典都是由以url为输入的函数创建的。请发布一个合适的Ok Bruno,让我来修复它,让我更清楚地告诉你