Python list tuple dictionary collection

Source: Internet
Author: User
Tags python list

Meta-group

A tuple (tuple) in Python is similar to an array in Java, and once a tuple is created, it cannot be changed in any way. This is similar to a string in python, so we say that tuples and strings are immutable sequences. Tuples also support indexing and sharding operations.

Define a tuple using a pair of small (round) brackets "()".

  1. #定义一个元组
  2. tuple1 = (1, 2, ‘3‘, 4, ‘5‘)
  3. # 定义了一个元组之后就无法再添加或修改元组中的元素
  4. print tuple1[0] # 元组的元素都有确定的顺序。元组的索引也是以0为基点的
  5. print tuple1[-1] # 负的索引从元组的尾部开始计数
  6. print tuple1[1:3] # 元组也可以进行切片操作。对元组切片可以得到新的元组。
  7. # 可以使用 in 运算符检查某元素是否存在于元组中。
  8. print 1 in tuple1 # True
  9. #使用for in 进行遍历元组
  10. for item in tuple1:
  11. print item
  12. # 如果需要获取item的序号 可以使用下面的遍历方法:
  13. for index in range(len(tuple1)):
  14. print tuple1[index]
  15. # 还可以使用内置的enumerate函数
  16. for index, item in enumerate(tuple1):
  17. print ‘%i, %s‘ % (index, item)
List

Lists are the most flexible, ordered collection object types in Python, and unlike strings, lists can contain objects of any kind: numbers, strings, and even other lists. and lists are mutable objects, It supports an operation that is modified in place. You can also get elements by specifying the index and shards. The list is a mutable version of the tuple.

Define a list using a pair of medium (square) brackets "[]". Unlike tuples, lists have built-in functions for adding, modifying, and deleting lists.

  1. #定义一个列表
  2. listA = [‘a‘, ‘b‘, ‘c‘, 1, 2]
  3. # 向 list 中增加元素
  4. # 1.使用append 向 list 的末尾追加单个元素。
  5. listA.append(3)
  6. # 2.使用 insert 将单个元素插入到 list 中。数值参数是插入点的索引
  7. listA.insert(3, ‘d‘) # 在下标为3处添加一个元素
  8. # 3.使用 extend 用来连接 list
  9. listA.extend([7, 8])
  10. ### extend 和 append 看起来类似,但实际上完全不同。
  11. ### extend 接受一个参数,这个参数总是一个 list,
  12. ### 并且把这个 list 中的每个元素添加到原 list 中。
  13. ### 另一方面,append 接受一个参数,这个参数可以是任何数据类型,并且简单地追加到 list 的尾部。
  14. # 获取列表的长度
  15. print len(listA) # 9
  16. # 在 list 中搜索
  17. listA.index(3) # index 在 list 中查找一个值的首次出现并返回索引值。
  18. listA.index(‘100‘) # 如果在 list 中没有找到值,Python 会引发一个异常。
  19. print 5 in listA # 要测试一个值是否在 list 内,使用 in。如果值存在,它返回 True,否则返为 False 。
  20. # 从 list 中删除元素
  21. listA.remove(3) # remove 从 list 中 仅仅 删除一个值的首次出现。如果在 list 中没有找到值,Python 会引发一个异常
  22. print listA.pop() # pop 它会做两件事:删除 list 的最后一个元素,然后返回删除元素的值。
  23. # 遍历list
  24. for item in listA:
  25. print item
Dictionary

A dictionary (Dictionary) is one of the built-in data types of Python that defines a a-to-a relationship between keys and values, but they are stored in an unordered manner. The dictionary in Python resembles an instance of the Hashtable class in Java.

Define Dictionary using a pair of large (curly) brackets "{}"

  1. # 定义一个字典
  2. # Dictionary 不只是用于存储字符串。Dictionary 的值可以是任意数据类型,
  3. # 包括字符串、整数、对象,甚至其它的 dictionary。
  4. # 在单个 dictionary 里,dictionary 的值并不需要全都是同一数据类型,可以根据需要混用和匹配。
  5. dict1 = {‘name‘ : ‘LiuZhichao‘, ‘age‘ : 24, ‘sex‘ : ‘Male‘}
  6. dict1[‘name‘] = ‘Liuzc‘ # 为一个已经存在的 dictionary key 赋值,将简单覆盖原有的值。
  7. dict1[‘Age‘] = 25 # 在 Python 中是区分大小写的 age和Age是完全不同的两个key
  8. # 从字典中删除元素
  9. del dict1[‘sex‘] # del 允许您使用 key 从一个 dictionary 中删除独立的元素
  10. dict1.clear() # clear 从一个 dictionary 中清除所有元素
Collection

Python's collection (set), like other languages, is an unordered set of distinct elements, with basic functionality including relationship testing and de-duplication elements. The collection object also supports mathematical operations such as Union (union), intersection (intersection), Difference (poor), and sysmmetric difference (symmetric difference sets). Because the collection is unordered, the sets does not support indexing, sharding, or other class sequence (sequence-like) operation.

The collection also has immutable forms, frozenset are fixed sets.

  1. #定义一个集合
  2. set1 = {1, 2, 3, 4, 5}
  3. # 或者使用 set 函数
  4. list1 = [6, 7, 7, 8, 8, 9]
  5. set2 = set(list1)
  6. set2.add(10) # 添加新元素
  7. print set2 # set([8, 9, 6, 7]) 去掉重复内容,而且是无序的
  8. set3 = frozenset(list1)
  9. set3.add(10) # 固定集合不能添加元素
  10. ### 集合有并集,交集,求差操作
  11. ### 并集:intersection() 方法返回一个新集合,包含在两个集合中同时出现的所有元素。
  12. ### 交集:union() 方法返回一个新集合,包含在两个 集合中出现的元素。
  13. ### 差集:difference() 方法返回的新集合中,包含所有在 集合A出现但未在集合B中的元素。
  14. ### symmetric_difference() 方法返回一个新集合,包含所有只在其中一个集合中出现的元素。
  15. # 删除元素
  16. set2.discard(6) # 当元素不存在时,不会引发异常
  17. set2.remove(6) # 与discard的区别在于,如果没有要删除的元素,remove会引发一个异常
  18. set2.pop() # 因为set是无序的,所以pop会随机的从set中删除一个元素

Python list tuple dictionary collection

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.