列表基础
1.已知一个列表,求列表中心元素。
list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
num = len(list1) / 2
if num % 1 != 0:
print('中心元素是{}'.format([int(num)]))
else:
print('中心元素是{}和{}'.format(list1[int(num) - 1], list1[int(num)]))
结果如下:
E:\LHYPython\vacation_job\venv\Scripts\python.exe E:/LHYPython/vacation_job/01-job.py
中心元素是5和6
Process finished with exit code 0
2.已知一个列表,求所有元素和。
list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
sum1 = 0
for num in list1:
sum1 += num
print('元素和等于{}'.format(sum1))
结果如下:
E:\LHYPython\vacation_job\venv\Scripts\python.exe E:/LHYPython/vacation_job/01-job.py
元素和等于55
Process finished with exit code 0
3.已知一个列表,输出所有奇数下标元素。
list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
for index in range(1, len(list1), 2):
print(list1[index], end=',')
print()
结果如下:
E:\LHYPython\vacation_job\venv\Scripts\python.exe E:/LHYPython/vacation_job/01-job.py
2 4 6 8 10
Process finished with exit code 0
4.已知一个列表,输出所有元素中,值为奇数的。
list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
for index in range(len(list1)):
if list1[index] % 2 != 0:
print(list1[index], end=',')
print()
结果如下:
E:\LHYPython\vacation_job\venv\Scripts\python.exe E:/LHYPython/vacation_job/01-job.py
1,3,5,7,9,
Process finished with exit code 0
5.已知一个列表,将所有元素乘二。
list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 'abc']
for index in range(len(list1)):
list1[index] *= 2
print(list1)
结果如下:
E:\LHYPython\vacation_job\venv\Scripts\python.exe E:/LHYPython/vacation_job/01-job.py
[2, 4, 6, 8, 10, 12, 14, 16, 18, 'abcabc']
Process finished with exit code 0
6.有一个长度是10的列表,数组内有10个人名,要求去掉重复的
例如:names = ['张三', '李四', '大黄', '张三'] -> names = ['张三', '李四', '大黄']
names1 = ['黄渤', '张艺兴', '王迅', '黄渤', '黄磊', '孙红雷', '黄磊', '罗志祥', '张艺兴']
index = 0
while index < len(names1):
name = names1[index]
num = index + 1
while num < len(names1):
if name == names1[num]:
del names1[num]
else:
num += 1
else:
index += 1
print(names1)
names = ['黄渤', '张艺兴', '王迅', '黄渤', '黄磊', '孙红雷', '黄磊', '罗志祥', '张艺兴']
for name in names[:]:
if names.count(name) > 1:
names.remove(name)
print(names)
结果如下:
E:\LHYPython\vacation_job\venv\Scripts\python.exe E:/LHYPython/vacation_job/01-job.py
['黄渤', '张艺兴', '王迅', '黄磊', '孙红雷', '罗志祥']
Process finished with exit code 0
7.已经一个数字列表(数字大小在0~6535之间), 将列表转换成数字对应的字符列表
例如: list1 = [97, 98, 99] -> list1 = ['a', 'b', 'c']
list1 = [1, 10, 100, 1000, 10000, 2, 20, 200, 2000, 20000]
for index in range(len(list1)):
list1[index] = chr(list1[index])
print(list1)
结果如下:
E:\LHYPython\vacation_job\venv\Scripts\python.exe E:/LHYPython/vacation_job/01-job.py
['\x01', '\n', 'd', 'Ϩ', '✐', '\x02', '\x14', 'È', 'ߐ', '丠']
Process finished with exit code 0
8.用一个列表来保存一个节目的所有分数,求平均分数(去掉一个最高分,去掉一个最低分,求最后得分)
list1 = [89, 90, 89, 91,91, 75, 80, 85, 100, 60 ]
list1.remove(max(list1))
list1.remove(min(list1))
average = sum(list1) / len(list1)
print(list1, average)
结果如下:
E:\LHYPython\vacation_job\venv\Scripts\python.exe E:/LHYPython/vacation_job/01-job.py
[89, 90, 89, 91, 91, 75, 80, 85] 86.25
Process finished with exit code 0
9.有两个列表A和B,使用列表C来获取两个列表中公共的元素
例如: A = [1, 'a', 4, 90] B = ['a', 8, 'j', 1] --> C = [1, 'a']
# 1.
list_a = [1, 3, 5, 9]
list_b = [1, 3, 7, 9]
list_c = []
for a_index in range(len(list_a)):
for b_index in range(len(list_b)):
if list_a[a_index] == list_b[b_index]:
list_c.append(list_a[a_index])
index = 0
while index < len(list_c):
name = list_c[index]
num = index + 1
while num < len(list_c):
if name == list_c[num]:
del list_c[num]
else:
num += 1
else:
index += 1
print(list_c)
# 2.
list_a = [1, 3, 5, 9]
list_b = [1, 3, 7, 9]
list_c = [val for val in list_a if val in list_b]
print(list_c)
结果如下:
E:\LHYPython\vacation_job\venv\Scripts\python.exe E:/LHYPython/vacation_job/01-job.py
[1, 3, 9]
Process finished with exit code 0
网友评论