首页 > 编程语言 >Python基础二【列表】

Python基础二【列表】

时间:2022-10-12 13:15:10浏览次数:48  
标签:good spam Python 基础 list 列表 yestody print

 1 #列表【可变数据类型】
 2 spam=['cat', 'bat', 'rat', 'elephant']
 3 print(spam[1])
 4 
 5 spam = [['cat', 'bat'], [10, 20, 30, 40, 50]]
 6 print(spam[0])  #['cat', 'bat']
 7 print( spam[0][1])  #'bat'
 8 print(spam[1][4])  # 50
 9 
10 #负数下标:整数值−1 指的是列表中的最后一个下标,−2 指的是列表中倒数第二个下标,以此类推。
11 
12 spam = ['cat', 'bat', 'rat', 'elephant']
13 print(spam[-1])  #elephant
14 
15 #切片取得子列表
16 print(spam[1:2])# 参数:开始下标:结束下标(不包含)
17 
18 #用 len()取得列表的长度
19 
20 print(len(spam))  #4
21 
22 #用下标改变列表中的值
23 spam[1]="today"
24 print(spam)   #['cat', 'today', 'rat', 'elephant']
25 
26 #列表连接  【+ 操作符】
27 list=[' I was ',' not good',' yestody!']
28 print(spam+list)  #['cat', 'today', 'rat', 'elephant', ' I was ', ' not good', ' yestody!']
29 
30 #列表复制【*整数】操作符
31 print(list*2)   #[' I was ', ' not good', ' yestody!', ' I was ', ' not good', ' yestody!']
32 
33 #从列表中删除值【del 语句】
34 del (list[0])
35 print(list)   #[' not good', ' yestody!']
36 
37 #列表连接
38 who='I';
39 list= [who]+list
40 print(list)   #['I', ' not good', ' yestody!']
41 
42 #列表迭代
43 for i in range(len(list)):
44     print(list[i])
45 
46 #批量赋值
47 word1,word2,word3=list
48 print(word3)# ' yestody'
49 
50 #检查列表中是否包含某值【in , not in】
51 print( 'wang' in list)#False
52 print('wanng' not in list) #True
53 
54 #获取某字符串在列表中的下标; 注意,如果列表中有多个,则只返回第一次出现的下标;不在列表中时,报ValueError
55 print(list.index('I')) #0
56 list=list+['I']
57 print(list) #['I', ' not good', ' yestody!', 'I']
58 print(list.index('I')) #0
59 #print(list.index('sorry')) #ValueError: 'sorry' is not in list
60 
61 #列表中添加值 append() 和 insert()
62 list.append('stupid')#将数据添加到末尾
63 print(list)#['I', ' not good', ' yestody!', 'I', 'stupid']
64 list.insert(1,'dog')#第一个参数是要插入的下标,第二个参数是值
65 print(list)#['I', 'dog', ' not good', ' yestody!', 'I', 'stupid']
66 
67 #从列表中删除值【remove()】
68 list.remove('stupid')#注意:此方法 没有返回值
69 print(list)#['I', 'dog', ' not good', ' yestody!', 'I']
70 
71 #列表排序【sort()方法,当场对序列排序,返回空;不能对既有数字又和字符串的列表进行比较】
72 intList=[1,5,10,8,2,3]
73 print(intList)
74 intList.sort()
75 print(intList)#[1, 2, 3, 5, 8, 10]
76 intList.sort(reverse=True)
77 print(intList)#[10, 8, 5, 3, 2, 1]
78 print(intList.sort(reverse=True))#None
79 
80 intStrList=['Alice','Bob',1,7,3]
81 intStrList.sort()#TypeError: '<' not supported between instances of 'int' and 'str'
82 
83 #sort()对字符串列表排序时,使用的“ASCII”字符顺序而不是实际的字典顺序,A-Z然后a-z
84 #如果要按字典排序,增加参数 key=str.lower
85 # strList.sort(key=str.lower)

 

标签:good,spam,Python,基础,list,列表,yestody,print
From: https://www.cnblogs.com/ltsgsh/p/16784155.html

相关文章

  • Python深度学习:逻辑、算法与编程实战
    "IT有得聊”是机械工业出版社旗下IT专业资讯和服务平台,致力于帮助读者在广义的IT领域里,掌握更专业、实用的知识与技能,快速提升职场竞争力。 今天为您推荐一本精品图书--Pyt......
  • python传递关系
    Python参数传递采用的肯定是“传对象引用”的方式。这种方式相当于传值和传引用的一种综合。如果函数收到的是一个可变对象(比如字典或者列表)的引用,就能修改对象的原始值--相......
  • python yield 进阶(一)
    PS:硬说原创我只能说自己太不要脸了就当是个搬运工吧希望对您有帮助先来看看基础的---重头戏在后面:yield的英文单词意思是生产,刚接触Python的时候感到非常困惑,一直没......
  • python lambda匿名函数
    例如:lambdax,y:x*x+y*y如何调用lambda函数?1、赋值f=lambdastr:len(str.split())f('helloworld')2、(lambdastr:len(str.split()))('helloworld')......
  • 【Py4OH 1.1.0】新版发布及鸿蒙设备Python网络编程简介
            大家好,自从去年(没错,是去年)发布了​​Py4OH第一个正式版​​之后,我开启了佛系开发模式,没有继续更新了。有小......
  • Python数据分析实战案例:统计分析微信朋友圈数据(附实操视频)
    "IT有得聊”是机械工业出版社旗下IT专业资讯和服务平台,致力于帮助读者在广义的IT领域里,掌握更专业、实用的知识与技能,快速提升职场竞争力。 本文内容将通过一个具体实例讲......
  • 为什么要让孩子学习Python
    "IT有得聊”是机械工业出版社旗下IT专业资讯和服务平台,致力于帮助读者在广义的IT领域里,掌握更专业、实用的知识与技能,快速提升职场竞争力。 每一个新领域都能给孩子们带来......
  • Python Parser的用法
    ------2022年10月12日11:56:29-------注意,在解析parse中,对于可选参数选取最长的名称,中划线转换为下划线--------PythonParser的用法文章目录[隐藏]目录一、介绍......
  • python 不可变数据类型--字符串
    不可变数据类型:字符串是不可变数据类型,但一些性质类似列表(每个位置都有索引),但因为是不可变数据类型,字符串生成了就不能发生改变(不能str[0]='d'),有些字符串方法‘改变了’,......
  • python中 yaml.dump 对dict dump成yaml文件时,给特定字符串保留(加上)单引号
    以下例子来源于https://www.cnblogs.com/saiminhou/p/13729119.htmlpythonyamldict对str类型加上单引号源数据:dict1={"user_id":23026,"contact":[{"na......