排序:

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. selenium-java,cookie登陆

    方法 public void login(WebDriver WebDriver,String cookie1[],String cookie2[]) throws ParseException{// ...

  2. BZOJ1017: [JSOI2008]魔兽地图DotR【树形DP】【玄学】

    Description DotR (Defense of the Robots) Allstars是一个风靡全球的魔兽地图,他的规则简单与同样流行的地图DotA (Defense of the Anc ...

  3. ubuntu 配置静态IP及DNS

    http://blog.csdn.net/njchenyi/article/details/8715417

  4. Sprint第一个冲刺(第五天)

    一.Sprint介绍 今天我们主要做的是修复乱码,完善项目. 任务进度: 二.Sprint周期 看板: 燃尽图:

  5. nginx 调试

    配置单进程非daemon方式启动 daemon off; master_process off;

  6. 给 odoo 的 bom 明细加上位置字段

    给 odoo 的 bom 明细加上位置字段 odoo 的 BOM 明细没有位置字段,这可无法用于电子生产中. 摸索了几天,找了一些教程,最终实现了. 在模型中找到 mrp_bom_line 增加自定义 ...

  7. [LeetCode系列] 二叉树最大深度求解问题(C++递归解法)

    问: 给定二叉树, 如何计算二叉树最大深度? 算法描述如下: 如果当前节点为空, 返回0(代表此节点下方最大节点数为0) 如果当前节点不为空, 返回(其左子树和右子树下方最大节点数中的最大值+1) 上 ...

  8. WaitHandle学习笔记

    信号量与互斥体 互斥体(Mutex)是操作系统中一种独占访问共享资源的机制.它像一把所锁,哪个线程获取到互斥体的控制权,则可以访问共享的资源,或者执行处于受保护的代码.而其他的线程如果也想获取控制权, ...

  9. 【转】【备忘录】MySQL性能优化的21个最佳实践 和 mysql使用索引

    今天,数据库的操作越来越成为整个应用的性能瓶颈了,这点对于Web应用尤其明显.关于数据库的性能,这并不只是DBA才需要担心的事,而这更是我 们程序员需要去关注的事情.当我们去设计数据库表结构,对操作数 ...

  10. appium+python自动化32-android_uiautomator定位进阶版

    前言 上一篇介绍uiautomator的定位方式都是类似这种'new UiSelector().xxx("xxx")',看起非常长,我也记不住,这很不python.于是本篇优化了定 ...