作业
有如下值集合[11,22,33,44,55,66,77,88,99,90...],将所有大于 66 的值保存至字典的第一个key中,将小于 66 的值保存至第二个key的值中,结果为{'k1': 大于66的所有值, 'k2': 小于66的所有值}。 kema = {11 ,22 ,33 ,44 ,55 ,66 ,77 ,88 ,99 ,99 ,90}
kee = {'k1':[] ,'k2':[]}
for i in kema:
if i> 66:
kee['k1'].append(i)
else:
kee['k2'].append(i)
print(kee)
有难度
第二种答案:自己搜集的答案
li =[11,22,33,44,55,66,77,88,99,90]
dic={}
list_1 =[] #大于66
list_2 =[] #小于66
for i in li:
if i ==66: continue
if i > 66:
list_1.append(i)
else:
list_2.append(i)
dic.setdefault('k1',list_1)
dic.setdefault('k2',list_2)
print(dic)
第三种答案:
lis = [11,22,33,44,55,66,77,88,99,90]
dic = dict()
dic['k1'] = list()
dic['k2'] = list()
print(dic)
for i in lis:
if i > 66:
dic['k1'].append(i)
elif i < 66:
dic['k2'].append(i)
print(dic)
1. 统计`s='hello jason tank tank jason sean say hello dsb dsb sb'`中每个单词的个数,结果为`{'hello': 2, 'alex': 2, 'say': 1, 'sb': 2}`。
```python
s='hello jason tank tank jason sean say hello dsb dsb sb'
s = s.split( )
kee = {}
for key in s:
if key in kee:
kee[key] += 1
else:
kee[key] = 1
print(kee)
有如下两个集合,pythons是报名python课程的学员名字集合,linuxs是报名linux课程的学员名字集合
pythons={'alex','egon','yuanhao','wupeiqi','gangdan','biubiu'}
linuxs={'wupeiqi','oldboy','gangdan'}
1. 求出即报名python又报名linux课程的学员名字集合
2. 求出所有报名的学生名字集合
3. 求出只报名python课程的学员名字
4. 求出没有同时这两门课程的学员名字集合
print(pythons&linuxs) # print(f"{pythons.intersection(linuxs)}")
print(pythons|linuxs) # print(f"{pythons.union(linuxs)}")
print(pythons-linuxs) # print(f"{pythons.difference(linuxs)}")
print(pythons^linuxs) # print(f"{pythons.symmetric_difference(linuxs)}")
有如下列表,列表元素为不可hash类型,去重,得到新列表,且新列表一定要保持列表原来的顺序
l=[
{'name':'egon','age':18,'sex':'male'},
{'name':'alex','age':73,'sex':'male'},
{'name':'egon','age':20,'sex':'female'},
{'name':'egon','age':18,'sex':'male'},
{'name':'egon','age':18,'sex':'male'},
]
```第一种答案:
ln = []
for i in l:
if i not in ln:
ln.append(i)
print(ln)
第二种答案:自己搜集
new_stu_info_list = []
for stu_info in stu_info_list:
if stu_info not in new_stu_info_list:
new_stu_info_list.append(stu_info)
for new_stu_info in new_stu_info_list:
print(new_stu_info)