# string.sort(), sorted(), reverse, set(), list slice,list comprehension

+关注继续查看

The sort()method changes the ordering of lists in-place.

>>> l=[1,2,3,2,3,4,1]
>>> l.sort()
>>> print(l)
[1, 1, 2, 2, 3, 3, 4]

The sorted() BIF sorts most any data structure by providing copied sorting.

>>> l=[1,2,3,2,3,4,1]
>>> sorted(l)
[1, 1, 2, 2, 3, 3, 4]
>>> print(l)
[1, 2, 3, 2, 3, 4, 1]

Pass reverse=True to either sort()or sorted()to arrange your data in descending order.
sort()和sorted()的反向排序参数reverse=True
>>> sorted(l,reverse=True)
[4, 3, 3, 2, 2, 1, 1]

When you have code like this:
  new_l = []
for t in old_l:
new_l.append(len(t))

rewrite it to use a list comprehension,
like this:

new_l = [len(t) for t in old_l]

list轮询处理产生另一个list的缩写
>>> l=[1,2,3,2,3,4,1]
>>> print([x*2 for x in l])
[2, 4, 6, 4, 6, 8, 2]

To access more than one data item from a list, use a slice. For example:
my_list[3:6] 

accesses the items from index location 3 up-to-but-not-including index location 6.
list slice的访问方法, 不包含最大值索引.(索引从0开始), 反向索引从-1开始.
>>> l=[0,1,2,3,4,5,6,7]
>>> print(l[-3:-1])
[5, 6]
>>> print(l[0:3])
[0, 1, 2]

Createa setusing the set() factory function.

>>> l=[1,3,1,3,1,3]
>>> for i in l:
...   if i not in new_l:
...     new_l.append(i)
...
>>> print(new_l)
[1, 3]

>>> new_set=set(l)
>>> print(new_set)
{1, 3}

>>> new_set1=set()
>>> print(type(new_set1))
<class 'set'>

>>> new_set2={}
>>> print(type(new_set2))
<class 'dict'>

【Elastic Engineering】Elasticsearch：管理 Elasticsearch 内存并进行故障排除
Elasticsearch：管理 Elasticsearch 内存并进行故障排除
2 0
【Typescript入门手册】类型进阶
【Typescript入门手册】类型进阶
12 0
python中循环的技巧

15 0
python中对切片的理解

14 0
mysql总结
mysql总结
26 0

22 0
HTML中的<meta/>标签还能这么玩？？？【居然能搞动画】
HTML中的<meta/>标签还能这么玩？？？【居然能搞动画】
6 0

16 0

Elastic自身设计了集群分片的负载平衡机制，当有新数据节点加入集群或者离开集群，集群会自动平衡分片的负载分布。
7 0
+关注

2153

245

《2021云上架构与运维峰会演讲合集》

《零基础CSS入门教程》

《零基础HTML入门教程》