Day06字典&元组&集合的简单介绍

字典dict:
1.字典里面的元素是一个键值对：key-value
2.key有三个特点：a.唯一b.不可变类型c.无序（没有下标）
# 1.创建字典
d = {"name":"张三","age":22,"height":184}
# d = {0:1,1:2}
print(d)
# 2.查：通过key来获取value
print(d['name'])
print(d.get('name'))
# 3.遍历字典
d = {"name":"蔡徐坤","age":22,"height":184}
for k in d:
print(k)  # 得到key
for k in d.keys():
print(k)  # 得到key
for v in d.values():
print(v)  # values
for k,v in d.items():
print(k,v)  # key values
# 4.合并
d1 = {1:2}
d2 = {3:4}
d1.update(d2)
print(d1)  # {1: 2, 3: 4}



# 1. 创建元组
t = ()  # 空元组
# t = (1,) # 一个元素的元组
t = (1,2,3,'hello',True)
print(t)
# 2.合并
print((1,2)+(3,4)) # (1, 2, 3, 4)
# 3.重复
print((1,2,3) * 3) # (1, 2, 3, 1, 2, 3, 1, 2, 3)
# 4.遍历
t = (11,22,33)
for n in t:
print(n)  # 元素
for i in range(len(t)):
print(i)  # 下标
for i,n in enumerate(t):
print(i,n)

# 1.创建集合
s = {1,2,2,2,3,4}
print(s)  # {1, 2, 3, 4}
s = set()
print(s)  # set()
s = {'a','b','c'}
print(s)
# 2.列表去重
l = [1,2,3,5,6,6,2,2,6,3]
l2 = list(set(l))
print(l2)
# 3.遍历
s = {11,22,33}
for n in s:
print(n)

nums = [9,7,6,8,5,4,3,2,1]
for i in range(len(nums)-1):
for j in range(len(nums)-i-1):
if nums[j] > nums[j+1]:
nums[j],nums[j+1] = nums[j+1],nums[j]
print(nums)

nums = [9,7,6,8,5,4,3,2,1]
for i in range(len(nums)-1):
min_index = i
for j in range(i, len(nums)):
if nums[j] < nums[min_index]:
min_index = j
#用最小数和第i个元素交换
nums[i],nums[min_index] = nums[min_index],nums[i]
print(nums)

01-13
07-25 20
10-23 2970
07-04 19
01-17 81万+