python列表题⽬_python列表练习题1.已知⼀个数字列表,求列表中⼼元素。
sum = [59, 54, 89, 45, 78, 45, 12, 96, 789, 45, 69]
if len(sum) & 1 == 0:
print(sum[len(sum) // 2 - 1], sum[len(sum) // 2])
else:
print(sum[len(sum) // 2])
2.已知⼀个数字列表,求所有元素和。
sum = [59, 54, 89, 45, 78, 45, 12, 96, 789, 45, 69]
sum1 = 0
for i in range(0, len(sum)):
sum1 += sum[i]
print(sum1)
3.已知⼀个数字列表,输出所有奇数下标元素。
sum = [59, 54, 89, 45, 78, 45, 12, 96, 789, 45, 69]
for i in range(1, len(sum),2):
print(sum[i])
4.已知⼀个数字列表,输出所有元素中,值为奇数的元素。
sum = [59, 54, 89, 45, 78, 45, 12, 96, 789, 45, 69]
for i in range(0, len(sum)):
if sum[i] % 2 == 1:
print(sum[i])
5.已知⼀个数字列表,将所有元素乘⼆。
例如:nums = [1, 2, 3, 4] —> nums = [2, 4, 6, 8]
nums = [1, 2, 3, 4]
for i in range(0, len(nums)):
nums[i] *= 2
print(nums)
python获取数组长度6.有⼀个长度是10的列表,数组内有10个⼈名,要求去掉重复的
例如:names = ['张三', '李四', '⼤黄', '张三'] -> names = ['张三', '李四', '⼤黄']
names = ['张三', '李四', '⼤黄', '张三']
names1 = []
for i in range(0, len(names) - 1):
for j in range(i + 1, len(names)):
if names[i] == names[j]:
continue
else:
names1.append(names[i])
names = names1
print(names)
names = ['张三', '李四', '⼤黄', '张三']
new_names = []
for name in names:
if name not in new_names:
new_names.append(name)
names = new_names
print(names)
7.已经⼀个数字列表(数字⼤⼩在0~6535之间), 将列表转换成数字对应的字符列表(暂时不做)
例如: list1 = [97, 98, 99] -> list1 = ['a', 'b', 'c']
8.⽤⼀个列表来保存⼀个节⽬的所有分数,求平均分数(去掉⼀个最⾼分,去掉⼀个最低分,求最后得分) scores = [9.8, 9.5, 9.9, 9.3, 8.9, 9.5, 9.6, 9.3, 9.4, 9.6]
max = min =scores[0]
sum = 0
for i in range(0, len(scores)):
if max < scores[i]:
max = scores[i]
if min > scores[i]:
min = scores[i]
sum += scores[i]
print('最后得分是:', (sum - max - min) / (len(scores) - 2))
9.有两个列表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 = []
for i in range(0, len(A)):
for j in range(0, len(B)):
if A[i] == B[j]:
C.append(B[j])
print(C)
10.有⼀个数字列表,获取这个列表中的最⼤值.(注意: 不能使⽤max函数)例如: nums = [19, 89, 90, 600, 1] —> 600
nums = [19, 89, 90, 600, 1]
max = 0
for i in range(0, len(nums)):
if max < nums[i]:
max = nums[i]
print(max)
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]
cou_i = 0
cou = 0
for i in range(0, len(nums) - 1):
count = 0
for j in range(i + 1, len(nums)):
if nums[i] == nums[j]:
count += 1
if cou < count:
cou = count
cou_i = i
print(nums[cou_i])

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。