python:数组/列表(remove()函数、append()函数、sort()函数、reverse()函数)
排序:
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()函数)的更多相关文章
- python数组列表、字典、拷贝、字符串
python中字符串方法 name = "I teased at life as if it were a foolish game" print(name.capitalize( ...
- Python数组列表(List)
Python数组列表 数组是一种有序的集合,可以随时添加和删除其中的元素. 一.数组定义: 数组是最常用的Python数据类型,它可以作为一个方括号内的逗号分隔值出现. 数组的数据项不需要具有相同的类 ...
- python 里列表 extend 与 append 的区别
extend 只能添加以列表形式的,而 append 可以添加任何的. 来自别人家的官方句子: extend 与 append 方法的相似之处在于都是将新接收到参数放置到已有列表的后面.而 exten ...
- Python基础-列表、元组、字典、字符串(精简解析),全网最齐全。
一.列表 =====================================================1.列表的定义及格式: 列表是个有序的,可修改的,元素用逗号隔开,用中括号包围的序列 ...
- Python基础-列表、元组、字典、字符串(精简解析)
一.列表 =====================================================1.列表的定义及格式: 列表是个有序的,可修改的,元素用逗号隔开,用中括号包围的序列 ...
- python中列表和字典常用方法和函数
Python列表函数&方法 Python包含以下函数: 序号 函数 1 cmp(list1, list2)比较两个列表的元素 2 len(list)列表元素个数 3 max(list)返回列表 ...
- python字符串 列表 元组 字典相关操作函数总结
1.字符串操作函数 find 在字符串中查找子串,找到首次出现的位置,返回下标,找不到返回-1 rfind 从右边查找 join 连接字符串数组 replace 用指定内容替换指定内容,可以指定次数 ...
- python中列表常用的几个操作函数
# coding=utf-8#在列表末尾添加新的对像#实例展现函数append()的用法aList=[456,'abc','zara','ijk',2018]aList.append(123)prin ...
- Python: 字典列表: itemgetter 函数: 根据某个或某几个字典字段来排序列表
问题:根据某个或某几个字典字段来排序Python列表 answer: 通过使用operator 模块的itemgetter 函数,可以非常容易的排序这样的数据结构 eg: rows = [ {'fna ...
随机推荐
- BZOJ5450: 轰炸(水题,Tarjan缩点求最长路)
5450: 轰炸 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 43 Solved:18[Submit][Status][Discuss] Desc ...
- SpringBoot 项目修改html后不需要重新启动(热部署)
基于IDEA配置: 一.引入依赖 <dependency> <groupId>org.springframework.boot</groupId> <arti ...
- Quartz 2D编程指南(4) - 颜色和颜色空间
不同的设备(显示器.打印机.扫描仪.摄像头)处理颜色的方式是不同的.每种设备都有其所能支持的颜色值范围.一种设备能支持的颜色可能在其它设备中无法支持.为了有效的使用颜色及理解Quartz 2D中用于颜 ...
- Nchan 实时消息内置变量
以下参考官方文档: $nchan_channel_idThe channel id extracted from a publisher or subscriber location requ ...
- 笔记:服务器压缩方案 来源于 Accept-Encoding: gzip, deflate 问题
笔记:服务器压缩方案 来源于 Accept-Encoding: gzip, deflate 问题 事情起因:odoo demo 没有启动web 压缩 目前流行的 web 压缩技术 gzip br 支持 ...
- Errors running builder 'DeploymentBuilder' on project ' 解决方法
此问题一般发生在Myeclipse 保存文件并自动部署时候. Errors occurred during the build. Errors running builder 'DeploymentB ...
- 谈谈JS中的高级函数
博客原文地址:Claiyre的个人博客如需转载,请在文章开头注明原文地址 在JavaScript中,函数的功能十分强大.它们是第一类对象,也可以作为另一个对象的方法,还可以作为参数传入另一个函数,不仅 ...
- 关于WCF引用方式之WCF服务寄宿控制台
1.创建解决方案WCFService 依次添加四个项目,如上图,Client和Hosting为控制台应用程序,Service和Service.Interface均为类库. 2.引用关系 Service ...
- tensorboard的使用
# -*- coding: utf-8 -*- """ Created on: 2017/10/29 @author : Shawn function : "& ...
- 【kubernetes】kubectl logs connection refused
因为启动dashboard报CrashLoopBackOff错误,尝试使用logs去查看日志,结果报错,错误如下: [root@localhost ~]# kubectl -s http://192. ...