![](/img/trans.png)
[英]Why is it that in numpy certain methods (e.g. np.dot) can be called with dot notation, while others (e.g. np.insert) cannot?
[英]Is the claim that Python methods that use dot notation e.g. `.sort()` don't work on types other than string true?
當我在學習Codecademy的Python課程時,他們說“使用點表示法的方法僅適用於字符串”,所以... .sort()方法僅適用於String類型嗎?還是可以與其他類型進行排序? (int,float等)
您為什么不自己快速嘗試一下?
>>> l = ['c', 'b', 'a']
>>> l.sort()
>>> l
['a', 'b', 'c']
>>> l = [3, 2, 1]
>>> l.sort()
>>> l
[1, 2, 3]
>>>
實際上,不能使用sort()
字符串或字符串類型進行排序。 例如,如果您選擇執行以下操作
name = "hello"
print name.sort()
但這會引發錯誤。但是,如果您真的想對字符串或字符串類型進行sorted()
嘗試使用sorted()
,如下所示。
name = "hello"
print sorted(name)
輸出:
['e', 'h', 'l', 'l', 'o']
注意:排序時,大寫字母優先於小寫字母。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.