1.已知一個(gè)列表驮吱,求列表中心元素茧妒。
list1 = [1,'asd',123,324,'dfs']
index = int(len(list1))
if index & 1 == 0:
print(list1[int(index>>1)],list1[int((index>>1) -1)])
else:
print(list1[int((index-1)>>1)])
2.已知一個(gè)列表,求所有元素和左冬。
list1 = [1,23,32,345,3456,34,985]
print(sum(list1))
3.已知一個(gè)列表桐筏,輸出所有奇數(shù)下標(biāo)元素。
list1 = [1,23,32,345,3456,34,985]
list2 = []
for index in range(1,len(list1),2):
item = list1[index]
list2.append(item)
print(list2)
4.已知一個(gè)列表拇砰,輸出所有元素中梅忌,值為奇數(shù)的。
list1 = [1,23,32,345,3456,34,985]
index = 0
while index < len(list1):
if list1[index] & 1 == 0:
del list1[index]
continue
index += 1
print(list1)
5.已知一個(gè)列表毕匀,將所有元素乘二铸鹰。
list1 = [1,23,32,345,3456,34,985,'123','da']
list2 = []
index = 0
while index < len(list1):
item = list1[index] * 2
list2.append(item)
index += 1
print(list2)
6.有一個(gè)長(zhǎng)度是10的列表,數(shù)組內(nèi)有10個(gè)人名皂岔,要求去掉重復(fù)的
例如:names = ['張三', '李四', '大黃', '張三'] -> names = ['張三', '李四', '大黃']
names = ['a','b','c','d','e','b','f','a','c']
index = 0
while index < len(names):
i = 1
while index + i < len(names):
if names[index] == names[index+i]:
names.remove(names[index])
continue
i += 1
index += 1
print(names)
7.已經(jīng)一個(gè)數(shù)字列表(數(shù)字大小在0~6535之間), 將列表轉(zhuǎn)換成數(shù)字對(duì)應(yīng)的字符列表
例如: list1 = [97, 98, 99] -> list1 = ['a', 'b', 'c']
list1 = [4,654,1234,5214,1236,214,368]
list2 = []
index = 0
while index < len(list1):
item = chr(list1[index])
list2.append(item)
index += 1
print(list2)
8.用一個(gè)列表來(lái)保存一個(gè)節(jié)目的所有分?jǐn)?shù)蹋笼,求平均分?jǐn)?shù)(去掉一個(gè)最高分,去掉一個(gè)最低分躁垛,求最后得分)
score = [3,5,4,8,6,1,5,8,7,9,9]
score.remove(max(score))
score.remove(min(score))
index = 0
item1 = 0
while index < len(score):
item = score[index]
item1 += item
index += 1
print(item1/len(score))
9.有另個(gè)列表A和B剖毯,使用列表C來(lái)獲取兩個(gè)列表中公共的元素
例如: A = [1, 'a', 4, 90] B = ['a', 8, 'j', 1] --> C = [1, 'a']