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.已知一个列表,求所有奇数下标元素
nums = [2, 6, 1, 8, 10, 15]
new_nums = nums[1::2]
for new_num in new_nums:
print(new_num)
4.已知一个列表,输出所有元素中,值为奇数的
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,将奇数位置元素存的B列表中,偶数元素存到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的列表中,计算列表数组对角元素之和
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的列表,列表内有10个不重复的数字,要求按照从大到小排序
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的列表,按照递增的顺序排列,用户输入一个数,插入适当的位置
nums = [1, 3, 10, 32, 33, 65, 90, 108, 123, 754]
while True:
new_num = input('请输入您想插入的数字,输入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的列表,列表内有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
# 创建一个副本,遍历副本,但是在原列表中删除元素
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)
网友评论