Python 3.3 Tutorial Notes - 3:Data Structure

1. List
     1.1 list.pop([i]), list.sort(), list.reverse()
     1.2 insert, remove or sort that modify the list have no return value printed – they return None.
     1.3 list.pop() and deque.popleft()
          1.3.1 using "deque" need "from collections import deque"
     1.4 squares = [x**2 for x in range(10)] --> the list will be [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
          1.4.1 it also could be squares = list(map(lambda x: x**2, range(10)))
          1.4.2 Another sample: [(x, y) for x in [1,2,3] for y in [3,1,4] if x != y]
2. Tuple
     2.1 list a=[1,2,3]; tuple b=(1,2,3)
     2.2 tuple is immutable, list is mutable.
     2.3 create empty tuple: tuple_empty = ()
     2.4 create tuple with only one element: one_element = "hello" , 
3. Sets
     3.1 unordered, no duplication
     3.2 Creation: a = {1, 2, 3}, or a = set()
     3.3 -, |, &, ^
4. Dictionary
     4.1 a dictionary is an unordered set of key: value pairs, with the requirement that the keys are unique (within one dictionary).
5. Loop
     5.1 When looping through a sequence, the position index and corresponding value can be retrieved at the same time using the enumerate() function.
          >>> for i, v in enumerate([’tic’, ’tac’, ’toe’]):
          ... print(i, v)
     5.2 To loop over two or more sequences at the same time, the entries can be paired with the zip() function.
>>> questions = [’name’, ’quest’, ’favorite color’]
>>> answers = [’lancelot’, ’the holy grail’, ’blue’]
>>> for q, a in zip(questions, answers):
... print(’What is your {0}? It is {1}.’.format(q, a))
...
What is your name? It is lancelot.
What is your quest? It is the holy grail.
What is your favorite color? It is blue.
     5.3 To loop over a sequence in reverse, first specify the sequence in a forward direction and then call the reversed() function.
     5.4 To change a sequence you are iterating over while inside the loop (for example to duplicate certain items), it is recommended that you first make a copy.
          5.4.1 To make a copy, use slice: words[...] -> words[:]
6. Other
     6.1 The operators  is  and   is not  compare whether two objects are really the same object; this only matters for mutable objects like lists. 
     6.2 A and not B or C is equivalent to (A and (not B)) or C.
     6.3 Comparisons can be chained. For example, a < b == c tests whether a is less than b and moreover b equals c.
     6.4 The Boolean operators  and  and  or  are so-called short-circuit operators. When used as a general value and not as a Boolean, the return value of a short-circuit operator is the last evaluated argument.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值