python 复习实验3(选择循环结构)

题目1
a构造字典{‘1’:1,‘2’:4,‘3’:9,‘4’:16…‘100’:10000}
b 修改字典
如果关键字为3整除,其对应值加1
如果关键字为被3除余数1,其对应值减少1
c 构造字典{1:2,3:4,5:6…99:100} 并且和上面的字典合并为新的字典

代码:

#构造字典
x={}
for i in range(1,101):
    x[str(i)]=i*i #对应的键是字符型


#修改字典
for i in x.keys():    #x.key()表示字典中所有键的集合
    a=int(i)
    if a%3==0:
        x[i]=x[i]+1   #x[i]可以直接对键操作
    if  a%3==1:
        x[i]=x[i]-1
print(x)    

#字典合并
y={}
for i in range(1,100,2):
    y[i]=i+1     #对应的键是整型
print(y)

x.update(y)      #x.update(y)     y字典对向x中有的键,进行值更新;没有的键值对,会添加到x中
print(x)

运行结果为:
{‘1’: 0, ‘2’: 4, ‘3’: 10, ‘4’: 15, ‘5’: 25, ‘6’: 37, ‘7’: 48, ‘8’: 64, ‘9’: 82, ‘10’: 99, ‘11’: 121, ‘12’: 145, ‘13’: 168, ‘14’: 196, ‘15’: 226, ‘16’: 255, ‘17’: 289, ‘18’: 325, ‘19’: 360, ‘20’: 400, ‘21’: 442, ‘22’: 483, ‘23’: 529, ‘24’: 577, ‘25’: 624, ‘26’: 676, ‘27’: 730, ‘28’: 783, ‘29’: 841, ‘30’: 901, ‘31’: 960, ‘32’: 1024, ‘33’: 1090, ‘34’: 1155, ‘35’: 1225, ‘36’: 1297, ‘37’: 1368, ‘38’: 1444, ‘39’: 1522, ‘40’: 1599, ‘41’: 1681, ‘42’: 1765, ‘43’: 1848, ‘44’: 1936, ‘45’: 2026, ‘46’: 2115, ‘47’: 2209, ‘48’: 2305, ‘49’: 2400, ‘50’: 2500, ‘51’: 2602, ‘52’: 2703, ‘53’: 2809, ‘54’: 2917, ‘55’: 3024, ‘56’: 3136, ‘57’: 3250, ‘58’: 3363, ‘59’: 3481, ‘60’: 3601, ‘61’: 3720, ‘62’: 3844, ‘63’: 3970, ‘64’: 4095, ‘65’: 4225, ‘66’: 4357, ‘67’: 4488, ‘68’: 4624, ‘69’: 4762, ‘70’: 4899, ‘71’: 5041, ‘72’: 5185, ‘73’: 5328, ‘74’: 5476, ‘75’: 5626, ‘76’: 5775, ‘77’: 5929, ‘78’: 6085, ‘79’: 6240, ‘80’: 6400, ‘81’: 6562, ‘82’: 6723, ‘83’: 6889, ‘84’: 7057, ‘85’: 7224, ‘86’: 7396, ‘87’: 7570, ‘88’: 7743, ‘89’: 7921, ‘90’: 8101, ‘91’: 8280, ‘92’: 8464, ‘93’: 8650, ‘94’: 8835, ‘95’: 9025, ‘96’: 9217, ‘97’: 9408, ‘98’: 9604, ‘99’: 9802, ‘100’: 9999}
{1: 2, 3: 4, 5: 6, 7: 8, 9: 10, 11: 12, 13: 14, 15: 16, 17: 18, 19: 20, 21: 22, 23: 24, 25: 26, 27: 28, 29: 30, 31: 32, 33: 34, 35: 36, 37: 38, 39: 40, 41: 42, 43: 44, 45: 46, 47: 48, 49: 50, 51: 52, 53: 54, 55: 56, 57: 58, 59: 60, 61: 62, 63: 64, 65: 66, 67: 68, 69: 70, 71: 72, 73: 74, 75: 76, 77: 78, 79: 80, 81: 82, 83: 84, 85: 86, 87: 88, 89: 90, 91: 92, 93: 94, 95: 96, 97: 98, 99: 100}
{‘1’: 0, ‘2’: 4, ‘3’: 10, ‘4’: 15, ‘5’: 25, ‘6’: 37, ‘7’: 48, ‘8’: 64, ‘9’: 82, ‘10’: 99, ‘11’: 121, ‘12’: 145, ‘13’: 168, ‘14’: 196, ‘15’: 226, ‘16’: 255, ‘17’: 289, ‘18’: 325, ‘19’: 360, ‘20’: 400, ‘21’: 442, ‘22’: 483, ‘23’: 529, ‘24’: 577, ‘25’: 624, ‘26’: 676, ‘27’: 730, ‘28’: 783, ‘29’: 841, ‘30’: 901, ‘31’: 960, ‘32’: 1024, ‘33’: 1090, ‘34’: 1155, ‘35’: 1225, ‘36’: 1297, ‘37’: 1368, ‘38’: 1444, ‘39’: 1522, ‘40’: 1599, ‘41’: 1681, ‘42’: 1765, ‘43’: 1848, ‘44’: 1936, ‘45’: 2026, ‘46’: 2115, ‘47’: 2209, ‘48’: 2305, ‘49’: 2400, ‘50’: 2500, ‘51’: 2602, ‘52’: 2703, ‘53’: 2809, ‘54’: 2917, ‘55’: 3024, ‘56’: 3136, ‘57’: 3250, ‘58’: 3363, ‘59’: 3481, ‘60’: 3601, ‘61’: 3720, ‘62’: 3844, ‘63’: 3970, ‘64’: 4095, ‘65’: 4225, ‘66’: 4357, ‘67’: 4488, ‘68’: 4624, ‘69’: 4762, ‘70’: 4899, ‘71’: 5041, ‘72’: 5185, ‘73’: 5328, ‘74’: 5476, ‘75’: 5626, ‘76’: 5775, ‘77’: 5929, ‘78’: 6085, ‘79’: 6240, ‘80’: 6400, ‘81’: 6562, ‘82’: 6723, ‘83’: 6889, ‘84’: 7057, ‘85’: 7224, ‘86’: 7396, ‘87’: 7570, ‘88’: 7743, ‘89’: 7921, ‘90’: 8101, ‘91’: 8280, ‘92’: 8464, ‘93’: 8650, ‘94’: 8835, ‘95’: 9025, ‘96’: 9217, ‘97’: 9408, ‘98’: 9604, ‘99’: 9802, ‘100’: 9999, 1: 2, 3: 4, 5: 6, 7: 8, 9: 10, 11: 12, 13: 14, 15: 16, 17: 18, 19: 20, 21: 22, 23: 24, 25: 26, 27: 28, 29: 30, 31: 32, 33: 34, 35: 36, 37: 38, 39: 40, 41: 42, 43: 44, 45: 46, 47: 48, 49: 50, 51: 52, 53: 54, 55: 56, 57: 58, 59: 60, 61: 62, 63: 64, 65: 66, 67: 68, 69: 70, 71: 72, 73: 74, 75: 76, 77: 78, 79: 80, 81: 82, 83: 84, 85: 86, 87: 88, 89: 90, 91: 92, 93: 94, 95: 96, 97: 98, 99: 100}

题目2
随机产生1000个字符的字符串(小写字母和标点符号和数字组成),
然后统计这个字符串出现的字母和数字和标点符号的次数

提示:string.punctuation+string.ascii_lowercase+string.digits
每个字母或者符号出现的次数形成一个字典,最后产生这个字典
提高:讲字典按照关键字ASCII码的顺序从小到大打印

import random
import string
s=string.punctuation+string.ascii_lowercase+string.digits
x=[random.choice(s) for i in range(1000)]  #random.choice(s) 在s中选择随机数
y=set(x)
dic={}
for i in y:   #去重之后的元素
    dic[i]=x.count(i)
print(dic)

li=list(dic.keys())  #将键放入列表
li.sort()            #排序

for i in li:
    print(i,dic[i])
    

题目3
a.产生100个随机(1到100以内)的随机整数的集合,删除其奇数。
b.产生20个随机数(1到200内)的列表,对奇数序号的元素升序排列,
对偶数序号的元素降序排列,形成新的列表

代码:a

x=[random.randint(1,101) for i in range(100)]
y=set(x)
print(x) #x是列表
print(y) #y是集合
z=list(y)
for i in z:  #不能直接用for i in y,集合无法对自身迭代,要转换为列表
    if i%2==1:
        y.remove(i) 
print(y)

list与array
list是列表,里面的数据类型可以是不一样的,array是numpy库里的函数,里面的数据类型要一致。

import numpy as np
x=[1.2,5,6,8,7.9,6,5]
arr1=np.array(x)  l   #array 类型是数组,里面的数据类型都是相同的
arr1

结果为:
array([1.2, 5. , 6. , 8. , 7.9, 6. , 5. ])

b

x=[random.randint(1,201) for i in range(20)]
print(x)
a=x[1::2]
a.sort()
b=x[::2]
b.sort(reverse=True)
x[1::2]=a            #将排序后值赋给列表对应的位置
x[::2]=b
print(x)

结果为:
[107, 23, 80, 82, 154, 180, 150, 180, 46, 68, 49, 84, 143, 132, 39, 177, 147, 33, 199, 21]
[199, 21, 154, 23, 150, 33, 147, 68, 143, 82, 107, 84, 80, 132, 49, 177, 46, 180, 39, 180]
这里一定要有这一步,要不然结果还是和原来的一样
x[1::2]=a #将排序后值赋给列表对应的位置
x[::2]=b

你可能感兴趣的:(python笔记)