博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
coursesa课程 Python 3 programming course_2_assessment_8 sorted练习题
阅读量:4056 次
发布时间:2019-05-25

本文共 1948 字,大约阅读时间需要 6 分钟。

Given the same dictionary, medals, now sort by the medal count. Save the three countries with the highest medal count to the list, top_three.

medals = {
'Japan':41, 'Russia':56, 'South Korea':21, 'United States':121, 'Germany':42, 'China':70}top_three = sorted(medals,key = lambda x:medals[x] , reverse = True)[0:3]

We have provided the dictionary groceries. You should return a list of its keys, but they should be sorted by their values, from highest to lowest. Save the new list as most_needed.

groceries = {
'apples': 5, 'pasta': 3, 'carrots': 12, 'orange juice': 2, 'bananas': 8, 'popcorn': 1, 'salsa': 3, 'cereal': 4, 'coffee': 5, 'granola bars': 15, 'onions': 7, 'rice': 1, 'peanut butter': 2, 'spinach': 9}most_needed = sorted(groceries , key = lambda fruit:groceries[fruit] , reverse = True )print(most_needed)

Create a function called last_four that takes in an ID number and returns the last four digits. For example, the number 17573005 should return 3005. Then, use this function to sort the list of ids stored in the variable, ids, from lowest to highest. Save this sorted list in the variable, sorted_ids. Hint: Remember that only strings can be indexed, so conversions may be needed.

def last_four(x):    x = str(x)    return x[-4:]   ids = [17573005, 17572342, 17579000, 17570002, 17572345, 17579329]sorted_ids = sorted(ids,key=last_four)print(sorted_ids)

Sort the list ids by the last four digits of each id. Do this using lambda and not using a defined function. Save this sorted list in the variable sorted_id.

ids = [17573005, 17572342, 17579000, 17570002, 17572345, 17579329]sorted_id = sorted(ids,key = lambda x:(str(x))[-4:])print(sorted_id)

Sort the following list by each element’s second letter a to z. Do so by using lambda. Assign the resulting value to the variable lambda_sort.

ex_lst = ['hi', 'how are you', 'bye', 'apple', 'zebra', 'dance']lambda_sort = sorted(ex_lst , key = lambda x:x[1] )print(lambda_sort)

转载地址:http://pirci.baihongyu.com/

你可能感兴趣的文章
mysql:sql alter database修改数据库字符集
查看>>
mysql:sql truncate (清除表数据)
查看>>
yuv to rgb 转换失败呀。天呀。谁来帮帮我呀。
查看>>
驱动TFT要SDRAM做为显示缓存
查看>>
使用file查看可执行文件的平台性,x86 or arm ?
查看>>
qt 创建异形窗体
查看>>
简单Linux C线程池
查看>>
内存池
查看>>
linux 驱动开发 头文件
查看>>
ipconfig,ifconfig,iwconfig
查看>>
opensuse12.2 PL2303 minicom
查看>>
网络视频服务器移植
查看>>
Encoding Schemes
查看>>
移植QT
查看>>
如此调用
查看>>
计算机的发展史
查看>>
带WiringPi库的交叉编译如何处理一
查看>>
带WiringPi库的交叉笔译如何处理二之软链接概念
查看>>
Spring事务的七种传播行为
查看>>
ES写入找不到主节点问题排查
查看>>