[英]How to combine the elements of multiple collections in python?
I am trying to work out how to combine two collections together.我正在努力研究如何将两个集合组合在一起。 In my code, I have a list (2D) with some information, the array looks like:
在我的代码中,我有一个包含一些信息的列表(2D),数组如下所示:
array = [
["peter", "peter.com"],
["jake, "jake.com"],
["simon, "simon.com"]
]
Also I have multiple dictionaries.我也有多个字典。 Every dictionary is created by function with URL as the input eg:
function(URL)
.每个字典都是由函数创建的,以 URL 作为输入,例如:
function(URL)
。 For the first item in the array lets make the result like:对于数组中的第一项,让结果如下:
def function(url)
released = {
"burger" : 200,
"pasta" : 120,
"thai" : 70,
}
return(relased)
The point where I struggle is, I would like to combine these two collections together in list like this:我挣扎的重点是,我想将这两个集合组合在列表中,如下所示:
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]
....
]
I am also adding visualisation in case it will help:我还添加了可视化,以防万一它会有所帮助:
Which is the proper way how to achieve this?这是如何实现这一目标的正确方法? I tried using for, but obviously it leads to malfunction:
我尝试使用 for,但显然它导致故障:
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)
generates产生
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]
]
You should be looking at something like this:你应该看看这样的事情:
new_list = []
for name, URL in array:
for item, price in function(URL).items():
new_list.append([name, URL, item, price])
Or you can do it in one list comprehension:或者你可以在一个列表理解中做到这一点:
new_list = [[name, URL, item, price] for name, URL in array for item, price in function(URL).items()]
Test:测试:
array = [
['name1', 'URL1'],
['name2', 'URL2']
]
def function(URL): # mock function to return dictionary
return {f'item{i}': f'price{i}' for i in range(1, 4 if URL == 'URL1' else 3)}
new_list = [[name, URL, item, price] for name, URL in array for item, price in function(URL).items()]
pprint(new_list)
Output:输出:
[['name1', 'URL1', 'item1', 'price1'],
['name1', 'URL1', 'item2', 'price2'],
['name1', 'URL1', 'item3', 'price3'],
['name2', 'URL2', 'item1', 'price1'],
['name2', 'URL2', 'item2', 'price2']]
You can use a combination of itertools.groupby
, itertools.chain
and operator.itemgetter
in a comprehension.您可以在理解中使用
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)
Results:结果:
[['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']]
I would suggest using pandas dataframes and join them together, for that, you have to create a dataframe from your list as follows:我建议使用熊猫数据框并将它们连接在一起,为此,您必须从列表中创建一个数据框,如下所示:
df1 = pd.DataFrame(my_list, columns=['name', 'URL'])
also, create a dataframe from your dictionary as follows:另外,从您的字典中创建一个数据框,如下所示:
df2 = pd.DataFrame.from_dict(my_dict)
Then, if you have the URL column in both dataframes (which I understood from the comments), you have to join them:然后,如果您在两个数据框中都有 URL 列(我从评论中了解到),则必须加入它们:
result = df1.join(df2, on='URL')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.