day7 练习

1.已知一个数字列表,求列表中心元素。

list1=list(input('请输入一串数字:'))
if len(list1)%2==0:
    number=len(list1)//2
    print(*list1[number-1:number+1])
else:

    number=len(list1)//2
    print(list1[number])

2.已知一个数字列表,求所有元素和。

list2=list(input('请输入一串数字:'))
print(type(list2))
count=0
for index in list2:
    count+=int(index)
print(count)

3.已知一个数字列表,输出所有奇数下标元素。

list3=list(input('请输入一串数字:'))
for x in range(len(list3)):
    if x%2==1:
        print(list3[x],end='')

4.已知一个数字列表,输出所有元素中,值为奇数的。

list4=list(input('请输入一串数字:'))
new_list=[]
for index in list4:
    if int(index)%2==1:
        new_list.append(index)
print(*new_list)

5.已知一个数字列表,将所有元素乘二。

例如:nums = [1, 2, 3, 4] —> nums = [2, 4, 6, 8]


list5=list(input('请输入一串数字:'))
new_list=[]
for index in list5:
    index=int(index)*2
    new_list.append(index)
print(new_list)

6.有一个长度是10的列表,数组内有10个人名,要求去掉重复的

例如:names = ['张三', '李四', '大黄', '张三'] -> names = ['张三', '李四', '大黄']

names=['张三','李四','老王','张三','小张','小红','老王','小田','小东','小张']
new_name=[]
for index in names:
    if index  not in new_name:
        new_name.append(index)

print(new_name)
print('=============')
names=['张三','李四','老王','张三','小张','小红','老王','小田','小东','小张']
for index in names[:]:
    if names.count(index)>1:
        names.remove(index)
print(names)

7.已经一个数字列表(数字大小在0~6535之间), 将列表转换成数字对应的字符列表

例如: list1 = [97, 98, 99] -> list1 = ['a', 'b', 'c']


list7=[97,98,99]
new_list7=[]
for index in list7:
    new_list7.append(chr(int(index)))
print(new_list7)

8.用一个列表来保存一个节目的所有分数,求平均分数(去掉一个最高分,去掉一个最低分,求最后得分)

list8=[56,78,88,98,67,87,34]

max_list8=list8[len(list8)-1]
for index1 in list8:
    if index1> max_list8:
        max_list8=index1
list8.remove(max_list8)
min_list8=list8[len(list8)-1]
for index2 in list8:
    if index2<min_list8:
        min_list8=index2
list8.remove(min_list8)

num=len(list8)
count=0
for index in list8:
    count+=int(index)
print('平均分是:%.2f'%(count/num))

9.有两个列表A和B,使用列表C来获取两个列表中公共的元素

例如: A = [1, 'a', 4, 90] B = ['a', 8, 'j', 1] --> C = [1, 'a']

A=[1,'a',17,'abc','a',32]
B=['abc',34,17]
C=[]
for index in A:
    if index in B:
        C.append(index)
print(C)

10.有一个数字列表,获取这个列表中的最大值.(注意: 不能使用max函数)

例如: nums = [19, 89, 90, 600, 1] —> 600

nums = [19, 89, 90, 600, 1,34]
new_nuns=nums[len(nums)-1]
for x in range(len(nums)-1):
    if nums[x]>new_nuns:
        new_nuns=nums[x]
print(new_nuns)

11.获取列表中出现次数最多的元素

例如:nums = [1, 2, 3,1,4,2,1,3,7,3,3] —> 打印:3


nums = [1, 2, 3,1,4,2,1,3,7,3,3]
new_nums=[]
for index in nums:
    if index not in new_nums:
        new_nums.append(index)

max_nums=nums.count(new_nums[len(new_nums)-1])
for x in new_nums:
    if  nums.count(x)>max_nums:
           max_str = x
           max_nums=nums.count(x)


print(max_str)
print('=============================')
nums = [1, 2, 3,1,4,2,1,3,7,3,3]
index1=0
max=0
for i in range(len(nums)):
    num=0
    for y in range(i+1,len(nums)):
        if nums[i]==nums[y]:
           num+=1
    if num>max:
        max=num
        index1=i
print(nums[index1])
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 1.已知一个数字列表,求列表中心元素。 list1=[2,3,4,45,6,6,7,7]if len(list1)...
    pythonefb6阅读 4,104评论 0 0
  • 1.使用位运算判断一个数是否是奇数num & 1 ==0 num 是偶数num & 1 ==1 num 是奇数...
    多多爸是小白阅读 4,585评论 0 0
  • 1.已知一个列表,求列表中心元素。 list1 = [4,5,76,55,14,67,43]number = le...
    烧了回忆取暖丶阅读 3,153评论 0 1
  • 1.已知一个数字列表,求列表中心元素。 2.已知一个数字列表,求所有元素和。 3.已知一个数字列表,输出所有奇数下...
    咔佈阅读 1,157评论 0 0
  • 这个胡子都白了的人,渣的比较厉害。他的狂,连母亲身边的丫环都不放过,偏那鸳鸯横竖看不上他,这贾赦不免恼羞成怒,她大...
    龙少之说阅读 3,880评论 2 0

友情链接更多精彩内容