python note 3

本文深入讲解了元组、列表、集合、字典等基本数据结构的使用方法与特性,包括元组的创建、集合的修改及排序、字典的访问与操作等关键知识点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

元组只有一个元素的时候,在元素加上逗号
print(sport*3)——sprotsprotsprot
print(('唱歌,')*3)——唱歌,唱歌,唱歌,
print(('唱歌',)*3)——('唱歌', '唱歌', '唱歌')

元组与列表用法类似

#dir()查看元组可以调用的属性和方法
print(t1.index(3))


#元组.count(元素):查看元组中匹配到的元素个数
t1=('jj','kdjf','dkjfow','kjfo')
print(t1.count('jj'))

集合不支持索引,元素不重复
print({},type({})#字典类型
print(set(),,type())#集合类型

#集合修改元素:先删除要修改的元素,然后再添加新的元素进去
fruit.remove("香蕉")
print(fruits)
fruit.add("荔枝")
print(fruits)

#复制集合:复制值,但是不复制内存地址
print(fruits,fruits.copy())
print(id(id(fruits),id(fruits.copy()))

#清空集合
fruit.clear()

#排序
num=[int(v) for v in num]
print(num)
num.sort(reverse=False)

#去差集(-):
print(a-b)#a排除a和b的交集
print(b-a)#b排除a和b的交集
#取不同时存在的元素
print(a^b)

#访问字典——字典键用[ ]
person=dict(
    id="008",
    name="李四",
    sex="男",
    hobby=["python","踢足球"]
)
print(person)
print("身份证:{},姓名:{},性别:{},爱好:{}".format(
    person["id"],person["name"],person["sex"],"、".join(person["hobby"])
))
或者:
person=dict(
    id="008",
    name="李四",
    sex="男",
    hobby=["python","踢足球"]
)
print(person)
print("身份证:{id},姓名:{name},性别:{sex},爱好:{hobby}".format(
    id=person["id"],
    name=person["name"],
    sex=person["sex"],
    hobby="、".join(person["hobby"])
))
又或者:——**为不定长参数
print("身份证:{id},姓名:{name},性别:{sex},爱好:{hobby}".format(
    **person
))


#循环迭代数据
import math
from prettyprinter import cpprint
students=[
    dict(id="001",name="张三",sex="男",age=18,score=90),
    dict(id="002",name="李四",sex="女",age=19,score=100),
    dict(id="003",name="王五",sex="女",age=16,score=88),
    dict(id="004",name="赵六",sex="男",age=15,score=76),
]
cpprint(students)
print("-----------------------------------------------------")
for v in students:
    print("学号:{id},姓名:{name},性别:{sex},年龄:{age},总成绩:{score}".format(
        **v
    ))


#单独访问
print(students[1]["name"])
#单独赋值
students[3]["score"]=99
cpprint(students)
#添加一个
students.append(
   dict(id="005",name="孙七",age=20,sex="保密",score=92)
)
cpprint(students)

#删除一个
del students[2]
cpprint(students)

#定义一个接口
# _*_ coding: utf-8 _*_
import datetime #日期时间库
from prettyprinter import cpprint #导入颜色+格式化输出模块
"""
xml数据结构
json数据结构:网站、微信小程序、原生APP、pc桌面客户端、游戏
http api接口
标准接口:
code:状态码【0表示请求失败,1表示请求成功!】
data:具体数据【数组、对象】
msg:返回信息【错误信息,请求失败的信息,请求成功的信息】
"""
dt=datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
data=dict(
    code=1,
    data=[
        dict(id=1,title="文章标题",info="文章介绍",author="作者",createdAT=dt),
        dict(id=2,title="文章标题",info="文章介绍",author="作者",createdAT=dt),
        dict(id=3,title="文章标题",info="文章介绍",author="作者",createdAT=dt),
        dict(id=4,title="文章标题",info="文章介绍",author="作者",createdAT=dt),
        dict(id=5,title="文章标题",info="文章介绍",author="作者",createdAT=dt),
    ],
    msg="请求成功!"
)
cpprint(data)

# _*_ coding: utf-8 _*_
#! /usr/bin/env python
from prettyprinter import cpprint
#查看字典的内置方法和属性
person=dict(
    id="001",
    name="小明",
    score="None"
)
cpprint(dir(person))

#字典无序键值对应集合

#1.取出字典中所有键
print(person.keys())
#2.获取字典中所有值
print(list(person.values()))
#3.获取字典中键值元组列表[(k1,v1),(k2,v2),(k3,v3),(k4,v4)...]
print(list(person.items()))

#4.通过键获取值,对象.get("键")返回None不能完全表示键不存在,可能键的值本身就是None
print(person["id"])
print(person["name"])
print(person.get("age"))
print(person.get("sex"))
print(person.get("score"))

person2=dict(
    weight=180,
    height=70
)
#把字典person2更新到person中去
print(person.update(person2))
print(person)

#6.单个键对它值进行更新
person["score"]=88
person["age"]=18
person["sex"]="男"
#7.更新赋值,如果键不存在自动赋一个空值,对象.setdefault(键,[值])
person.setdefault("hobby")
person.setdefault("skin","白色")
print(person)
#8.删除键所对应的值
person.pop("id")
print(person)

del person["name"]
print(person)

#9.复制拷贝字典:复制值,不复制内存地址
person3=person.copy()
print(person,person3)
print(id(person),id(person3))
#10.清空字典
person.clear()
print(person)

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值