代码之家  ›  专栏  ›  技术社区  ›  user3476463

从dicts列表中取样[关闭]

  •  -1
  • user3476463  · 技术社区  · 6 年前

    我有如下的数据样本。它非常大,我想从中抽取前10件。它看起来像是一个dict列表,但是如果我尝试user_train[:5],就会得到一个错误。我可以像用户培训[4]一样一次抽取一个项目。任何提示都非常感谢。

    代码:

    user_train[0]
    

    输出:

    [{u'asin': u'B00APT3MHO',
      u'helpful': [0, 0],
      u'overall': 5.0,
      'productid': 1,
      u'reviewText': u"Good for someone who likes skinny jeans but doesn't look great in the legging-tight ones. A little stretchy. Not super tight in the knee or ankle, but snug on the thigh and calf.",
      u'reviewTime': u'11 17, 2013',
      u'reviewerID': u'A1JWX45KHE34AL',
      u'reviewerName': u'varnienarsil',
      u'summary': u'Love these jeans',
      u'unixReviewTime': 1384646400},
     {u'asin': u'B00CJ5NH36',
      u'helpful': [0, 0],
      u'overall': 5.0,
      'productid': 2,
      u'reviewText': u"This shirt with it's bold graphic is seriously adorable. I have pretty narrow shoulders, and like the way the sleeves slope off them. The shirt fits loosely in a way that is flattering and I liked the length. I'm no model, but the shirt looks on me as great as it looks in the photo.",
      u'reviewTime': u'11 17, 2013',
      u'reviewerID': u'A1JWX45KHE34AL',
      u'reviewerName': u'varnienarsil',
      u'summary': u'As cute as it looks',
      u'unixReviewTime': 1384646400},
     {u'asin': u'B00F9NGAPM',
      u'helpful': [1, 1],
      u'overall': 3.0,
      'productid': 4,
      u'reviewText': u"The shirt is a little flowy-er than I expected. I like the way it drapes, but the arms are a bit loose (and on me, short—I'm pretty tall). Has a sort of after-yoga feel rather than the urban feel I was looking for. Super comfortable.",
      u'reviewTime': u'11 17, 2013',
      u'reviewerID': u'A1JWX45KHE34AL',
      u'reviewerName': u'varnienarsil',
      u'summary': u"Like, don't love",
      u'unixReviewTime': 1384646400}]
    
    
    
    Update:
    
    code:
    
    user_train[:5]
    
    error:
    
    ---------------------------------------------------------------------------
    TypeError                                 Traceback (most recent call last)
    <ipython-input-17-bb27c2e9fa75> in <module>()
    ----> 1 user_train[:5]
    
    TypeError: unhashable type
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   Elodin    6 年前

    如果你想找到解决办法的话。你可以去:

    sample = [user_train[x] for x in range(10)]

    这称为列表理解,不可更改的类型错误通常是由于试图将dict转换为列表所致。