You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于两个字典列表实现按用户名获取评论的生成器函数需求

实现根据用户名生成所有评论的生成器函数

问题背景

我们有两个字典列表:

  • Reviewers_dicts:每个元素包含用户信息,定义的键为 ['Username', 'DOB', 'State', 'Reviewed_ProductID'](注:示例数据中实际键为Reviewed,需保持结构统一)
  • Products_dicts:每个元素包含产品信息,定义的键为 ['ProductID', 'sku', 'name_title', 'description', 'list_price', 'sale_price', 'category', 'category_tree', 'average_product_rating', 'product_url', 'product_image_urls', 'brand', 'total_number_reviews', 'Reviews', 'Bought With'](注:示例数据中产品ID键为uniq_id,需保持结构统一)

需求是编写一个生成器函数,接收用户名作为参数,逐个生成该用户撰写的所有评论。

示例数据

Products_dicts 示例条目

products_dicts = [
    {
        'uniq_id': 'b6c0b6bea69c722939585baeac73c13d',
        'total_number_reviews': 8,
        'Reviews': [
            {'User': 'fsdv4141', 'Review': 'You never have to worry about the fit...Alfred Dunner clothing sizes are true to size and fits perfectly. Great value for the money.', 'Score': 2},
            {'User': 'krpz1113', 'Review': 'Good quality fabric. Perfect fit. Washed very well no iron.', 'Score': 4},
            {'User': 'mbmg3241', 'Review': 'I do not normally wear pants or capris that have an elastic waist, but I decided to try these since they were on sale and I loved the color. I was very surprised at how comfortable they are and wear really well even wearing all day. I will buy this style again!', 'Score': 4},
            {'User': 'zeqg1222', 'Review': 'I love these capris! They fit true to size and are so comfortable to wear. I am planning to order more of them.', 'Score': 1},
            {'User': 'nvfn3212', 'Review': 'This product is very comfortable and the fabric launders very well', 'Score': 1},
            {'User': 'aajh3423', 'Review': 'I did not like the fabric. It is 100% polyester I thought it was different.I bought one at the store apprx two monts ago, and I thought it was just like it', 'Score': 5},
            {'User': 'usvp2142', 'Review': 'What a great deal. Beautiful Pants. Its more than I expected.', 'Score': 3},
            {'User': 'yemw3321', 'Review': 'Alfred Dunner has great pants, good fit and very comfortable', 'Score': 1}
        ],
        'Bought With': ['898e42fe937a33e8ce5e900ca7a4d924', '8c02c262567a2267cd207e35637feb1c', 'b62dd54545cdc1a05d8aaa2d25aed996', '0da4c2dcc8cfa0e71200883b00d22b30', '90c46b841e2eeece992c57071387899c']
    }
]

Reviewers_dicts 示例条目

Reviewers_dicts = [
    {'Username': 'bkpn1412', 'DOB': '31.07.1983', 'State': 'Oregon', 'Reviewed': ['cea76118f6a9110a893de2b7654319c0']},
    {'Username': 'gqjs4414', 'DOB': '27.07.1998', 'State': 'Massachusetts', 'Reviewed': ['fa04fe6c0dd5189f54fe600838da43d3']},
    {'Username': 'fsdv4141', 'DOB': '01.01.1970', 'State': 'California', 'Reviewed': ['b6c0b6bea69c722939585baeac73c13d']}
]

原代码的问题

原代码存在多处问题:

  • 语法错误:for dict_key in reviewers_dicts 末尾缺少冒号
  • 变量名错误:参数val未使用,反而引用未定义的val1;大小写不统一(reviewers_dicts vs Reviewers_dicts);产品列表变量名拼写错误(prodcuts_dicts)
  • 逻辑错误:条件判断逻辑混乱,未正确关联用户的已评论产品ID与产品中的评论

正确的生成器函数实现

def find_reviews(username):
    # 定位目标用户及其评论过的产品ID
    target_user = None
    for user in Reviewers_dicts:
        if user['Username'] == username:
            target_user = user
            break
    if not target_user:
        return  # 用户不存在时直接返回
    
    reviewed_product_ids = target_user.get('Reviewed', [])
    
    # 遍历所有产品,筛选目标用户的评论
    for product in products_dicts:
        product_id = product.get('uniq_id')  # 需与实际产品ID键名统一,比如改为ProductID
        if product_id in reviewed_product_ids:
            # 遍历产品下的所有评论,匹配当前用户
            for review in product.get('Reviews', []):
                if review.get('User') == username:
                    yield review  # 逐个生成评论

使用示例

# 生成用户fsdv4141的所有评论
for review in find_reviews('fsdv4141'):
    print(f"评分: {review['Score']}\n评论内容: {review['Review']}\n")

输出:

评分: 2
评论内容: You never have to worry about the fit...Alfred Dunner clothing sizes are true to size and fits perfectly. Great value for the money.

内容的提问来源于stack exchange,提问作者GLM

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 14:55:13