当前位置: 首页 > news >正文

python笔记(转存ipynb)------1

list1 = ["tom","cat","Lili"]
print(list1[0].title())
Tom
#append()列表方法在列表末尾添加新元素
list1.append(233)
print(list1)
#可以先创建空列表,再进行追加append(..)以添加
['tom', 'cat', 'Lili', 233]
#insert()列表方法插入元素
list1.insert(1,"kate")
print(list1)
['tom', 'kate', 'cat', 'Lili', 233]
#del...语句删除元素,注意这里是语句不是方法
del list1[2]
print(list1)
['tom', 'kate', 'Lili', 233]
#pop()列表方法删除(弹出)列表末尾的元素(对象)
poped_ele = list1.pop()
print(f"The poped element is {poped_ele}!")
print(f"The current list1 is {list1}")
#pop(n)可以从列表中部分弹出下标为n的元素
print(f"The 2th([1]) element 'kate' will be poped,the result is {list1.pop(1)} ")
The poped element is 233!
The current list1 is ['tom', 'kate', 'Lili']
The 2th([1]) element 'kate' will be poped,the result is kate 
print(f"The current list1 is {list1}")
The current list1 is ['tom', 'Lili']
#remove()列表方法根据值删除列表中的元素
list1.remove("tom")
print(f"The current list1 is {list1}")
The current list1 is ['Lili']
#sort()列表方法对列表进行默认永久排序(升)
list2 = ["d","a","c","b"]
list2.sort()
print(f"The sorted list2 is {list2}")
#reverse参数决定升降序,True则降序
The sorted list2 is ['a', 'b', 'c', 'd']
#sorted(listname)方法创建排序后的对象
list3= ["d","a","c","b"]
sorted_list3 = sorted(list3)
print(sorted_list3)
sorted_list3_reverse = sorted(list3,reverse=True)
print(sorted_list3_reverse)
['a', 'b', 'c', 'd']
['d', 'c', 'b', 'a']
#reverse(listname)方法反转列表顺序(不进行排序)
#len(list)确定列表长度
#4.操作列表
#list()将range()的结果转化为列表
numbers = list(range(1,6)) #[1,6)
print(numbers)
[1, 2, 3, 4, 5]
#min(listname),max(listname),sum(listname)对列表元素分别求最大最小与和
print(f"min:{min(numbers)} max:{max(numbers)} sum:{sum(numbers)}")
min:1 max:5 sum:15
#切片
list5 = list(range(1,6))
print(list5)
cut_list = list5[0:3]
print(cut_list)
[1, 2, 3, 4, 5]
[1, 2, 3]
#复制列表(浅拷贝)
list6 = list5[:]
print(list6)
print(id(list5) == id(list6))
[1, 2, 3, 4, 5]
False
#元组(元素不可变)
dimensions = (200,50)
print(f"({dimensions[0]},{dimensions[1]})")
for dimension in dimensions:print(dimension)
(200,50)
200
50
#in 和 not in判断元素是否在集合中
print(200 in dimensions)
print(300 not in dimensions)
True
True
#字典(键(字符串)值(任何对象)对集合)
dic = {'color' : 'yellow', "score" : 5,"a" : "3"}
dic["score"] = 3 #键不能变,值可以变,且可添加减少
print(dic)
print(dic["color"])
print(dic["score"])
print(dic["a"])
print("添加键值对:'name':'zhang san'")
dic['name'] = 'zhang san'
dic['name'].title()
print(f"添加后的字典是{dic}")
#也能进行遍历
print("遍历dic")
for key,value in dic.items():print(f"key:{key} value:{value}")
{'color': 'yellow', 'score': 3, 'a': '3'}
yellow
3
3
添加键值对:'name':'zhang san'
添加后的字典是{'color': 'yellow', 'score': 3, 'a': '3', 'name': 'zhang san'}
遍历dic
key:color value:yellow
key:score value:3
key:a value:3
key:name value:zhang san
#del() 语句可以删除键值对
del dic['score']
print()
#从字典中分离键值对,创建键值列表,items()方法可将字典生成列表(特殊列表,只可用for拷贝后使用)
#key()/values()方法可将字典的键/值生成列表(特殊列表,只可用for拷贝后使用)
keys = []
values = []
for key,value in dic.items():keys.append(key)values.append(value)pass
print(dic)
print(keys)
print(dic.keys())
print(dic.values())
print(values)
{'color': 'yellow', 'a': '3', 'name': 'zhang san'}
['color', 'a', 'name']
dict_keys(['color', 'a', 'name'])
dict_values(['yellow', '3', 'zhang san'])
['yellow', '3', 'zhang san']
#set()函数可以将传入的列表/元组去掉重复项,返回一个集合{...}
sets = set(list([1,1,1,2,3,4]))
print(sets)
{1, 2, 3, 4}
http://www.lryc.cn/news/400683.html

相关文章:

  • excel系列(二) - 利用 easypoi 快速实现 excel 文件导入导出
  • 邀请函|2024第八届中国太阳能电池浆料与金属化技术展
  • 图像边缘检测:技术原理与算法解析
  • 【Python星启航】少儿编程精英启蒙之旅 - 大纲
  • MATLAB的mat文件转换成json文件
  • STM32第九课:STM32-基于标准库的42步进电机的简单I/O控制(附电机教程,看到即赚到)
  • 文件安全传输系统,如何保障信创环境下数据的安全传输?
  • 论文分享|AAAI2024‘北航|用大语言模型缩小有监督和无监督句子表示学习的差距
  • vue3相比于vue2有哪些新特性?
  • Gooxi受邀参加第三届中国数据中心服务器与设备峰会
  • 3个实现前端节流的方法,附代码。
  • uniapp 微信小程序根据后端返回的文件链接打开并保存到手机文件夹中【支持doc、docx、txt、xlsx等类型的文件】
  • 一群追星星的人,对AI的盼与怕
  • 同步IO、异步IO以及五种网络IO模式
  • IP-Guard日志数据上传至 SYSLOG 服务器操作指南
  • 线程安全(二)synchronized 的底层实现原理、锁升级、对象的内存结构
  • 【学习笔记】无人机(UAV)在3GPP系统中的增强支持(十四)-无人机操控关键绩效指标(KPI)框架
  • 数电基础 - 半导体存储
  • 校园工会体育报名小程序的设计
  • 2024Datawhale AI夏令营---基于术语词典干预的机器翻译挑战赛--学习笔记
  • 手机下载APP (uniapp/vue)
  • python数据可视化(5)——绘制饼图
  • 实习随笔【iviews的Select实现‘与全部互斥’的多选】
  • 网站架构核心要素
  • XML 解析异常问题解决
  • C# 匿名方法、Lambda、Linq概念及联系
  • django ninja get not allowed 能用 put delete
  • 服务器操作集合
  • 论文阅读【时空+大模型】ST-LLM(MDM2024)
  • 【linux基础】linux远程传输三种免交互方式