相关疑难解决方法(0)

TypeError:method()占用1个位置参数,但给出了2个

如果我上课了......

class MyClass:

    def method(arg):
        print(arg)
Run Code Online (Sandbox Code Playgroud)

...我用来创建一个对象......

my_object = MyClass()
Run Code Online (Sandbox Code Playgroud)

......我就这样打电话method("foo")......

>>> my_object.method("foo")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: method() takes exactly 1 positional argument (2 given)
Run Code Online (Sandbox Code Playgroud)

...为什么Python告诉我我给了它两个参数,当我只给出一个?

python methods arguments self python-3.x

210
推荐指数
8
解决办法
41万
查看次数

位置参数vs关键字参数

基于

位置参数是一个名称,后面没有等号(=)和默认值.

关键字参数后跟一个等号和一个给出其默认值的表达式.

def rectangleArea(width, height):
    return width * height

print rectangleArea(width=1, height=2)
Run Code Online (Sandbox Code Playgroud)

提问 >我假定这两个widthheight的位置参数.那么为什么我们也可以用关键字真实参数语法来调用呢?

python

63
推荐指数
4
解决办法
10万
查看次数

使用seaborn绘制系列

category = df.category_name_column.value_counts()  
Run Code Online (Sandbox Code Playgroud)

我有以上系列返回值:

CategoryA,100
CategoryB,200
Run Code Online (Sandbox Code Playgroud)

我试图在X轴上绘制前5个类别名称,在y轴上绘制值

head = (category.head(5)) 
sns.barplot(x = head ,y=df.category_name_column.value_counts(), data=df)
Run Code Online (Sandbox Code Playgroud)

它不会在X轴上打印类别的"名称",而是打印计数.如何打印X中的前5个名称和Y中的值?

python data-visualization matplotlib pandas seaborn

2
推荐指数
1
解决办法
5500
查看次数

TypeError: barplot() 采用 0 到 1 个位置参数,但给出了 2 个

有人可以告诉我这里出了什么问题吗

\n
#stemming all the words to their root word\nstemmer = SnowballStemmer(language='english')\nstem=[]\nfor word in lines:\n    stem.append(stemmer.stem(word))\nstem[:20]\n#removes stopwords (very common words in a sentence)\nstem2 = []\nfor word in stem:\n    if word not in nlp.Defaults.stop_words:\n        stem2.append(word)\n#creates a new dataframe for the stem and shows the count of the most used words\ndf = pd.DataFrame(stem2)\ndf=df[0].value_counts()\ndf #shows the new dataframe\n
Run Code Online (Sandbox Code Playgroud)\n

我不知道这里出了什么问题,当我运行这些代码时遇到的错误 \xe2\xac\x87\xe2\xac\x87\xe2\xac\x87

\n
#plots the top 20 used words\ndf = df[:20]\nplt.figure(figsize=(10,5))\nsns.barplot(df.values, df.index, alpha=0.8)\nplt.title('Top Words Overall')\nplt.xlabel('Count of words', fontsize=12)\nplt.ylabel('Word from Tweet', fontsize=12)\nplt.show()\n
Run Code Online (Sandbox Code Playgroud)\n

然后得到这些错误

\n …

python bar-chart seaborn

-1
推荐指数
1
解决办法
2万
查看次数