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

在Python中,当列表太大时不可修改的列表

  •  0
  • msh855  · 技术社区  · 7 年前

    我有两个数据帧(都很大), df df3 ,以及以下代码行:

      set1 = df3['JointObligorID'].unique()
      set2 = df['JointObligorID'].unique()
      set3 = list(set(set1).intersection(set2)) 
    
      # slice df3 by keeping what's in set3 
      df4 = df3[df3['JointObligorID'].isin([set3])]
    

    但是,当我到达最后一步时,会出现以下错误:

    TypeError: unhashable type: 'list'
    

    提供的解决方案 here 不起作用,要么是因为有具体的问题或一些答案不够笼统(我的清单)- set3

    有人能帮忙解决这个问题吗?

    2 回复  |  直到 7 年前
        1
  •  3
  •   Rakesh    7 年前

    您正在传递一个列表列表,而不是使用

    df4 = df3[df3['JointObligorID'].isin(set3)]
    
        2
  •  1
  •   vurmux    7 年前

    如果您想要hashable list-like结构,可以使用 tuples

    set3 = tuple(set(set1).intersection(set2))

    而不是:

    set3 = list(set(set1).intersection(set2))