我有两个名单,我正试图拉链。我想对它们进行压缩,以便通过它们的id值进行匹配。因此,对于行,id为'Video ID',对于founded_evideo,id为'post_id'。我想拉链以便ids匹配。
我试过先使用sort_values函数对熊猫框架进行排序,但这似乎对zip函数没有影响。例如:
码
founded_edvideos_list = find_matching_posts_forupdate(video_id_list, stop_at_page=stop_at_page)
founded_edvideos_df = pd.DataFrame(founded_edvideos_list)
founded_edvideos_df = founded_edvideos_df.sort_values(by=['post_id'], ascending=True)
founded_edvideos_df = founded_edvideos_df.where(pd.notnull(founded_edvideos_df), None)
excel_data = file_as_df[file_as_df['Video ID'].isin(founded_edvideos_df['post_id'])]
excel_data = excel_data.copy()
excel_data['Video ID'] = excel_data['Video ID'].astype("category")
excel_data['Video ID'].cat.set_categories(file_as_df['Video ID'].to_list(), inplace=True)
excel_data.sort_values(["Video ID"])
excel_data = excel_data.to_dict('r')
for (row,founded_evideo) in zip(excel_data,founded_edvideos_list):
more code行示例
{
'Video ID': 12894,
'Title': 'Trailblazer Melba Pattillo Beals',
'Excerpt': 'Meet Melba Pattillo Beals PhD., member of the Little Rock Nine, and trailblazer for civil rights.',
'Video Description': 'Meet Melba Pattillo Beals PhD., member of the Little Rock Nine, and trailblazer for civil rights.',
'Quick Ideas for Using the Video': '',
'Video URL': 'https://vimeo.com/32351',
'Keywords': "African American History, African American Studies, America and Civil Rights"
}founded_evideo示例
{
'post_id': 12994,
'title': 'Trailblazer Melba Pattillo Beals',
'video_page_description': 'Meet Melba Pattillo Beals PhD., member of the Little Rock Nine, and trailblazer for civil rights.',
'content': '<p>Meet Melba Pattillo Beals PhD., member of the Little Rock Nine, and trailblazer for civil rights.</p>\n',
'quick_ideas': '',
'video_url': 'https://vimeo.com/3242',
'keywords': ''
}发布于 2022-03-05 20:06:01
你可以这样做:
l1 = [
{"Video ID": 12894, "Title": "title1"},
{"Video ID": 14897, "Title": "title2"},
{"Video ID": 45124, "Title": "title4"},
{"Video ID": 54613, "Title": "title3"},
]
l2 = [
{"post_id": 54613, "Title": "title3"},
{"post_id": 10278, "Title": "title4"},
{"post_id": 12894, "Title": "title1"},
{"post_id": 14897, "Title": "title2"},
]
common_ids = set(item["post_id"] for item in l2).intersection(
set(item["Video ID"] for item in l1)
)
def new(list, key):
"""Helper function which returns list of dicts which ids are found in common_ids."""
return sorted(
[item for item in list if item[key] in common_ids], key=lambda x: x[key]
)
for item1, item2 in zip(new(l1, "Video ID"), new(l2, "post_id")):
print(item1, item2)
# Output
{'Video ID': 12894, 'Title': 'title1'} {'post_id': 12894, 'Title': 'title1'}
{'Video ID': 14897, 'Title': 'title2'} {'post_id': 14897, 'Title': 'title2'}
{'Video ID': 54613, 'Title': 'title3'} {'post_id': 54613, 'Title': 'title3'}https://stackoverflow.com/questions/71314691
复制相似问题