day6-列表作业

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

#注意列表元素个数的奇偶
nums = [12, 34, 56, 32, 78, 90, 15, 18]
for index in range(len(nums)):
    index1, index2 = (len(nums) // 2 - 1), (len(nums) // 2)
    if len(nums) % 2 == 0:
        print('列表中心元素是:', nums[index1], '和', nums[index2])
        break
    else:
        print('列表中心元素是:', nums[index2])
        break
        
nums = [12, 34, 56, 32, 78, 90, 15, 18]
count = len(nums)
if count % 2:
    print(nums[count // 2])
else:
    print(nums[count // 2 - 1], nums[count // 2])

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

nums = [12, 34, 56, 32, 78, 90, 15, 18, 20]
sum_nums = 0
for index in range(len(nums)):
    sum_nums += nums[index]
print('列表所有元素和:', sum_nums)

nums = [12, 34, 56, 32, 78, 90, 15, 18, 20]
sum1 = 0
for x in nums:
    sum1 += x
print(sum1)

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

nums = [38, 34, 56, 32, 78, 90, 15, 18, 20]
for index in range(len(nums)):
    if index % 2 != 0:
        print(nums[index])
nums = [38, 34, 56, 32, 78, 90, 15, 18, 20]
print(nums[1: :2])

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

nums = [38, 11, 34, 56, 31, 77, 32, 78, 90, 15]
for x in nums:
    if x % 2 != 0:
        print('列表nums所有元素中值为奇数的元素:', x)

nums = [38, 11, 34, 56, 31, 77, 32, 78, 90, 15]
for x in nums:
    if x % 2:
        print(x)

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

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

#方法一:
nums = [38, 34, 56, 32, 78, 90, 15, 21]
new_nums = []
for x in nums:
    x *= 2
    new_nums.append(x)
    #new_nums.append(x * 2)
print(new_nums)

#方法二:
nums = [38, 11, 34, 56, 31, 77, 32, 78, 90, 15]
for index in range(len(nums)):
    nums[index] *= 2
print(nums)

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

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

#去重:相当于列表中每个元素个数都分别只有一个;
#注意:遍历删除可能出现删除不净的情况。因为每一次遍历删除后列表中元素的位置发生了变化,导致部分元素取不到。遍历删除的陷阱
#方案一:利用集合自动去重
names = ['Tom', 'John', '约瑟夫', '章子怡', '约瑟夫', '王菲', 'Tom', '章子怡', 'Bruce·Li', '约瑟夫']
names = list(set(names))
print(names)
#方案二:所有的删除都可以用添加的方式进行解决(添加的思想)
names = ['Tom', 'John', '约瑟夫', '章子怡', '约瑟夫', '王菲', 'Tom', '章子怡', 'Bruce·Li', '约瑟夫']
new_names = []
for name in names:
    if name not in new_names:
        new_names.append(name)
print(new_names)
#方案三: 倒着取获取元素然后判断再删除,最后再打印(删除的思想)
names = ['Tom', 'John', '约瑟夫', '章子怡', '约瑟夫', '王菲', 'Tom', '章子怡', 'Bruce·Li', '约瑟夫']
for index in range(len(names)-1, -1, -1):
    if names[index] in names[0:index]:
        del names[index]
print(names)
#方案四:
names = ['Tom', 'John', '约瑟夫', '章子怡', '约瑟夫', '王菲', 'Tom', '章子怡', 'Bruce·Li', '约瑟夫']
index = 0
while index < len(names):
    if names[index] in names[index+1:]:
        del names[index]
    else:
        index += 1
print(names)
    

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

#方案一:先排序然后删除最小值和最大值,再求和求平均数
scores = [99, 89, 90, 78, 89, 87, 85, 80, 95, 93]
for x in range((len(scores)-1), 0, -1):
    for index in range(x):
        if scores[index] > scores[index + 1]:
            scores[index], scores[index + 1] = scores[index + 1], scores[index]
#print(scores)
scores.pop(0)
scores.pop(-1)
sum_scores = 0
for i in scores:
    sum_scores += i
print("平均分数:", sum_scores / len(scores))
#补充:求取最大值的套路:先假设第一个值最大,然后遍历列表中的元素与其比较,如果遍历的元素最大,就赋值给最大值
scores = [99, 89, 90, 78, 89, 87, 85, 80, 95, 93]
m = scores[0]
for x in scores[1:]:
    if x > m:
        m = x
print(m)
#方案二:
scores = [99, 89, 90, 78, 89, 87, 85, 80, 95, 93]
total = 0
max_scores = min_scores = scores[0]
for x in scores:
    total += x
    if x > max_scores:
        max_scores = x
    if x < min_scores:
        min_scores = x
print((total - max_scores - min_scores) / (len(scores) - 2))

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

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

#方案一:
A = [1, 'a', 4, 90]
B = ['a', 8, 'j', 1]
c = list(set(A) & set(B))
for x in range(len(c)):
    print(c[x])
#方案二:遍历判断,添加
A = [1, 'a', 4, 90]
B = ['a', 8, 'j', 1]
c = []
for x in A:
    if x in B and x not in C:
        c.append(x)
print(c)
 

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

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

nums = [24, 45, 67, 23, 789, 123, 345, 11]
#方案一:对这个列表进行排序,然后直接取最后一个值就是最大值
for x in range((len(nums)-1), 0, -1):
    for index in range(x):
        if nums[index] > nums[index + 1]:
            nums[index], nums[index + 1] = nums[index + 1], nums[index]
print('这个列表中的最大值:', nums[-1])
nums = [24, 45, 67, 23, 789, 123, 345, 11]
max_num = nums[0]
for x in nums:
    if x > max_num:
        max_num = x
print('这个列表中的最大值:', max_num)

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

例如: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]

    
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值