10.7Python

元组

menus = ('东坡肉', '佛跳墙', '狮子头', '酱大骨', '猪肘子')
print('修改前的菜单为:', end='')
for i in menus:
    print(i, end=" ")
print('')
menus_list = list(menus)
menus_list[3] = '杏仁豆腐'
menus = tuple(menus_list)
print('修改后的菜单为:', end='')
for i in menus:
    print(i, end=" ")

集合

existed_usernames = {'viv', 'apple', 'sky', 'apple'}
while 1:
    new_username = input('请输入用户名:')
    tem = {new_username}
    if tem.isdisjoint(existed_usernames):
        print('可以使用该用户名')
        existed_usernames.add(new_username)
        break
    else:
        print('已有用户名请重新输入')

print(existed_usernames)

随机十个三位数

import random

num = set()
while len(num) < 10:
    num_ = random.randint(100, 999)
    tem = {num_}
    num.add(num_)

print(num)

将字符串处理成字典

str = "k:1|k1:2|k2:3|k3:4"
str = str.split('|')
dict = {}
for i in str:
    tem = i.split(':')
    #print(tem)
    dict.setdefault(tem[0], tem[1])
print(dict)