024、【练习】

 

 

1、把  字符串 'createJobFile" ——>  'create_job_file'

# 把 createJobFile——>create_job_file
a = 'createJobFile'
for i in a:
    if i.isupper():
        a = a.replace(i, f'_{i.lower()}')
print(a)
View Code

执行结果如下:

create_job_file

Process finished with exit code 0
View Code

 

2、把字典的key ,value 对换

# 把字典的key ,value 对换
stu_info = {'name': 'sky', 'hobby': 'swimming', 'age': 18}
new_dic = {}
for k, v in stu_info.items():
    new_dic[v] = k

print(new_dic)
View Code

执行结果如下:

{'sky': 'name', 'swimming': 'hobby', 18: 'age'}

Process finished with exit code 0
View Code

 

3、a = 'dfjsaklfjsalfjlksadjflkajdflksa'

    求每一个字符串出现的次数,并且次数从大到小排序 ;

# a = 'dfjsaklfjsalfjlksadjflkajdflksa'
# 求每一个字符串出现的次数,并且次数从大到小排序 ;
a = 'dfjsaklfjsalfjlksadjflkajdflksa'
dict_temp = {}
for i in a:
    dict_temp[i] = a.count(i)

new_list = sorted(dict_temp.items(), key=lambda item: item[1], reverse=True)
print(dict(new_list))
View Code

执行结果如下:

{'f': 5, 'j': 5, 'a': 5, 'l': 5, 's': 4, 'k': 4, 'd': 3}

Process finished with exit code 0
View Code

 

posted @ 2021-08-10 23:22  空-山-新-雨  阅读(30)  评论(0编辑  收藏  举报