简体   繁体   English

声称使用点表示法(例如.sort())的Python方法在字符串以外的其他类型上不起作用吗?

[英]Is the claim that Python methods that use dot notation e.g. `.sort()` don't work on types other than string true?

When i was studying Python course of Codecademy, they said 'Methods that use dot notation only work with strings', so... Is .sort() method only working in String type?, or Can it sort with other types? 当我在学习Codecademy的Python课程时,他们说“使用点表示法的方法仅适用于字符串”,所以... .sort()方法仅适用于String类型吗?还是可以与其他类型进行排序? (int, float, etc) (int,float等)

Why didn't you quickly try it yourself? 您为什么不自己快速尝试一下?

>>> l = ['c', 'b', 'a']        
>>> l.sort()
>>> l
['a', 'b', 'c']
>>> l = [3, 2, 1]
>>> l.sort()
>>> l
[1, 2, 3]
>>> 

Actually string or string type can't be sorted using sort() . 实际上,不能使用sort()字符串或字符串类型进行排序。 For example if you choose to do like the following 例如,如果您选择执行以下操作

name = "hello"
print name.sort()

then it'll throw an error.But still if you really like to sort string or string types then try using sorted() as shown below. 但这会引发错误。但是,如果您真的想对字符串或字符串类型进行sorted()尝试使用sorted() ,如下所示。

name = "hello"
print sorted(name)

output: 输出:

['e', 'h', 'l', 'l', 'o']

Note: Upper case letters take the precedence over the lower case letters while sorting. 注意:排序时,大写字母优先于小写字母。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么在 numpy 中某些方法(例如 np.dot)可以用点表示法调用,而其他方法(例如 np.insert)不能? - 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? 为什么有些方法使用点符号而其他方法没有? - Why do some methods use dot notation and others don't? 查找有关python“本地”类型的文档,例如set - Finding documentation on python “native” types, e.g. set 有什么方法可以将一个Graph渲染引擎(例如fdp)用于节点坐标,而将另一个Graph渲染引擎(例如点)用于边缘? - Any way to use one Graph rendering engine (e.g. fdp) for node coordinates, and the use another Graph rendering engine (e.g. dot) for edges? 使用点表示法的Python导入无法按预期进行 - Python import using dot notation doesn't work as expected 重构:用实例参数重载方法的类型安全方法是什么,例如method(self,other)? - Refactor: What is the type-safe way to overload methods with instance arguments, e.g. method(self, other)? 熊猫argmax在屏蔽后获取所有True索引(Python 3)(例如(pd.Series> 0).argmax()) - `Pandas argmax` to get all `True` indices after masking (Python 3) (e.g. (pd.Series > 0).argmax())) 如何在 python3 中使用非常小的浮点数(例如 8.5e-350) - How to work with very small float numbers in python3 (e.g. 8.5e-350) 数字后面的包含点的数组(例如[1. 2.]与没有点的数组(例如[1 2])和有什么区别? - What is the difference between and array containing dot after the number e.g. [ 1. 2. ] and an array without dot e.g. [ 1 2 ]? 如何在基于asyncio.Protocol的类中公开和使用接口方法(例如send和recv) - How to expose and use interface methods (e.g. send and recv) in a class based on asyncio.Protocol
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM