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 = ["1","2","3"]
i = len(list_num)
print(list_num[i-1::-1])
打印结果:D:\untitled\1\venv\Scripts\python.exe D:/untitled/1/venv/main.py
[‘3‘, ‘2‘, ‘1‘]

Process finished with exit code 0

使用reverse()函数

list_num = ["1","2","3"]
list_num.reverse()
print(list_num)
打印结果:
D:\untitled\1\venv\Scripts\python.exe D:/untitled/1/venv/main.py
[‘3‘, ‘2‘, ‘1‘]

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 = ["1","2","3"]
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

原文地址:https://www.cnblogs.com/dxxblog/p/9007367.html

时间: 2024-08-12 16:02:36

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

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

python中字符串方法 1 name = "I teased at life as if it were a foolish game" 2 print(name.capitalize())#首字母大写 3 print(name.count("a"))#查找字符串中a的个数 4 print(name.center(50,"-"))#长度为50将name放中间不够的用-补全 5 print(name.endswith("ex"

Python 数组/列表

范围取值 指定范围 names = ["零","一","二","三","四","五","六","七"] print(names[1:4])#打印 一,二,三 左包括右不包括 从某位置取到末尾 names = ["零","一","二","三","四",

Python:列表函数

list函数: 将字符串转化成列表,例: >>> name = list('hello') >>> name ['h', 'e', 'l', 'l', 'o'] 列表基本函数: 1.改变列表:元素赋值 使用索引标记 >>>x = [1, 1, 1] >>>x[1] = 2 >>>x [1, 2, 1] 2.删除元素 del语句实现 >>>names = ['one', 'two', 'three']

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

# coding=utf-8#在列表末尾添加新的对像#实例展现函数append()的用法aList=[456,'abc','zara','ijk',2018]aList.append(123)print("Updated List:",aList)输出结果:Updated List: [456, 'abc', 'zara', 'ijk', 2018, 123] #统计列表中某个元素出现的次数#实例展现count()函数的用法aList=[456,'abc','zara','ijk',2

python中列表(list)函数及使用

序列是Python中最基本的数据结构.序列中的每个元素都分配一个数字 - 它的位置,或索引,第一个索引是0,第二个索引是1,依此类推. Python有6个序列的内置类型,但最常见的是列表和元组. 序列都可以进行的操作包括索引,切片,加,乘,检查成员. 此外,Python已经内置确定序列的长度以及确定最大和最小的元素的方法. 列表是最常用的Python数据类型,它可以作为一个方括号内的逗号分隔值出现. 列表的数据项不需要具有相同的类型 创建一个列表,只要把逗号分隔的不同的数据项使用方括号括起来即可

python中sort和sorted函数小结

因为作者本人也是刚学python不久,在看python官方文档的时候看到了 Lambda 函数,其中使用sort函数的地方看的有些迷惑,所以就查找了一些关于sort和sorted函数的资料,在此整理到博客 L.sort(cmp=None, key=None, reverse=False) sorted(iterable, cmp=None, key=None, reverse=False) 这样看,sorted函数只比sort函数多一个iterable参数,其余没什么不同,iterable是一个

Python 基础学习之: Python math 模块、cmath 模块 区别是 cmath 模块运算的是复数,math 模块运算的是数学运算 Python数学函数列表及解释 Python math 模块提供了许多对浮点数的数学运算函数。 Python cmath 模块包含了一些用于复数运算的函数

Python math 模块.cmath 模块 Python 中数学运算常用的函数基本都在 math 模块.cmath 模块中. Python math 模块提供了许多对浮点数的数学运算函数. Python cmath 模块包含了一些用于复数运算的函数. cmath 模块的函数跟 math 模块函数基本一致,区别是 cmath 模块运算的是复数,math 模块运算的是数学运算. 要使用 math 或 cmath 函数必须先导入: import math 查看 math 查看包中的内容: impo

python之集合、深浅copy、文件操作、函数初识

本节内容:集合.深浅copy.文件操作.函数初识 1.小知识点补充 2.编码二 3.集合 4.深浅copy 5.文件操作 6.函数的初识 1.小知识点补充                                                                                           1.1 小数据池====str.int 小数据池存在的意义就是节省内存 节省内存,位多个变量开辟一个共享空间(str和int) int:-5--256 int===

Python【map、reduce、filter】内置函数使用说明

介绍下Python 中 map,reduce,和filter 内置函数的方法: 一:map map(...)     map(function, sequence[, sequence, ...]) -> list 说明: 对sequence中的item依次执行function(item),执行结果输出为list. 例子: >>> map(str, range(5))           [, , , , ]        >>>  add(n): n+>&