1.已知一個數(shù)字列表,求列表中心元素
nums = [10,20,30,45,21,46,89]
if len(nums)%2 == 0:
n = int(len(nums) / 2)
print(nums[n-1:n+1])
else:
n = int((len(nums)+1)/2)
print(nums[n-1:n])
2.已知一個數(shù)字列表消玄,求所有元素和
numbers = [2,3,1,4,5]
sum1 = sum(numbers)
print(sum1)
3.已知一個數(shù)字列表,輸出所有奇數(shù)下標元素酬姆。
numbers = [12,34,54,12,45,21]
for index in range(len(numbers)):
if index % 2 !=0:
print(numbers[index],end=',')
print()
4.已知一個數(shù)字列表奥溺,輸出所有元素中,值為奇數(shù)的相满。
numbers = [12,34,54,13,45,21]
for num in numbers:
if num % 2 !=0:
print(num,end=',')
print()
5.已知一個數(shù)字列表桦卒,將所有元素乘二。
numbers = [12,34,54,13,45,21]
new_numbers = []
for num in numbers:
new_num = num *2
new_numbers.append(new_num)
print(new_numbers)
6.有一個長度是10的列表建蹄,數(shù)組內有10個人名裕偿,要求去掉重復的
names = ['路飛','索隆','烏索普','山治','山治','路飛','索隆','路飛','喬巴','布魯克']
new_names = []
for name in names:
if name not in new_names:
new_names.append(name)
print(new_names)
7.已知一個數(shù)字列表(數(shù)字大小在0~6535之間), 將列表轉換成數(shù)字對應的字符列表
例如: list1 = [97, 98, 99] -> list1 = ['a', 'b', 'c']
list1 = [97,98,99,101]
list2 = []
for num in list1:
list2.append(chr(num))
print(list2)
用一個列表來保存一個節(jié)目的所有分數(shù),求平均分數(shù)(去掉一個最高分劲腿,去掉一個最低分鸟妙,求最后得分)
scores = [8,10,6,8,9,10]
max = 0
min = 10
for item in scores:
if item > max:
max = item
if item<min:
min = item
while max in scores:
scores.remove(max)
while min in scores:
scores.remove(min)
print(sum(scores)/len(scores))
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 num1 in a:
for num2 in b:
if num1 == num2:
c.append(num1)
print(c)
1.已知一個數(shù)字列表,求列表中心元素坪郭。
nums = [1, 2, 3, 4, 5, 10, 11]
length = len(nums)
if length % 2 == 0:
print(nums[length//2], nums[length//2-1])
else:
print(nums[length//2])
"""
[1,2,3,4] 1, 2 --- 4
[1, 2, 3, 4, 5, 6] 2,3 --- 6
[1, 2, 3] 1 -- 3
"""
2.已知一個數(shù)字列表歪沃,求所有元素和嫌松。
print(sum(nums))
3.已知一個數(shù)字列表,輸出所有奇數(shù)下標元素萎羔。
print(nums[1::2])
4.已知一個數(shù)字列表,輸出所有元素中缘眶,值為奇數(shù)的。
for num in nums:
if num % 2 == 1:
print(num)
5.已知一個數(shù)字列表该抒,將所有元素乘二顶燕。
nums = [1, 2, 3, 4, 5, 10, 11]
# nums = [2, 4, 6, 8, 10, 20, 22]
# 列表[下標] = 值
for index in range(len(nums)):
nums[index] *= 2 # nums[index] = nums[index] * 2
print(nums)
6.有一個長度是10的列表,數(shù)組內有10個人名欧引,要求去掉重復的
# 方法一:用集合
names = ['張三', '李四', '大黃', '張三']
names = list(set(names))
print(names)
names = ['張三', '張三', '李四', '大黃', '張三']
for name in names[:]:
if names.count(name) > 1:
names.remove(name)
print(names)
7.已經(jīng)一個數(shù)字列表(數(shù)字大小在0~65535之間), 將列表轉換成數(shù)字對應的字符列表
例如: list1 = [97, 98, 99] -> list1 = ['a', 'b', 'c']
nums = [60, 78, 89, 0x4fff, 0x78ef, 120, 67]
for index in range(len(nums)):
nums[index] = chr(nums[index])
print(nums)
8.用一個列表來保存一個節(jié)目的所有分數(shù)恳谎,求平均分數(shù)(去掉一個最高分,去掉一個最低分癌蓖,求最后得分)
scores = [89, 99, 87, 70, 64, 10]
print((sum(scores) - max(scores) - min(scores)) / (len(scores) - 2))
9.有另個列表A和B婚肆,使用列表C來獲取兩個列表中公共的元素
例如: A = [1, 'a', 4, 90, 1] B = ['a', 8, 'j', 1] --> C = [1, 'a']
A = [1, 'a', 4, 90, 1]
B = ['a', 8, 'j', 1]
C = list(set(A) & set(B))
print(C)
C = []
for item in A:
if (item in B) and (item not in C):
C.append(item)
print(C)