生成随机数
工具库:random
生成随机整数:random.randint(0, 99999)
生成随机大写英文字母:random.choice(string.ascii_uppercase)
例子
'''
3. 写代码实现,车牌的区域划分。如 car_list = ['鲁A32444', '沪B12333', '京B8989M', '京C49678', '黑C46555', '晋B25041', '沪C34567']a.自动生成一个含有100个元素的car_listb.根据生成的car_list获取各省车牌数量,例如:info = {"沪":2,"京":2 ...}
'''
import random
import stringdef get_car_list(n):car_list=[]car_num = {}plist = []province = ['鲁','沪','京','黑','晋','粤','浙']for i in range(int(n)):# 省份简称short = province[random.randint(0,len(province)-1)]# 大写英文首字母letter = random.choice(string.ascii_uppercase)# 五位随机数字num = str(random.randint(0, 99999))if len(num) < 5:n = 5 - len(num)for i in range(n):num = '0' + str(num)# 车牌号码car = short+letter+num# 添加车牌到列表car_list.append(car)for i in range(len(car_list)):# 生成的省份简称添加到列表plist.append(car_list[i][0])# 简称对应的数量for i in range(len(province)):car_num[province[i]] = plist.count(province[i])print(car_list)print(car_num)get_car_list(100)