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]
2.利用列表推導式, 完成以下需求import random
nums =
for _ in
range(10
):num = random.randint(50,
300)
for num in nums:
if nums.count(num)
>1:
nums.remove(num)
new_nums =
sorted
(nums, reverse=
true
(new_nums)
a. 生成乙個存放1-100中各位數為3的資料列表
結果為 [3, 13, 23, 33, 43, 53, 63, 73, 83, 93]
b. 利用列表推到是將 列表中的整數提取出來nums =
[x for x in
range(1
,101
)if x %
10==3]
(nums)
nums =
[x for x in
range(3
,101,10
(nums)
例如:[
true,17
,"hello"
,"bye",98
,34,21
]---
[17,98
,34,21
]
c.利用列表推導式 存放指定列表中字串的長度nums =
[true,17
,"hello"
,"bye",98
,34,21
]new_nums =
[x for x in nums if
type
(x)==
int]
(new_nums)
例如 [
"good"
,"nice"
,"see you"
,"bye"]-
--[4
,4,7
,3]
3.已知**如下,請回答出各個print的結果 並說明原因words =
["good"
,"nice"
,"see you"
,"bye"
]nums =
[len
(x)for x in words]
(nums)
4.定義乙個列表,在列表中儲存6個學生的資訊(學生資訊中包括: 姓名、年齡、成績(單科)、**、性別(男、女、不明) )nums =[17
,39,28
,51]nums2 = nums
nums2.pop(
(len
(nums)
)# 這個結果是什麼 請描述原因
# 3# nums2 = nums指的是nums和nums2指向同乙個列表位址,nums2.pop()移除原列表最後乙個元素
numlist =[17
,22,39
,58,[
55,43]
]nums3 = numlist.copy(
(numlist is nums3)
# 結果 原因是什麼
# false
# numlist.copy()結果是返回乙個和原列表相等的列表nums3,nums3的位址和原列表位址不相同
numlist[-1
][0]
=99print
(nums3)
# num3會不會發生變化
# [17, 22, 39, 58, [99, 43]]
# 會變化,nums3的列表完全取決於numlist列表,所以原列表的改變會影響.copy()的列表
a.統計不及格學生的個數
b.列印不及格學生的名字和對應的成績
c.統計未成年學生的個數
d.列印手機尾號是8的學生的名字
e.列印最高分和對應的學生的名字
f.刪除性別不明的所有學生
g.將列表按學生成績從大到小排序(掙扎一下,不行就放棄)
students =[,
,,,,
]# 統計不及格學生個數
result1 =
[x['score'
]for x in students if x[
'score'
]<60]
('不及格學生個數:'
,len
(result1)
)# 列印不及格學生的姓名和對應的成績
result2 =
[[x[
'name'
], x[
'score']]
for x in students if x[
'score'
]<60]
(result2)
# 統計未成年學生的個數
result3 =
[x['age'
]for x in students if x[
'age'
]<18]
('未成年學生個數:'
,len
(result3)
)# 列印手機尾號是8的同學的名字
result4 =
[x['name'
]for x in students if
int(x[
'tel'])
%10==8
('手機號尾號是8的同學為:'
,result4)
# 列印最高分和對應的學生姓名
result5 =
[x['score'
]for x in students]
result6 = students[result5.index(
max(result5))]
('最高分的學生姓名{},分數是{}分'
.format
(result6[
'name'
], result6[
'score'])
)# 刪除性別不明的所有學生
result7 =
[x for x in students if x[
'gender']!=
'不明'
(result7)
for x in result7:
(x)# 將列表按學生成績從大到小排序
for i in
range
(len
(students)):
for j in
range
(len
(students)-1
):if students[j]
['score'
]< students[j+1]
['score']:
students[j]
, students[j+1]
= students[j+1]
, students[j]
for x in students:
(x)
day7 列表和字典作業
1.建立乙個列表,列表中有10個舒宗,保證列表中元素的順序,對列表進行排重,並對列表使用進行降序排序 例如 隨機生成了 70,88,91,70,107,234,91,177,282,197 去重之後 70,88,91,107,234,177,282,197 降序排序 282,234,197,177,...
day7 列表和字典作業
1.建立乙個列表,列表中有10個元素,保證列表中元素的順序,對列表進行排重,並對列表使用進行降序排序 例如 隨機生成了 70,88,91,70,107,234,91,177,282,197 去重之後 70,88,91,107,234,177,282,197 降序排序 282,234,197,177,...
day7 列表和字典作業
1.建立乙個列表,列表中有10個數字,保證列表中元素的順序,對列表進行排重,並對列表使用進行降序排序 例如 隨機生成了 70,88,91,70,107,234,91,177,282,197 去重之後 70,88,91,107,234,177,282,197 降序排序 282,234,197,177,...