# 1. 选择嵌套列表里的元素(内部进行了for循环)

li = [1,2,"age",["熊红","你好",["",45]],"abc",True]
a = li[3][2][1]
print(a)

运行结果:

45

Process finished with exit code 0

#2. 字符串转换列表

a = "iamchinese"
b =list(a)
print(b)

运行结果:

['i', 'a', 'm', 'c', 'h', 'i', 'n', 'e', 's', 'e']

Process finished with exit code 0

#3. 列表转换字符串

第一种方法:(for循环)

li =[123,456,"abcdefg"]
b = " "
for a in li:
b=b+str(a)
print(b)

运算结果:

123456abcdefg

Process finished with exit code 0

第二种方法:(列表中的元素只有字符串时)

li =["abcdefg"]
a ="".join(li)
print(a)

运行结果:

abcdefg

Process finished with exit code 0

#4. list 的其他功能

-追加(也可以加字符串,列表等)

li = [123,13121,55]
li.append(6)
print(li)

运算结果:

[123, 13121, 55, 6]

Process finished with exit code 0

-清空

li = [123,13121,55]
li.clear()
print(li)

运算结果:

[]

Process finished with exit code 0

-浅拷贝

li = [123,13121,55]
a = li.copy()
print(li)
print(a)

运算结果:

[123, 13121, 55]
[123, 13121, 55] Process finished with exit code 0

-计数

li = [55,123,13121,55]
a =li.count(55) #计算 55 出现过几次 print(a)

运算结果:

2

Process finished with exit code 0

-迭加

li = [1,2,3,4,5]
li.extend([7,8,9,"你好"]) print(li)

运算结果:

[1, 2, 3, 4, 5, 7, 8, 9, '你好']

Process finished with exit code 0

ps: append 将整个元素添加, extend 是内部经过for循环将一个添加

-获取位置

li = [1,22,33,4,5,33]
a=li.index(33) #(从左向右获取索引位置(获取到第一个就不会继续了),可以指定位置li.index(0:3)) print(a)

运算结果:

2           #(索引,不是数量)

Process finished with exit code 0

-删除某个元素并获取删除的元素

li = [1,22,33,4,5,33]
a=li.pop() # 可以指定索引位置(不指定索引,默认删除最后一个)
print(li)
print(a)

运算结果:

[1, 22, 33, 4, 5]
33 Process finished with exit code 0

-删除指定元素

li = [1,22,33,4,5,33]
li.remove(33) #从左向右只删除第一个
print(li)

运算结果:

[1, 22, 4, 5, 33]

Process finished with exit code 0

-反转

li = [1,22,33,4,5,33]
li.reverse()
print(li)

运算结果:

[33, 5, 4, 33, 22, 1]

Process finished with exit code 0

-排序

li = [1,22,33,4,5,33]
li.sort(reverse=True) # 括号里不填默认从小到大排序
print(li)

运算结果:

[33, 33, 22, 5, 4, 1]

Process finished with exit code 0

最新文章

  1. 60.Android通用流行框架大全
  2. SQLite手工注入方法小结
  3. (二) 一起学 Unix 环境高级编程 (APUE) 之 文件 IO
  4. typeof和instanceof简介及用法
  5. 解决:打开OleView报错 dllregisterserver in iviewers failed
  6. Informix如何释放异常的锁资源
  7. 【Windows】如何判断当前鼠标是否按下左键或右键
  8. Ombrophobic Bovines
  9. Android 自定义view实现水波纹效果
  10. 2014 ACM/ICPC Asia Regional Beijing Site
  11. 1-安装MQTT服务器(Windows)
  12. AVR 嵌入式单片机芯片的中断系统介绍
  13. MYSQL事务处理失效原因
  14. linux 进程 ctrl-c,ctrl-z,ctrl-d
  15. python 全栈开发,Day25(复习,序列化模块json,pickle,shelve,hashlib模块)
  16. Jenkins常见任务配置
  17. Android Timer的应用示例
  18. IE8崩溃在CElement::GetUpdatedLayoutWithContext
  19. Activity 在横竖屏切换情况下的生命周期变化
  20. jquery不能是使用普通的for循环 因为普通的for循环通过下表获取对象 如果通过下表获取对象的话 会转成dom对象

热门文章

  1. D-【乐】k进制数(同余)
  2. C++ <Algorithm>小小总结
  3. 配置mysql远程访问
  4. 2018-2019-2 20165312《网络攻防技术》Exp 8 Web基础
  5. git clone 报“The project you were looking for could not be found.”
  6. Flutter移动电商实战 --(40)路由_Fluro的全局注入和使用方法
  7. Git放弃修改
  8. Docs-.NET-C#-指南-语言参考-预处理器指令:#endif(C# 参考)
  9. myadmin不需要路劲提权之法
  10. Vue.js学习之简介(待续)