Python Day6 列表练习题

Day6 列表练习

  1. 创建一个列表,列表中有10个数字, 保证列表中元素的顺序,对列表进行排重,并对列表使用进行降序排序
例如:[70, 88, 91, 70, 107, 234, 91, 177, 282, 197]
		--- 去重之后 [70, 88, 91, 107, 234, 177, 282, 197]
  	---- 降序排序 [282, 234, 197, 177, 107, 91, 88, 70]
nums = [70, 88, 91, 70, 107, 234, 91, 177, 282, 197]
nums1 = []
for i in nums:
    if i not in nums1:
        nums1.append(i)
print(nums1)

nums1.sort(reverse=True)
print(nums1)

[70, 88, 91, 107, 234, 177, 282, 197]
[282, 234, 197, 177, 107, 91, 88, 70]

  1. 利用列表推导式, 完成以下需求

a. 生成一个存放1-100中个位数为3的数据列表

结果为 [3, 13, 23, 33, 43, 53, 63, 73, 83, 93]
list1 = [i for i in range(1, 101) if i % 10 == 3]
print(list1)

[3, 13, 23, 33, 43, 53, 63, 73, 83, 93]

b. 利用列表推到是将列表中的整数提取出来

例如:[True, 17, "hello", "bye", 98, 34, 21] --- [17, 98, 34, 21]
list1 = [True, 17, "hello", "bye", 98, 34, 21]
new_list1 = [i for i in list1 if type(i) == int]
print(new_list1)

[17, 98, 34, 21]

c.利用列表推导式存放指定列表中字符串的长度

例如: ["good", "nice", "see you", "bye"] --- [4, 4, 7, 3]
list1 = ["good", "nice", "see you", "bye"]
new_list1 = [len(i) for i in list1]
print(new_list1)

[4, 4, 7, 3]

d. 利用列表推导式删除列表中整数个位数小于5的元素

例如:[24, 'abc', 99, True, 21, 38, 'hello'] --- ['abc', 99, True, 38, 'hello']

方法1:

list1 = [24, 'abc', 99, True, 21, 38, 'hello']
# ['abc', 99, True, 38, 'hello']
new_list1 = [i for i in list1 if type(i) == int and i % 10 < 5]
print(new_list1)

for i in list1:
    if i in new_list1:
        list1.remove(i)
print(list1)

[24, 21]
[‘abc’, 99, True, 38, ‘hello’]

方法2:

list1 = [24, 'abc', 99, True, 21, 38, 'hello']
# ['abc', 99, True, 38, 'hello']
new_list1 = [i for i in list1 if type(i) != int or i % 10 >= 5]
print(new_list1)

[‘abc’, 99, True, 38, ‘hello’]

e. 利用列表推导式获取元素是元组的列表中每个元组的最后一个元素

例如:[(10, 20, 30), ('abc', 'hello'), (1, 2, 3.4), (True, False)]  --- [30, 'hello', 3.4, False]
list1 = [(10, 20, 30), ('abc', 'hello'), (1, 2, 3.4), (True, False)]
new_list1 = [i[-1] for i in list1]
print(new_list1)

[30, ‘hello’, 3.4, False]

f.利用列表推导式将数字列表中所有的奇数乘以2,所有的偶数除以2

例如: [23, 4, 67, 88, 90, 21]  -> [46, 2, 134, 44, 45, 42]

方法1:

list1 = [23, 4, 67, 88, 90, 21]
# [46, 2, 134, 44, 45, 42]
new_list1 = [i * 2 for i in list1 if i % 2 == 1]
new_list2 = [int(i / 2) for i in list1 if i % 2 == 0]
new_list3 = []
new_list3 += new_list1 + new_list2
print(new_list3)

[46, 134, 42, 2, 44, 45]

方法2:

list1 = [23, 4, 67, 88, 90, 21]
new_list1 = [i*2 if i % 2 != 0 else i//2 for i in list1]
print(new_list1)

[46, 2, 134, 44, 45, 42]

  1. 已知一个列表获取列表中指定元素所有的下标
  例如:[10, 20, 34, 10, 9, 78]
  10的下标:[0, 3]
  20的下标:[1]
  30的下标:[]
list1 = [10, 20, 34, 10, 9, 78]
item = 10
list_10 = [index for index in range(len(list1)) if list1[index] == 10]
print('10的下标:', list_10)

list_20 = [index for index in range(len(list1)) if list1[index] == 20]
print('20的下标:', list_20)

list_30 = [index for index in range(len(list1)) if list1[index] == 30]
print('30的下标:', list_30)

10的下标: [0, 3]
20的下标: [1]
30的下标: []

  1. *已知一个数字列表,写程序判断这个列表是否是连续递增列表。
   例如:
   [1, 2, 3, 4, 5]   -> True
   [23, 45, 78, 90]  -> True
   [1, 3, 2, 4, 5]	-> False
list1 = [23, 45, 78, 90]

for index in range(0, len(list1)-1):
    if list1[index] > list1[index+1]:
        print('false')
        break
else:
    print('true')

true

  1. 已知两个列表,将两个列表按照下面的规律交叉合并
   A = [10, 20, 30, 40, 50]
   B = [100, 200, 300]
   结果:[10, 100, 20, 200, 30, 300, 40, 50]
A = [10, 20, 30, 40, 50]
B = [100, 200, 300]
C = []
while True:
    a = A.pop(0)
    b = B.pop(0)
    C.append(a)
    C.append(b)
    if A == [] or B == []:
        break
C += A + B
print(C)

[10, 100, 20, 200, 30, 300, 40, 50]

  1. 已知两个有序列表,将两个列表合并,合并后的新列表中元素仍然是递增列表
   A = [10, 20, 30, 40, 50]
   B = [25, 44, 60]
   结果:[10, 20, 25, 30, 40, 45, 50, 60]
A = [10, 20, 30, 40, 50]
B = [25, 44, 60]
C = []
C += A + B
C.sort(reverse=False)
print(C)

[10, 20, 25, 30, 40, 44, 50, 60]

你可能感兴趣的:(Python,python练习,初级Python,python,数据结构,算法)