1.已知一個列表,求列表中心元素
list1 = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k']
list2 = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
def mid(list_in):
if len(list_in) % 2:
mid_pos = int((len(list_in) - 1) / 2)
mid_argu = list_in[mid_pos]
else:
mid_pos = int((len(list_in) - 2)/2)
mid_argu = [list_in[mid_pos], list_in[mid_pos+1]]
return mid_argu
mid_list1 = mid(list1)
mid_list2 = mid(list2)
print(mid_list1)
print(mid_list2)
2.已知一個列表,求所有元素和
nums = [2, 6, 1, 8, 10, 15]
sum1 = 0
for num in nums:
sum1 += num
print(sum1)
3.已知一個列表,求所有奇數(shù)下標元素
nums = [2, 6, 1, 8, 10, 15]
new_nums = nums[1::2]
for new_num in new_nums:
print(new_num)
4.已知一個列表,輸出所有元素中,值為奇數(shù)的
nums = [2, 6, 1, 8, 10, 15]
for num in nums:
if num & 1:
print(num)
5.已知一個列表,將所有元素乘二
nums = [2, 6, 1, 8, 10, 15]
index = 0
for index in range(0,len(nums)):
num = nums[index]
nums[index] = num*2
print(nums)
6.已知一個列表,將所有元素加到第一個元素中
nums = [2, 6, 1, 8, 10, 15]
first = 0
for num in nums:
first += num
nums[0] = first
print(nums)
7.已知一個列表A,將奇數(shù)位置元素存的B列表中,偶數(shù)元素存到C列表中
list_a = [2, 6, 1, 8, 10, 15]
list_b = []
list_c = []
index = 0
for index in range(0,len(list_a)) :
if not index & 1:
list_b.append(list_a[index])
if not list_a[index] & 1:
list_c.append(list_a[index])
print(list_b,list_c,sep=' ** ')
8.把A列表的前5個元素復制到B列表中
list_a = [2, 6, 1, 8, 10, 15]
list_b = list_a[:5]
print(list_b)
9.把1~36分別放入6*6的列表中,計算列表數(shù)組對角元素之和
nums = [
1, 2, 3, 4, 5, 6,
7, 8, 9, 10, 11, 12,
13, 14, 15, 16, 17, 18,
19, 20, 21, 22, 23, 24,
25, 26, 27, 28, 29, 30,
31, 32, 33, 34, 35, 36,
]
sum1 = 0
for i in range(0,6):
index = i*6 + i
sum1 += nums[index]
print(nums[index])
print(sum1)
10.有一個長度是10的列表,列表內(nèi)有10個不重復的數(shù)字,要求按照從大到小排序
nums = [32, 65, 1, 33, 754, 3, 123, 10, 90, 108]
nums.sort(reverse=True)
print(nums)
11.有一個長度是10的列表,要求刪除某一個位置的元素
nums = [32, 65, 1, 33, 754, 3, 123, 10, 90, 108]
index = input('請輸入您想刪除的元素的位置: ')
index = int(index)-1
del nums[index]
print(nums)
12.有一個長度是10的列表,按照遞增的順序排列,用戶輸入一個數(shù),插入適當?shù)奈恢?/h1>
nums = [1, 3, 10, 32, 33, 65, 90, 108, 123, 754]
while True:
new_num = input('請輸入您想插入的數(shù)字,輸入q退出: ')
if new_num == 'q':
break
new_num = int(new_num)
index = 0
while index < len(nums):
if new_num <= nums[index]:
nums.insert(index,new_num)
break
index += 1
else:
nums.append(new_num)
print(nums)
13.有一個長度是10的列表,列表內(nèi)有10個人名,要求去掉重復的
names = [
'zhang san', 'li si', 'wang wu', 'zhao liu',
'haha', 'haha', 'zhang san', 'zhao er',
'yan shisan', 'haha',
]
for name in names[:]:
while names.count(name) > 1:
names.remove(name)
print(names)
14.把A列表第3到6位之間的元素刪除
方法一:
nums = [1, 3, 10, 32, 33, 65, 90, 108, 123, 754]
del nums[2:6]
print(nums)
方法二:
nums = [1, 3, 10, 32, 33, 65, 90, 108, 123, 754]
index = 0
# 創(chuàng)建一個副本,遍歷副本,但是在原列表中刪除元素
for num in nums[:]:
if index in range(3,7):
nums.remove(num)
index += 1
print(nums)
15.已知A列表,B列表,定義一個列表C,要求C包含A和B列表中的元素(但是無重復)
list_a = [1, 2, 3, 4, 5, 6, 7, 8]
list_b = [8, 7, 6, 13, 14, 15, 16]
# 方法一:
```python
先將A和B拼接,再刪除重復項
list_c = list_a[:]
list_c.extend(list_b)
print(list_c)
for num in list_c[:]:
while list_c.count(num) > 1:
list_c.remove(num)
print(list_c)
方法二:
# 先刪除A和B的重復項,再拼接(此方法需要A和B本身無重復元素)
for num in list_a[:]:
if num in list_b:
list_a.remove(num)
list_c = list_a[:]
list_c.extend(list_b)
print(list_c)