Python学习的第十三天:第二周总结

本文详细介绍了Python中的数据结构,包括列表、元组、字符串和集合的创建、操作及特性。重点讲解了列表的索引、切片、排序、成员运算以及常用方法如append、extend、remove等。此外,还探讨了元组的不可变性、字符串的格式化输出和字符串方法,以及集合的哈希性质、操作方法如交并差运算。最后,简要提及了字典的构建和操作,并提到了JSON在数据交换中的作用。
摘要由CSDN通过智能技术生成

Python学习的第十三天

今天是Python学习第二周的末尾,我将照例整理这一周所学的知识点

random

import random
# random.randrange函数可以从范围内随机选取数据
num1 = random.randrange(1,11)
print(num1)	# 6

names = [1, 3, 5, 7, 8, 10, 9, 6]
# len函数用于返回列表中元素的个数
print(len(names))	# 8
# sample函数可以对列表元素进行无放回抽样
print(random.sample(names, k=5))	# [8, 3, 6, 10, 1]
# choices函数可以对列表元素进行有放回抽样(可以重复抽中),使用时必须使用k = n表示样本数量,输出为列表
print(random.choices(names, k=5))	# [8, 7, 10, 9, 8]
# choice函数可以从列表中随机选择一个元素
print(random.choice(names))	# 8
# shuffle函数可以实现列表元素的随机乱序,返回新列表
random.shuffle(names)
print(names)	# [10, 5, 6, 8, 9, 7, 3, 1]

列表

列表的初步了解
  1. 列表是一种容器型数据类型
  2. 列表是由一系元素按特定顺序构成的数据序列,可以保存多个数据,而且允许有重复的数据列表的构建
  3. 列表是一种可变数据类型
  4. 嵌套列表:列表内的元素可以是列表,适用于储存表格形式的数据
列表的构建
# 创建列表的方式一:字面量语法
list1 = ['apple', 'orange', 'pitaya', 'durian'] 
print(list1)	# ['apple','orange','pitaya','durian']

# 创建列表的方式二:构造器语法
list2 = list(range(1, 10)) 
print(list2)	# [1,2,3,4,5,6,7,8,9]

# 创建列表的方式三:生成式(推导式)语法
list3 = [i ** 2 for i in range(1, 10)]
print(list3)	# [1,4,9,16,25,36,49,64,81]
列表的相关运算
# 列表的重复运算
list4 = [1, 10, 100] * 5
print(list4)	# [1, 10, 100, 1, 10, 100, 1, 10, 100, 1, 10, 100, 1, 10, 100]

# 列表的成员运算
print(10 in list4)	# True
print(5 not in list4)	# True

# 列表的拼接
list5 = [1, 3, 5, 7]
list6 = [4, 4, 8]

# 遍历列表中的元素
for i in range(len(list5)):
    print(list1[i])	# 1	 3	5  7
for x in list5:
    print(x)	# # 1  3  5  7
    
# 列表的枚举,可以同时输出元素和其对应的序号
for i, x in enumerate(list5):
    print(i, x)	#0 1	1 3 	2 5		3 7
    
# 方法一,通过+进行拼接
list6 = list5 + list6
list6 += list5

# 方法二,通过函数extend()进行拼接
list6.extend(list5)
print(list6)	# [1, 3, 5, 7, 4, 4, 8, 1, 3, 5, 7, 1, 3, 5, 7]


# 列表的索引
# 索引包括正向索引(从0开始直到长度-1)和负向索引(从-1开始直到-长度)
print(list5[0], list[-1])	# 1 7

# 通过索引运算重新赋值
list5[-1] = 100
print(list5[len(list5)-1], list5[-1])	# 100 100

# 列表的切片
import random

nums = [random.randrange(1, 100) for _ in range(9)]
print(nums)	# [13, 7, 32, 92, 16, 72, 74, 85, 34]

# 默认步长为1
print(nums[2:])	# [32, 92, 16, 72, 74, 85, 34]

# 默认从0开始直到列表长度
print(nums[::])	# [13, 7, 32, 92, 16, 72, 74, 85, 34]

# 列表的倒转
print(nums[::-1])	# [34, 85, 74, 72, 16, 92, 32, 7, 13]
print(nums[1:3])	#[7, 32]

# 列表的步长为2
print(nums[2:7:2])	# [32, 16, 74]

# 只能获取未超出部分
print(nums[10:15])	# []
print(nums[6:10]	# [74, 85, 34]

# 步长为正数时应取开始边界大于结束边界
print(nums[5:1])	#[]

# 比较 ---> 实际工作中使用较少(可以忽略不计)
# 列表之间对应下标的元素进行比较
# 列表和值之间列表每个元素与值进行比较
# 只需比较第一个
# 字符串可以进行比较,大小按照首字母在字母表内顺序
list7 = list(range(1, 8, 2))
list8 = [0, 3, 5, 7, 9]

# 比较两个列表的元素是否意义对应相等
print(list5 == list7)	# True
print(list7 != list8)	# True

# 比较两个列表对应元素的大小
print(list7 < list8)	#False
列表的操作方法
# copy()拷贝列表,相当于完整的切片
# index()查找对应元素在列表内的索引(下标),超出范围后不会显示
items = ['banana', 'grape', 'apple', 'waxberry', 'pitaya', 'apple'] 

# index() ---> 查找元素在列表中的索引(下标)
if 'strawberry' in items:
    print(items.index('strawberry'))
if 'apple' in items:
    print(items.index('apple'))
if 'apple' in items[3:]:
    print(items.index('apple', 3))

# 添加元素
# append()在末尾追加元素
items.append('blueberry')

# insert()在指定位置插入元素
items.insert(1, 'watermelon')
print(items)

items = ['banana', 'grape', 'apple', 'waxberry', 'pitaya', 'apple']

# 删除元素
# pop()删除对应下标位置的元素,默认删除末位,可以返回被删除的元素
items.pop()
print(items.pop(4)# del 删除对象的引用,作用类似于pop,但不能返回被删除的元素
del items[0]

# 通过remove()从列表中删除指定元素,从左到右删除第一个,可以通过循环删除全部
while 'apple' in items:
    items.remove('apple')
print(items)
items = ['banana', 'grape', 'apple', 'waxberry', 'pitaya', 'apple']
    
# count() ---> 统计元素在列表中出现的次数
print(items.count('apple'))
print(items.count('strawberry'))

# clear()请空列表
items = ['banana', 'grape', 'apple', 'waxberry', 'pitaya', 'apple']
print(items.clear())

# reverse()对列表进行反转,直接修改原列表
items = ['banana', 'grape', 'apple', 'waxberry', 'pitaya', 'apple']
print(items.reverse())

# sort()对列表进行排序,字符串会安装字母表排序,默认从小打大(升序)排序,即reverse=False
# 对字符串类型的数字,按照首位的单个数字进行字符排序
items = ['banana', 'grape', 'apple', 'waxberry', 'pitaya', 'apple']
items.sort()
print(items)

# 当reverse=True时会从大到小(降序)排序。
items.sort(reverse = True)
print(items)	# ['waxberry', 'pitaya', 'grape', 'banana', 'apple', 'apple']

# 通过sort()中的key选择排序方式(例如根据字符串、整数类型进行排序),排序后元素的类型不变
nums = ['1', '10', '234', '2', '35', '100']
nums.sort(key=int)	# ['apple', 'apple', 'banana', 'grape', 'pitaya', 'waxberry']
print(nums)	# ['1', '2', '10', '35', '100', '234']

元组

元组的初步了解
  1. 元组是不可变类型,这就意味着元组类型的变量一旦定义,其中的元素不能再添加或删除,而且元素的值也不能进行修改。
  2. ()内有几个元素我们就称此为几元组
  3. ()表示空元组
  4. 如果元组中只有一个元素,需要加上一个逗号,否则其不是元组
  5. 不可变类型更适合多线程环境
  6. 不可变类型在创建时间和占用空间上面都优于对应的可变类型
元组的构建
# 定义一个三元组
t1 = (30, 10, 55)
print(t1)	#(30,10,55)

# 定义一个四元组
t2 = ('河北', 40, True, '兴趣爱好')
print(t2)	# ('河北','40','True','兴趣爱好')
元组的相关运算

# 查看变量的类型
print(type(t1), type(t2))    # <class 'tuple'> <class 'tuple'>

# 查看元组中元素的数量
print(len(t1), len(t2))      # 3 4

# 通过索引运算获取元组中的元素
print(t1[0], t1[-3])         # 30 30
print(t2[3], t2[-1])         # 兴趣爱好 兴趣爱好

# 循环遍历元组中的元素
for member in t2:
    print(member)

# 成员运算
print(100 in t1)    # False
print(40 in t2)     # True

# 拼接
t3 = t1 + t2
print(t3)           # (30, 10, 55, '河北', 40, True, '兴趣爱好')

# 切片
print(t3[::3])      # (30, '河北', '兴趣爱好')

# 比较运算
print(t1 == t3)    # False
print(t1 >= t3)    # False
print(t1 < (30, 11, 55))    # True
元组的应用场景
  1. 打包和解包操作
  2. 交换两个变量的值
  3. 让函数返回多个值

字符串

字符串的初步了解
  1. 由零个或多个字符组成的有限序列
  2. 单引号’'或双引号""可以构建字符串,以三个双引号或单引号开头的字符串可以折行,Python中还允许在\后面还可以跟一个八进制或者十六进制数来表示字符
  3. \代表转义
  4. 字符串前的r代表原始字符串,表示该字符串的所有符号都是其原始含义,没有转义字符
字符串的相关运算
# 字符串的拼接
s1 = 'hello' + ' ' + 'world'
print(s1)    # hello world

# 字符串的重复

s2 = '!' * 3
print(s2)    # !!!

s1 += s2     # s1 = s1 + s2
print(s1)    # hello world!!!
s1 *= 2      # s1 = s1 * 2
print(s1)    # hello world!!!hello world!!!

# 字符串的成员运算

s1 = 'hello, world'
print('wo' in s1)    # True
s2 = 'goodbye'
print(s2 in s1)      # False

# 字符串的索引运算

s = 'abc123456'
N = len(s)

# 获取第一个字符
print(s[0], s[-N])    # a a

# 获取最后一个字符
print(s[N-1], s[-1])  # 6 6

# 获取索引为2或-7的字符
print(s[2], s[-7])    # c c

# 获取索引为5和-4的字符
print(s[5], s[-4])    # 3 3

# 字符串的切片

s = 'abc123456'

# i=2, j=5, k=1(默认)的正向切片操作
print(s[2:5])       # c12

# 等同于字符串的遍历
print(s[:])         # abc123456

# 字符串的翻转
print(s[::-1])      # 654321cba

# 字符串的遍历
# 根据下标进行遍历
s1 = 'hello'
for index in range(len(s1)):
    print(s1[index])
    
# 直接遍历
s1 = 'hello'
for ch in s1:
    print(ch)
    
# 字符串的比较
s1 = 'a whole new world'
s2 = 'hello world'
print(s1 == s2, s1 < s2)      # False True

# Python中大小写代表不同的字符
print(s2 == 'hello world')    # True
print(s2 == 'Hello world')    # False
print(s2 != 'Hello world')    # True

# 按照字符的编码大小进行比较(不清楚对应编码可以通过ord()获得)
s3 = '你好'
prit(ord('你'), ord('好'))               # 20320 22909
s4 = '再见吧'
print(ord('再'), ord('见'), ord('吧'))    # 20877 35265 21543
print(s3 > s4, s3 <= s4)      # False  True

s1 = 'hello world'
s2 = 'hello world'
s3 = s2

# 比较字符串的内容
print(s1 == s2, s2 == s3)    # True True

# 比较字符串的内存地址
print(s1 is s2, s2 is s3)    # False True
字符串的相关操作
# 字符串的大小写转换

s1 = 'hello, world!'

# 使用capitalize方法获得字符串首字母大写后的字符串
print(s1.capitalize())   # Hello, world!

# 使用title方法获得字符串每个单词首字母大写后的字符串
print(s1.title())        # Hello, World!

# 使用upper方法获得字符串大写后的字符串
print(s1.upper())        # HELLO, WORLD!

s2 = 'GOODBYE'

# 使用lower方法获得字符串小写后的字符串
print(s2.lower())        # goodbye

# 字符串的查找

s = 'hello, world!'

# find方法从字符串中查找另一个字符串所在的位置
# 找到了返回字符串中另一个字符串首字符的索引
print(s.find('or'))        # 8

# 找不到返回-1
print(s.find('shit'))      # -1

# index方法与find方法类似
# 找到了返回字符串中另一个字符串首字符的索引
print(s.index('or'))       # 8

# 找不到引发异常
print(s.index('shit'))     # ValueError: substring not found

s = 'hello good world!'

# 从前向后查找字符o出现的位置(相当于第一次出现)
print(s.find('o'))       # 4

# 从索引为5的位置开始查找字符o出现的位置
print(s.find('o', 5))    # 7

# 从后向前查找字符o出现的位置(相当于最后一次出现)
print(s.rfind('o'))      # 12

# 字符串性质判断

s1 = 'hello, world!'

# startwith方法检查字符串是否以指定的字符串开头返回布尔值
print(s1.startswith('He'))  # True
# Falseprint(s1.startswith('hel'))   # True

# endswith方法检查字符串是否以指定的字符串结尾返回布尔值
print(s1.endswith('!'))     # True

s2 = 'abc123456'

# isdigit方法检查字符串是否由数字构成返回布尔值
print(s2.isdigit())    # False

# isalpha方法检查字符串是否以字母构成返回布尔值
print(s2.isalpha())    # False

# isalnum方法检查字符串是否以数字和字母构成返回布尔值
print(s2.isalnum())    # True

# 字符串的格式化输出
s = 'hello, world'

# center方法以宽度20将字符串居中并在两侧填充*
print(s.center(20, '*'))  # ****hello, world****

# rjust方法以宽度20将字符串右对齐并在左侧填充空格
print(s.rjust(20))        #         hello, world

# ljust方法以宽度20将字符串左对齐并在右侧填充~
print(s.ljust(20, '~'))   # hello, world~~~~~~~~

# 拆分字符串

content = 'You go your way, I will go mine.'
content2 = content.replace(',', '').replace('.', '')

# 用空格拆分字符串得到一个列表

words = content2.split()
print(words, len(words))	# ['You', 'go', 'your', 'way', 'I', 'will', 'go', 'mine'] 8

for word in words:
    print(word)	# You  go  your  way  I  will  go  mine

# 用空格拆分字符串,最多允许拆分3次
words = content2.split(' ', maxsplit=3)
print(words, len(words))	# ['You', 'go', 'your', 'way I will go mine'] 4

# 从右向左进行字符串拆分,做多允许拆分3次
words = content2.rsplit(' ', maxsplit=3)
print(words, len(words))	# ['You go your way I', 'will', 'go', 'mine'] 4

# 用逗号拆分字符串
items = content.split(',')
for item in items:
    print(item)	# You go your way  I will go mine.

变量值占位符格式化结果说明
3.1415926{:.2f}'3.14'保留小数点后两位
3.1415926{:+.2f}'+3.14'带符号保留小数点后两位
-1{:+.2f}'-1.00'带符号保留小数点后两位
3.1415926{:.0f}'3'不带小数
123{:0>10d}0000000123左边补0,补够10位
123{:x<10d}123xxxxxxx右边补x ,补够10位
123{:>10d}' 123'左边补空格,补够10位
123{:<10d}'123 '右边补空格,补够10位
123456789{:,}'123,456,789'逗号分隔格式
0.123{:.2%}'12.30%'百分比格式
123456789{:.2e}'1.23e+08'科学计数法格式

集合

集合的初步了解
哈希
1. 如果一个对象无法计算哈希码,就不能放入集合中,可变容器(列表、集合、字典)都不能放入集合中
2. 集合底层使用了一种高效的存储方式:哈希存储(散列存储),因此集合在元素查找时效率远高于列表,不依赖问题的规模,是一种常量级时间复杂度的存储方案
3. 列表不是可以哈希的类型
4. 列表元组字符串的存储方式为顺序存储,优点为可以实现随机存取,缺点为需要判断元素是否存在,查找元素的效率十分低下
5. 哈希冲突会使效率降低
6. 百度网盘极速秒传原理:没有真正上传,其服务器已经拥有该文件,通过本地计算哈希码,上传哈希码后经过对比,返回数据:该用户已经拥有此文件并更改文件名
集合的性质
1. 互异性,集合内没有重复的元素
2. 无序性,集合内元素的顺序由哈希码决定,每个元素的地位都是相同的,元素之间是无序的,因此不能支持索引运算
3. 确定性,一个元素,只有属于和不属于集合两种情况,没有其他模棱两可的情况
4. 集合的类型为set
5. 空集合为set()
# 输入的字符串被随机排列,且删除了重复元素
set5 = set('hello')
print(set5)	# {'l', 'o', 'h', 'e'}

# bool值同样满足集合的互异性
set2 = {True, False, True, True, False}
print(set2)	# {False, True}

# 集合可以储存元组
set3 = {(1, 2, 3), (4, 5, 6)}
print(set3)	{(1, 2, 3), (4, 5, 6)}

# 列表可以储存集合
list4 = [set5, set2]
print(list4)	# [{'hello'}, {False, True}]
集合的构建
#创建集合的字面量语法(重复元素不会出现在集合中)
set1 = {1, 2, 3, 3, 3, 2}
print(set1)         # {1, 2, 3}
print(len(set1))    # 3

# 创建集合的构造器语法(后面会讲到什么是构造器)
set2 = set('hello')
print(set2)         # {'h', 'l', 'o', 'e'}

# 将列表转换成集合(可以去掉列表中的重复元素)
set3 = set([1, 2, 3, 3, 2, 1])
print(set3)         # {1, 2, 3}

# 创建集合的生成式语法(将列表生成式的[]换成{})
set4 = {num for num in range(1, 20) if num % 3 == 0 or num % 5 == 0}
print(set4)         # {3, 5, 6, 9, 10, 12, 15, 18}
集合的相关运算
# 集合元素的循环遍历
set4 = {num for num in range(1, 20) if num % 3 == 0 or num % 5 == 0}
for elem in set4:
	print(elem)	# 3  5  6  9  10  12  15  18

# 集合的成员运算
set1 = {1, 2, 3, 4, 5}
set2 = {2, 4, 6, 8}
print(1 in set1)	# True
print(1 not in set1)	# False

# 集合的交并差运算
set1 = {1, 2, 3, 4, 5}
set2 = {2, 4, 6, 8}

# 交集
# 符号方法
print(set1 & set2)	# {2, 4}

# 内置函数方法
print(set1.intersection(set2))	# {2, 4}

# 并集
# 符号方法
print(set1 | set2)	# {1, 2, 3, 4, 5, 6, 8}

# 内置函数方法
print(set1.union(set2))	# {1, 2, 3, 4, 5, 6, 8}

# 差集
# 差集不满足交换律
# 符号方法
print(set1 - set2)	# {1, 3, 5}

# 内置函数方法
print(set1.difference(set2))	# {1, 3, 5}

# 符号方法
print(set2 - set1)	# {8, 6}

# 内置函数方法
print(set2.difference(set1))	# {8, 6}

# 对称差
# 对称差满足交换律
# 符号方法
print(set1 ^ set2)	# {1, 3, 5, 6, 8}

# 定义方法
print((set1 | set2) - (set1 & set2))	# {1, 3, 5, 6, 8}

# 内置函数方法
print(set1.symmetric_difference(set2))	# {1, 3, 5, 6, 8}

set3 = {1, 2, 3, 4, 5, 6, 7, 8, 9}

# 判断真子集
# 符号方法
print(set1 < set3)	# True

# 内置函数方法
print(set1.issubset(set3))	# True

# 判断子集
print(set1 <= set3)	# True

# 判断超集
# 符号方法
print(set3 > set2)	# True

# 内置函数方法
print(set3.issuperset(set2))	# True
集合的相关操作
set1 = {'apple', 'banana', 'pitaya', 'apple'}

# 添加元素
# 由于集合的无序性,不可以使用追加(append)和插入(insert),加入后元素的位置不确定
set1.add('grape')
set1.add('durian')
print(set1)	# {'banana', 'pitaya', 'durian', 'grape', 'apple'}

# 删除元素
# 删除指定元素
set1.discard('pitaya')

# 随机删除元素,但会返回删除的值
print(set1.pop())	# banana
print(set1.pop())	# durian
print(set1)	# {'grape', 'apple'}

# 清空元素
set1.clear()
print(set1)	# set()

# 集合、元组和列表之间可以相互转换
nums = [1, 1, 10, 10, 10, 5, 3, 9, 9]

# 列表转换为集合
set2 = set(nums)
print(set2)	# {1, 3, 5, 9, 10}

# 集合转换为列表
list3 = list(set2)
print(list3)	# [1, 3, 5, 9, 10]

# 列表转换为元组
tuple4 = tuple(list3)
print(tuple4)	# (1, 3, 5, 9, 10)

字典

字典的初步了解
  1. 以键值对储存数据
  2. 数据的展现形式较好
  3. 可以通过键精准取值
  4. :前的键必须是不可变类型
  5. 键可以使用字符串、元组、数值,但优先使用字符串
  6. {}为空字典
  7. 字典内可以嵌套字典,新字典的键是旧字典的值
字典的构建
# 字典的字面量语法
student = {
    'name': '小明',
    'sex': 'True',
    'birthday': '1999.10.1'
}
print(student)	# {'name': '小明', 'sex': True, 'birthday': '1999.10.1'}

# 字典的构造器语法
student = dict(name='小明', sex=True, birthday='1999.10.1')
print(student)	# {'name': '小明', 'sex': True, 'birthday': '1999.10.1'}

# 通过Python内置函数zip压缩两个序列并创建字典
items1 = dict(zip('ABCDE', '12345'))
print(items1)	# {'A': '1', 'B': '2', 'C': '3', 'D': '4', 'E': '5'}
items2 = dict(zip('ABCDE', range(1, 10)))
print(items2)	# {'A': 1, 'B': 2, 'C': 3, 'D': 4, 'E': 5}

# 用字典生成式语法创建字典
items3 = {x: x ** 3 for x in range(1, 6)}
print(items3)	# {1: 1, 2: 8, 3: 27, 4: 64, 5: 125}
字典的运算
student = {
    'name': '小明',
    'sex': 'True',
    'birthday': '1999.10.1'
}

# 字典的成员运算
print('name' in student)	# True
print('birthday' not in student)	# False
print('age' in student)	# False
print(len(student))	# 3

# 字典的索引运算
# 索引内容不存将会报错
print(student['name'])	# 小明

# 可以通过索引运算对字典进行修改
# 如果赋值字典存在的键则为更新原值,不存在则为添加新的键值对
student['name'] = '小红'
student['sex'] = False
student['adresss'] = '四川成都'
print(student)	# {'name': '小红', 'sex': False, 'birthday': '1999.10.1', 'adresss': '四川成都'}

student['爱好'] = {'动漫': '罗小黑战记', '游戏': '空洞骑士'}
student['厌恶'] = {'习惯': '吃饭发声', '动物': '蚊子'}
print(student)	# {'name': '小红', 'sex': False, 'birthday': '1999.10.1', 'adresss': '四川成都', '爱好': {'动漫': '罗小黑战记', '游戏': '空洞骑士'}, '厌恶': {'习惯': '吃饭发声', '动物': '蚊子'}}
字典的操作方法
# 字典的操作方法

dict1 = {'a': 1, 'b': 3, 'c': 5, 'd': {'e': 7, 'f': 9}}

# 通过get()方法获取对应值
# 使用get函数通过key获取value时,如果key不存在,不会发生KeyError错误,而是得到一个None(空值)或者是你指定的默认值
print(dict1.get('a'))	# 1
print(dict1.get('d'))	# {'e': 7, 'f': 9}

# 获取字典中所有的键
print(dict1.keys())	# dict_keys(['a', 'b', 'c', 'd'])

# 获取字典中所有的值
print(dict1.values())	# dict_values([1, 3, 5, {'e': 7, 'f': 9}])

# 获取字典中所有的键值对
print(dict1.items())	# dict_items([('a', 1), ('b', 3), ('c', 5), ('d', {'e': 7, 'f': 9})])

# 对字典中所有的键值对进行循环遍历
for key, value in dict1.items():
    print(key, '--->', value)	# a ---> 1	b ---> 3	c ---> 5	d ---> {'e': 7, 'f': 9}
    
# 使用pop方法通过键删除对应的键值对并返回该值
stu1 = dict1.pop('d')
print(stu1)	# {'e': 7, 'f': 9}

print(len(dict1))	# 3

stu2 = dict1.pop('', {})
print(stu2)	# {}

# 使用popitem方法删除字典中最后一组键值对并返回对应的二元组
# 如果字典中没有元素,调用该方法将引发KeyError异常
key, value = dict1.popitem()
print(key, value)	# c 5

# 通过del删除不会返回数据
del dict1['a']
print(dict1)	# {'b': 3}

dict2 = {'A': 100, 'B': 200, 'C': 300}
dict3 = {'D': 400, 'E': 500, 'A': 600}

# 更新(元素的合并或更新)
dict2.update(dict3)
print(dict2)	# {'A': 600, 'B': 200, 'C': 300, 'D': 400, 'E': 500}

# 查找对应值,如果字典中包含有给定键,则返回该键对应的值,否则返回为该键设置的值。
print(dict2.setdefault('C'))	# 300
print(dict2.setdefault('K', 10000))	# 10000
print(dict2)	{'A': 600, 'B': 200, 'C': 300, 'D': 400, 'E': 500, 'K': 10000}

# 清空所有
dict2.clear()
print(dict2)	# {}	
json
  1. 两个异构的系统之间交换数据最好的选择是交换纯文本(可以屏蔽系统和编程语言的差异)

  2. 纯文本应该是结构化或半结构化的纯文本(有一定的格式)
    XML —> eXtensible Markup Language —> 可扩展标记语言
    JSON —> JavaScript Object Notation —> 大多数网站和数据接口服务使用的数据格式
    YAML —> Yet Another Markup Language
  3. 如何将JSON格式的字符串转成Python程序中的字典?
    —> json 模块 —> loads 函数

    loads函数可以将JSON格式的数据转成Python中字典

细碎知识

  1. 评价算法好坏的标准:渐近时间复杂度和渐近空间复杂度

  2. 渐近时间复杂度通过O标记

  3. os代表操作系统

    os.windows(‘clear’) 用于清除输出

  4. time.sleep() 用于让程序休眠

  5. 操作系统:Windows、iOS、Android、macOS、Linux、Unix

  6. 编程语言:Python、Java、PHP、Go、C++

  7. URL —> Universal Resource Locator —> 统一资源定位符

  8. 协议 —> 通信双方需要遵守的会话的规则。

  9. HTTP / HTTPS —> 通过URL访问网络资源的协议 —> Hyper-Text Transfer Protocol(超文本传输协议)

  10. 请求(request) - 响应(response)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

踏墟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值