【Python-15】Advanced Topics in Python

 

 第一节

      1 介绍了字典的一个内置方法items()

      2 比如我有一个字典d={"a":1,"b":2,"c":3},那么我print d.items()将会输出[("a",1),("b",2),("c",3)]

      3 练习:设置一个名为my_dict的字典,调用它的items方法来输出内容

my_dict = {
    "a": 1,
    "b": 2,
    "c": 3
}
print my_dict.items()

 

 

 第二节

     1 介绍了字典的另外两种方法,keys()和values(),用于输出字典的key的集合和value的集合,用法和items相似

     2 练习:代替第一节中的items方法,利用keys和values方法来输出key和value的集合

my_dict = {
    "a": 1,
    "b": 2,
    "c": 3
}
print my_dict.keys()
print my_dict.values()

>>['a', 'c', 'b']
>>[1, 3, 2]

 

 

 

 第三节

     1 介绍了怎样初始化一个list的用法

     2 比如new_list = [x for x in range(1,6)] 那么# => [1, 2, 3, 4, 5],doubles = [x*2 for x in range(1,6)]# => [2, 4, 6, 8, 10],doubles_by_3 = [x*2 for x in range(1,6) if (x*2)%3 == 0] # => [6]

     3 练习:初始化一个名为even_squares的列表,值为1~10区间能够被2整除的数的平方


even_squares = [i**2 for i in range(1,11) if i%2 == 0]
print even_squares    

 

 

 

 第四节 

     1 介绍了我们怎么把一个list反转过来,比如[1,2,3,4]反转为[4,3,2,1]

     2 介绍了我们可以使用[start:end:stride]来得到list的某个区间从start开始到end,增量为stride

     3 练习:把变量backwards的值设置为my_list的反转


my_list = range(1, 11)

# Add your code below!
backwards = my_list[::-1]

 

 

 

 第五节

     1 介绍了Python里面lambda的使用方式,lambda variable: condition,如果variable满足这个条件那么就为真

     2 比如lambda x: x % 3 == 0,等价于def by_three(x): return x % 3 == 0

     3 练习:把列表squares的值设置为1~10区间数的平方,然后利用lambda打印出大于等于30小于等于70的数


squares = [i**2 for i in range(1 , 11)]
print filter(lambda x : x >= 30 and x <= 70 , squares)

 

 

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Series: Expert's Voice in C Paperback: 312 pages Publisher: Apress; 1 edition (October 29, 2013) Language: English ISBN-10: 1430264004 ISBN-13: 978-1430264002 C is the most widely used programming language of all time. It has been used to create almost every category of software imaginable and the list keeps growing every day. Cutting-edge applications, such as Arduino, embeddable and wearable computing are ready-made for C. Advanced Topics In C teaches concepts that any budding programmer should know. You'll delve into topics such as sorting, searching, merging, recursion, random numbers and simulation, among others. You will increase the range of problems you can solve when you learn how to manipulate versatile and popular data structures such as binary trees and hash tables. This book assumes you have a working knowledge of basic programming concepts such as variables, constants, assignment, selection (if..else) and looping (while, for). It also assumes you are comfortable with writing functions and working with arrays. If you study this book carefully and do the exercises conscientiously, you would become a better and more agile programmer, more prepared to code today's applications (such as the Internet of Things) in C. What you’ll learn What are and how to use structures, pointers, and linked lists How to manipulate and use stacks and queues How to use random numbers to program games, and simulations How to work with files, binary trees, and hash tables Sophisticated sorting methods such as heapsort, quicksort, and mergesort How to implement all of the above using C Who this book is for Those with a working knowledge of basic programming concepts, such as variables, constants, assignment, selection (if..else) and looping (while, for). It also assumes you are comfortable with writing functions and working with arrays. Table of Contents 1. Sorting, Searching and Merging 2. Structures 3. Pointers 4. Linked Lists 5. Stacks and Queries 6. Recursion 7. Random Numbers, Games and Simulation 8. Working with Files 9. Introduction to Binary Trees 10. Advanced Sorting 11. Hash Tables

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值