1.已知⼀个列表,求列表中⼼元素。
list = [2,3,5,6,4,7,9]
num = len(list)
s = num // 2
print(list[s])

2.已知⼀个列表,求所有元素和。
list = [2,3,5,6,4,7,9]
s = 0
for i in list:
s += i
print(s)

3.已知⼀个列表,输出所有奇数下标元素。
list = [2,3,5,6,4,7,9]
for i in list[1::2]:
print(i)

4.已知⼀个列表,输出所有元素中,值为奇数的。
list = [2,3,5,6,4,7,9]
for i in list:
if i%2 :
print(i)

5.已知⼀个列表,将所有元素乘⼆。
list = [2,3,5,6,4,7,9]
lists = []
for i in list:
i *= 2
lists.append(i)
print(lists)

6.已知⼀个列表,将所有元素加到第⼀个元素中。
list = [2,3,5,6,4,7,9]
list1 = [list[0]]
for i in list[1:]:
list1.append(i)
lists = [list1] + list[1:]
print(lists)

7.已知⼀个列表A,将奇数位置元素存到B列表中,偶数元素存到C列表中。
listA = [2,3,5,6,4,7,9]
listB = []
listC = []
for i in listA[1::2]:
listB.append(i)
for j in listA[0::2]:
listC.append(j)
print(listB,listC)

8.把A列表的前5个元素复制到B列表中。
listA = [2,3,5,6,4,7,9]
listB = []
for i in listA[0:5]:
listB.append(i)
print(listB)

9.把1----36分别放⼊列表中,计算列表数组对⻆元素之和。 6 * 6的列表
s = 0
for i in range(1,37):
print([i], end=' ')
if i % 6 == 0:
print('')
s += i
print('对角线元素之和为%d'%(s/3))

10.有⼀个⻓度是10的列表,列表内有10个不重复的数字,要求按从⼤到⼩排序。
# sorted函数可以被赋值,原列表未被更新
lists = [1,44,3,4,5,8,7,13,9,11]
new_list = sorted(lists)
print(new_list[::-1])
# sort函数不能被赋值,且原列表已被更新
lists.sort()
print(lists[::-1])

11.有⼀个⻓度是10的列表,要求删除某⼀个位置的元素
lists = [1,2,3,4,5,6,7,8,9,11]
n = int(input('请输入下标:'))
del lists[n]
print(lists)

12.有⼀个⻓度是10的列表,按递增排列,⽤户输⼊⼀个数,插⼊适当位置。
lists = [1, 3, 4, 5, 7, 8, 9, 11, 13, 44]
new_list = []
num = int(input('请输入一个数:'))
lists.append(num)
lists.sort()
print(lists)

13.有⼀个⻓度是10的列表,数组内有10个⼈名,要求去掉重复的
lists = ['aa','cc','dd','bb','qq','ww','aa','rr','bb','tt']
listA = list(set(lists))
print(listA)
14.把A列表的第三到第六位之间的元素删除。
num = 1
lists = [1,2,3,4,5,6,7,8,9,11]
while num < 3:
del lists[3]
num += 1
print(lists)

15.已知A列表, B列表,定义⼀个列表C,要求C包含A,B数组中的数据(⽆重复值)。
listA = [2,5,7,3,6]
listB = [2,6,1,8,9]
listC = list(set(listA + listB))
print(listC)

网友评论