python练习2020/08/01

9. 删除无重复元组中给定的元素,并生成一个新元组
def dele_item(s,item):
    s_list=list(s)
    s_list2=s_list[:]
    for i in s_list:
        if i==item:
            s_list2.remove(i)
            
    return tuple(s_list2)

print('生成的新元组:',dele_item((1,2,3,4),3))

12、求一个n*n矩阵对角线元素之和
n_lst = [
    [1,2,3],
    [4,5,6],
    [7,8,9],
]

#00 01 02
#10 11 12
#20 21 22
result=0
for i in range(len(n_lst)):
    for j in range(len(n_lst[i])):
        if i==j:#正对角线
            result+=n_lst[i][j]
        if i+j==2:#反对角线
            result+=n_lst[i][j]
print('对角线的和为:',result)

13. 输入一个数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组
def max_min_nuber(arr):
    max_item=arr[0]
    min_item=arr[0]
    for i in arr:
        if i>max_item:
            max_item=i
        if i<min_item:
            min_item=i
    arr[0]=max_item
    arr[-1]=min_item
    return arr

print(max_min_nuber([1,24,5,69,4,65]))

14. 平衡点,一个数组,有一个数字左边所有的数字加起来的总和等于这个数右边所有数字的总和,请输出这个
数以及坐标
def balance_point(arr):
    num_dict={}
    for i in range(len(arr)):
        if i>=1:
            if sum(arr[:i])==sum(arr[i+1:]):
                num_dict[arr[i]]=i
    return num_dict

print(balance_point([1,2,3,4,5,1]))

16.输入一个数组,实现一个函数,让所有奇数都在偶数前面
def sort(s):
    result=[]
    for i in range(len(s)):
        if s[i]%2 !=0:
            result.insert(0,s[i])
        else:
            result.append(s[i])
    return result

print(sort([1,2,3,4,5,6]))

17.lista=['a','abc','d','abc','fgi','abf'],寻找列表中出现次数最多的第一个字母,出现了几次

lista=['a','abc','d','abc','fgi','abf']
result={}
first_letter=[]
for i  in lista:
    first_letter.append(i[0])

for letter in first_letter:
    result[letter]=first_letter.count(letter)
print(result)

for k,v in result.items():
    if v==max(result.values()):
        print('出现次数最多的字母是%s,出现次数是%s' %(k,v))


18.请输入星期几的前两个字母来判断一下是星期几,如果第一个字母一样,则继续判断第二个字母
weeks = ["monday","tuesday","wednesday","thursday","friday","saturday"]
while 1:
    
    first_letter = input("请输入第一个字母: ")
    if first_letter == "q":
        break
    first_word_lst = [word[0] for word in weeks] #推导列表取出第一个字母

#当首字母大于1时,输入第二个字母做比较
    if first_word_lst.count(first_letter) > 1:
        
        second_letter = input("请输入第二个字母: ")
        for word in weeks:
#当首字母等于输入的第一个字母,第二个字母等于输入的第二个字母
            if word[0] == first_letter and word[1] == second_letter:
                print(word)
    else:
        for word in weeks:
            if word[0] == first_letter:
                print(word)

#2
weeks = ["monday","tuesday","wednesday","thursday","friday","saturday","sunday"]
first_word=[]
for day in weeks:
    first_word.append(day[0])
print(first_word)
    
while True:
    first_letter=input('请输入第一个字母:')
    if first_letter=='q':
        break
    if first_word.count(first_letter) >1:
        second_letter=input('请输入第二个字母:')
        for word in weeks:
            if word[0]==first_letter and word[1]==second_letter:
                print(word)
    else:
        for word in weeks:
            if word[0]==first_letter:
                print(word)











©️2020 CSDN 皮肤主题: 1024 设计师:上身试试 返回首页