排序:

1:整理顺序

#冒泡
lista = [5,7,11,19,99,63,3,9,1]
list = []
while lista != []:
number = 0
for i in lista:
if number < i:
number = i
lista.remove(number)
list.append(number)
print(list)
打印结果:
D:\untitled\1\venv\Scripts\python.exe D:/untitled/1/venv/main.py
[99, 63, 19, 11, 9, 7, 5, 3, 1] Process finished with exit code 0

#选择
lista = [5,7,11,19,99,63,3,9,1]
list = []
while lista != []:
for i in lista:
number = 1
for j in lista:
if number < j:
number = j
lista.remove(number)
list.append(number)
print(list)
打印结果:
D:\untitled\1\venv\Scripts\python.exe D:/untitled/1/venv/main.py
[99, 63, 19, 11, 9, 7, 5, 3, 1] Process finished with exit code 0

2:sort()函数

#sort函数
lista =[5,7,11,19,99,63,3,9,1]
lista.sort()
print(lista)
打印结果:
D:\untitled\1\venv\Scripts\python.exe D:/untitled/1/venv/main.py
[1, 3, 5, 7, 9, 11, 19, 63, 99] Process finished with exit code 0

3:加上一个数

#若a=18,在原来的list中按顺序加上这个个数
list = [1,4,7,9,11,14,19,34,60,79,98]
print("前:%s" %list)
a = 18
list.append(a)
list_1 = []
while list != []:
number = 100
for i in list:
if i < number:
number = i
list.remove(number)
list_1.append(number)
print("后:%s" %list_1)
打印结果:
D:\untitled\1\venv\Scripts\python.exe D:/untitled/1/venv/main.py
前:[1, 4, 7, 9, 11, 14, 19, 34, 60, 79, 98]
后:[1, 4, 7, 9, 11, 14, 18, 19, 34, 60, 79, 98] Process finished with exit code 0

实例:

3*3表

#3*3表
listx = [1,2,3,]
for i in listx:
for j in listx:
if i >= j:
x = i*j
print("%s*%s=%s" %(i,j,x) ,end=" ")
print()
打印结果:
D:\untitled\1\venv\Scripts\python.exe D:/untitled/1/venv/main.py
1*1=1
2*1=2 2*2=4
3*1=3 3*2=6 3*3=9 Process finished with exit code 0

列表反向打印:list_num = ["1","2","3"]打印顺序:3,2,1

使用len()函数

#反向列表(len() 方法返回对象(字符、列表、元组等)长度或项目个数)
list_num = ["","",""]
i = len(list_num)
print(list_num[i-1::-1])
打印结果:
D:\untitled\1\venv\Scripts\python.exe D:/untitled/1/venv/main.py
['', '', ''] Process finished with exit code 0

使用reverse()函数

list_num = ["","",""]
list_num.reverse()
print(list_num)
打印结果:
D:\untitled\1\venv\Scripts\python.exe D:/untitled/1/venv/main.py
['', '', ''] Process finished with exit code 0

使用for语句

a = [1,2,3,4,5,6]
num = len(a)
for i in range(int(num/2)):
a[i],a[num -i -1 ] = a[num-i-1],a[i]
print(a)
D:\untitled\1\venv\Scripts\python.exe D:/untitled/1/venv/main.py
[6, 5, 4, 3, 2, 1] Process finished with exit code 0

小知识:

list_num = ["","",""]
for i in list_num[::-1]:#从后往前取值
print(i)
打印结果:
D:\untitled\1\venv\Scripts\python.exe D:/untitled/1/venv/main.py
3
2
1 Process finished with exit code 0

python:数组/列表(remove()函数、append()函数、sort()函数、reverse()函数)的更多相关文章

  1. python数组列表、字典、拷贝、字符串

    python中字符串方法 name = "I teased at life as if it were a foolish game" print(name.capitalize( ...

  2. Python数组列表(List)

    Python数组列表 数组是一种有序的集合,可以随时添加和删除其中的元素. 一.数组定义: 数组是最常用的Python数据类型,它可以作为一个方括号内的逗号分隔值出现. 数组的数据项不需要具有相同的类 ...

  3. python 里列表 extend 与 append 的区别

    extend 只能添加以列表形式的,而 append 可以添加任何的. 来自别人家的官方句子: extend 与 append 方法的相似之处在于都是将新接收到参数放置到已有列表的后面.而 exten ...

  4. Python基础-列表、元组、字典、字符串(精简解析),全网最齐全。

    一.列表 =====================================================1.列表的定义及格式: 列表是个有序的,可修改的,元素用逗号隔开,用中括号包围的序列 ...

  5. Python基础-列表、元组、字典、字符串(精简解析)

    一.列表 =====================================================1.列表的定义及格式: 列表是个有序的,可修改的,元素用逗号隔开,用中括号包围的序列 ...

  6. python中列表和字典常用方法和函数

    Python列表函数&方法 Python包含以下函数: 序号 函数 1 cmp(list1, list2)比较两个列表的元素 2 len(list)列表元素个数 3 max(list)返回列表 ...

  7. python字符串 列表 元组 字典相关操作函数总结

    1.字符串操作函数 find 在字符串中查找子串,找到首次出现的位置,返回下标,找不到返回-1 rfind 从右边查找 join 连接字符串数组 replace 用指定内容替换指定内容,可以指定次数 ...

  8. python中列表常用的几个操作函数

    # coding=utf-8#在列表末尾添加新的对像#实例展现函数append()的用法aList=[456,'abc','zara','ijk',2018]aList.append(123)prin ...

  9. Python: 字典列表: itemgetter 函数: 根据某个或某几个字典字段来排序列表

    问题:根据某个或某几个字典字段来排序Python列表 answer: 通过使用operator 模块的itemgetter 函数,可以非常容易的排序这样的数据结构 eg: rows = [ {'fna ...

随机推荐

  1. 【2018.06.26NOIP模拟】T2号码bachelor 【数位DP】*

    [2018.06.26NOIP模拟]T2号码bachelor 题目描述 Mike 正在在忙碌地发着各种各样的的短信.旁边的同学 Tom 注意到,Mike 发出短信的接收方手机号码似乎都满足着特别的性质 ...

  2. Excel VBA to Interact with Other Applications

    转载自:https://analysistabs.com/excel-vba/interact-with-other-applications/ Interact with PowerPoint fr ...

  3. 如何使用 MSBuild Target(Exec)中的控制台输出

    我曾经写过一篇文章 如何创建一个基于命令行工具的跨平台的 NuGet 工具包,通过编写一个控制台程序来参与编译过程.但是,相比于 基于 Task 的方式,可控制的因素还是太少了. 有没有什么办法能够让 ...

  4. .NET 中的轻量级线程安全

    对线程安全有要求的代码中,通常会使用锁(lock).自 .NET 诞生以来就有锁,然而从 .NET Framework 4.0 开始,又诞生了 6 个轻量级的线程安全方案:SpinLock, Spin ...

  5. GraphQL和RESTful的区别

    GraphQL和RESTful的区别 http://graphql.cn/learn/ https://www.cnblogs.com/Wolfmanlq/p/9094418.html http:// ...

  6. 进阶的Redis之数据持久化RDB与AOF

    大家都知道,Redis之所以性能好,读写快,是因为Redis是一个内存数据库,它的操作都几乎基于内存.但是内存型数据库有一个很大的弊端,就是当数据库进程崩溃或系统重启的时候,如果内存数据不保存的话,里 ...

  7. 前端(四):JavaScript面向对象之自定义对象

    一.对象 1.字面量创建对象 var person = { name: "sun", age: 18, work: function () { console.log(this.n ...

  8. oracle-分区(笔记)

    partition by 用于指定分区方式 range 表示分区的方式是范围划分 partition pn 用于指定分区的名字 values less than 指定分区的上界(上限) ------- ...

  9. MAC OS、Windows 、HTML,CSS,font-family:中文字体的英文名称

    宋体 SimSun 黑体 SimHei 微软雅黑 Microsoft YaHei 微软正黑体 Microsoft JhengHei 新宋体 NSimSun 新细明体 PMingLiU 细明体 Ming ...

  10. 【java基础】java中ArrayList,LinkedList

    [一]ArrayList 一ArrayList的内部结构 (1)ArrayList内部维护的是一个Object数组 (2)ArrayList数组扩容后数组的长度的公式:旧的数组长度+(旧数组长度> ...