如果我上课了......
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告诉我我给了它两个参数,当我只给出一个?
基于此
位置参数是一个名称,后面没有等号(=)和默认值.
关键字参数后跟一个等号和一个给出其默认值的表达式.
def rectangleArea(width, height):
return width * height
print rectangleArea(width=1, height=2)
Run Code Online (Sandbox Code Playgroud)
提问 >我假定这两个width和height的位置参数.那么为什么我们也可以用关键字真实参数语法来调用呢?
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中的值?
有人可以告诉我这里出了什么问题吗
\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\nRun 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()\nRun Code Online (Sandbox Code Playgroud)\n然后得到这些错误
\n …python ×4
seaborn ×2
arguments ×1
bar-chart ×1
matplotlib ×1
methods ×1
pandas ×1
python-3.x ×1
self ×1