如何按特定键对 python zip 函数进行排序

How can you sort a python zip function by a particular key

我有两个要压缩的列表。我想压缩它们,以便它们按 id 值匹配。因此,对于行,id 将是 'Video ID',对于 founded_evideo,id 将是 'post_id'。我想压缩以便 ID 匹配。

我尝试先使用 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': ''
}

你可以这样试试:

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'}