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 ...
随机推荐
- Uoj 441 保卫王国
Uoj 441 保卫王国 动态 \(dp\) .今天才来写这个题. 设 \(f[u][0/1]\) 表示子树 \(u\) 中不选/选 \(u\) 时的最小权值和,显然有:\(f[u][0]=\sum ...
- bzoj 4852 炸弹攻击
bzoj 4852 炸弹攻击 二维平面上的最优解问题,模拟退火是一个较为优秀的近似算法. 此题确定圆心后,便可 \(O(m)\) 算出收益,且最优解附近显然也较优,是连续变化的,可以直接模拟退火. 小 ...
- java集成WebSocket向所有用户发送消息
package com.reading.controller.library; import org.springframework.stereotype.Controller; import org ...
- 《DSP using MATLAB》示例Example 8.22
代码: %% ------------------------------------------------------------------------ %% Output Info about ...
- .OPF文件剖析
OPF文档是epub电子书的核心文件,且是一个标准的XML文件,依据OPF规范,主要由五个部分组成: 1.<metadata>,元数据信息,由两个子元素组成: <dc-metadat ...
- vs2013 boost signals
#include "stdafx.h" #include <boost/signals2/signal.hpp> #include <iostream> u ...
- [LeetCode系列]最大连续子列递归求解分析
本文部分参考Discuss: LeetCode. 步骤1. 选择数组的中间元素. 最大子序列有两种可能: 包含此元素/不包含. 步骤2. 步骤2.1 如果最大子序列不包含中间元素, 就对左右子序列进行 ...
- Eclipse下无法解析注解:@Getter和@Setter
接触到一个项目,java bean全部使用@Getter和@Setter来偷懒,我用getXXX方法,结果发现编译失败,没法用.后来看到另一个项目也是用了@Getter和@Setter注解,但人家用的 ...
- FPGA能代替CPU架构吗?
你还没听过FPGA?那你一定是好久没有更新自己在企业级IT领域的知识了.今天笔者就和大家聊聊何为FPGA?FPGA主要应用场景是什么?有人说FPGA是替代传统CPU和GPU的未来,你信吗? FPGA全 ...
- linux中日志文件查找,根据关键字,vi命令,awk和wc
参考: http://www.cnblogs.com/ggjucheng/archive/2013/01/13/2856896.html 当时需求:查看系统日志,统计系统的处理时间(从请求进去系统到系 ...