python第一次上机课作业(p61)

3-1:

num=[6 for i in range(10)]
print(num)

3-2:

a_list=[]
b_list=[]
for value in range(1,21):
    a_list.append(value)
b_list = [a_list[i:i+2] for i in range(4,len(a_list)+1,6)]
b_list = [i for j in b_list for i in j]
print(b_list)

 3-3:

num=[1,2,3,4,5,6,7,8,9]
for i in range(len(num)):
    if num[i]%2==0 :
        num[i]=num[i]*num[i]
print(num)

3-4:

num=[11,22,33,44,55,66,77,88,99,90]
list_a=[]
list_b=[]
for i in num:
    if(i>66):
        list_a.append(i)
    else :
        list_b.append(i)
tuple_a=tuple(list_a)
tuple_b=tuple(list_b)
k={'k1':tuple_a,'k2':tuple_b}
print("键k1内的元素 :\n \t{}".format(k['k1']))
print("键k2内的元素 :\n \t{}".format(k['k2']))

3-5:

k={'1':'1111','2':'2222','3':'33333','4':'4444','5':'55555'}
x=input("请输入一个数字:")
if x in k :
    print('该键对应的值为: {}'.format(k[x]))
else:
    print("您输入的键不存在!")

3-6:

A={1,2,3,5,6,43,6,4,52}
B={2,4,53,4,243,42,4,1,32,3,4,45}
print("集合A,B的交集为:{} \n并集为:{} \n差集为:{}".format(A&B,A|B,A-B))

3-7:  

import math
import numpy as np
list_a = [1, 2, 3, 1, 3, 4, 5, 5, 6]
list_a = np.unique(list_a)
print(list_a)

3-8:

str1 = input("请输入要计算的字符串:")
ans = {}
for s in str1:
    num = str1.count(s)
    ans[s] = num
print(ans)

3-9:

a = "aAsmr3idd4bgs7Dlsf9eAF"
print("原字符串为:{}".format(a))
# (1)将a字符串的数字取出,并输出成一个新的字符串。
stra = ""
for i in a:
    if i<='9' and i>='0' :
        stra=stra+i
print("由数字组成的新字符串为:{} ".format(stra))
# (2)去除a字符串多次出现的字母,仅留最先出现的一个。例'abcabb',经过去除后,输出 'abc'。
a_list = list(a)
set_list = list(set(a_list))
set_list.sort(key=a_list.index)
print('去除重复后的自字符串为:', ''.join(set_list))
# (3)将a字符串反转并输出。例:'abc'的反转是'cba'。
print('反转后的字符串为:', a[::-1])
# (4)输出a字符串出现频率最高的字母。
p = ([(x, a.count(x)) for x in set(a)])
p.sort(key=lambda k: k[1], reverse=True)
print('出现频率最高的字母为:', p[0][0])

你可能感兴趣的:(python,python,开发语言,numpy)