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

简介:
一些小结 : 
The sort()method changes the ordering of lists in-place.
直接修改list顺序的方法.
>>> 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.
修改list顺序的内建函数, 但是不修改list变量本身, 只是输出另一个list
>>> 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.
不带重复, 无序的list新类型set.
在没有set时, list可以使用以下方法去重复.
>>> 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]

使用set()去重
>>> new_set=set(l)
>>> print(new_set)
{1, 3}

创建set类型的方法:
>>> new_set1=set()
>>> print(type(new_set1))
<class 'set'>

以下方法创建的是dict类型
>>> new_set2={}
>>> print(type(new_set2))
<class 'dict'>

目录
相关文章
|
11月前
|
存储 JavaScript Java
(Python基础)新时代语言!一起学习Python吧!(四):dict字典和set类型;切片类型、列表生成式;map和reduce迭代器;filter过滤函数、sorted排序函数;lambda函数
dict字典 Python内置了字典:dict的支持,dict全称dictionary,在其他语言中也称为map,使用键-值(key-value)存储,具有极快的查找速度。 我们可以通过声明JS对象一样的方式声明dict
579 2
|
算法
你对Collection中Set、List、Map理解?
你对Collection中Set、List、Map理解?
338 18
你对Collection中Set、List、Map理解?
|
JSON Java 关系型数据库
Java更新数据库报错:Data truncation: Cannot create a JSON value from a string with CHARACTER SET 'binary'.
在Java中,使用mybatis-plus更新实体类对象到mysql,其中一个字段对应数据库中json数据类型,更新时报错:Data truncation: Cannot create a JSON value from a string with CHARACTER SET 'binary'.
1889 4
Java更新数据库报错:Data truncation: Cannot create a JSON value from a string with CHARACTER SET 'binary'.
|
存储 JSON NoSQL
redis基本数据结构(String,Hash,Set,List,SortedSet)【学习笔记】
这篇文章是关于Redis基本数据结构的学习笔记,包括了String、Hash、Set、List和SortedSet的介绍和常用命令。文章解释了每种数据结构的特点和使用场景,并通过命令示例演示了如何在Redis中操作这些数据结构。此外,还提供了一些练习示例,帮助读者更好地理解和应用这些数据结构。
redis基本数据结构(String,Hash,Set,List,SortedSet)【学习笔记】
|
存储 NoSQL 关系型数据库
Redis 有序集合(sorted set)
10月更文挑战第17天
383 4
|
存储 分布式计算 NoSQL
大数据-40 Redis 类型集合 string list set sorted hash 指令列表 执行结果 附截图
大数据-40 Redis 类型集合 string list set sorted hash 指令列表 执行结果 附截图
305 3
|
算法
你对Collection中Set、List、Map理解?
你对Collection中Set、List、Map理解?
245 5
|
索引 Python
Python列表操作-推导式(List Comprehension)
Python列表操作-推导式(List Comprehension)
1258 0
|
存储 Java 索引
Java 中 List、Set、Map 和 Queue 之间的区别
【8月更文挑战第22天】
732 0
|
测试技术 索引 Python
Python接口自动化测试框架(基础篇)-- 常用数据类型list&set()
本文介绍了Python中list和set两种数据类型的使用,包括它们的创建、取值、增删改查操作、排序以及内置函数的使用,还探讨了list的比较函数和set的快速去重功能。
499 0