Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 不易损坏类型:';列表';虽然在功能中,但在功能外工作良好_Python_Python 3.x - Fatal编程技术网

Python 不易损坏类型:';列表';虽然在功能中,但在功能外工作良好

Python 不易损坏类型:';列表';虽然在功能中,但在功能外工作良好,python,python-3.x,Python,Python 3.x,我将要编写一个函数,而不是我以前的逐行代码,但它似乎不起作用 这段代码运行良好: def gdp_fdi(odf, colname): tempo = odf[odf['id'].str.contains(temp, na=False)] df = tempo[['id',colname]] return df def sortdf(df): df['id'] = pd.Categorical(df.id, ca

我将要编写一个函数,而不是我以前的逐行代码,但它似乎不起作用

这段代码运行良好:

    def gdp_fdi(odf, colname):
        tempo = odf[odf['id'].str.contains(temp, na=False)] 
        df = tempo[['id',colname]]
        return df

    def sortdf(df):
        df['id'] = pd.Categorical(df.id, categories = top20code, ordered = True)
        df.sort_values(by='id')
        return df
<THIS PART>
    top20 =fdi.sort_values('fdi_1987',ascending = False).groupby('fdi_1987').head(2)
    top20 = top20[['fdi_1987','id']].head(21)

    top20code = top20['id']
    top20code = top20code.to_string(index=False)
    top20code = top20code.split()

    temp = '|'.join(top20code)


    top20_name=gdp_fdi(wb,'name')
    top20_region=gdp_fdi(wb,'region')
    top20_gdp=gdp_fdi(gdp,'gdp_1987')

    sort20gdp=sortdf(top20_gdp)
    sort20region=sortdf(top20_region)
    sort20name=sortdf(top20_name)

    from functools import reduce

    lists=[sort20gdp,sort20region,sort20name]
    df = reduce(lambda df1, df2: pd.merge(left=df1, right=df2, on=["id"], how="inner"), lists)
    df['id'] = pd.Categorical(df.id, categories = top20code, ordered = True)
    df.sort_values(by='id')
    raw=df.sort_values(by='id')
    raw
</THIS PART>
def gdp_fdi(odf,colname):
tempo=odf[odf['id'].str.contains(temp,na=False)]
df=节奏[['id',colname]]
返回df
def sortdf(df):
df['id']=pd.Categorical(df.id,categories=top20code,ordered=True)
df.sort_值(按class='id')
返回df
top20=fdi.sort_值('fdi_1987',升序=False)。groupby('fdi_1987')。标题(2)
top20=top20['fdi_1987','id']]
top20code=top20['id']
top20code=top20code.to_字符串(索引=False)
top20code=top20code.split()
temp='|'。连接(TOP20代码)
top20\u name=gdp\u fdi(世界银行,“名称”)
排名前20位的地区=gdp和fdi(世界银行,“地区”)
前20位的gdp=gdp和fdi(gdp,'gdp\u 1987')
sort20gdp=sortdf(前20位gdp)
SORT20地区=sortdf(排名前20位的地区)
sort20name=sortdf(前20名)
从functools导入reduce
列表=[sort20gdp,sort20region,sort20name]
df=reduce(lambda-df1,df2:pd.merge(left=df1,right=df2,on=[“id”],how=“inner”),列表)
df['id']=pd.Categorical(df.id,categories=top20code,ordered=True)
df.sort_值(按class='id')
raw=df.sort_值(按class='id')
未经加工的
但当我将其作为函数编写时,我使用
'fdi'+str(year)
而不是
'fdi_1987'
,并将
作为名为
top20(year)
的函数编写

但是当我运行top20(1987)的函数时,它说我有

不可损坏类型:“列表”


我可以问一下为什么吗?重新设计函数的任何提示?

列表不能用作
字典
中的
键,因为它不易损坏。您可以改用元组。

可变对象是不可修改的,因为它们的哈希值会随着修改而改变,因此查找会失败。切换到不可变类型,例如使用
json.dumps(somelist)
的字符串或类似
tuple(somelist)

的元组https://stackoverflow.com/search?q=python+不可损坏+类型+列表
。。。在前10题中至少5题左右解决你的问题。。。我没有费心检查剩下的190个