[Python] The difference between list, tuple, dict

http://www.cnblogs.com/Michael-Kong/archive/2012/07/11/2585840.html
  1. Dictionary is one of  Python  's built-in data types, which defines a one-to-one relationship between keys and values.
  2. Each element is a key-value pair, and the entire set of elements is enclosed in braces
  3. You can refer to its value by key, but you cannot get key by value
  4. There can be no duplicate keys in a dictionary. Assigning a value to an existing key will overwrite the original value. 2 A new key-value pair can be added at any time. This syntax is the same as modifying existing values.
  5. When using a dictionary, you need to know: The key of a dictionary is case sensitive
  6. Dictionary is not just for storing strings. The value of Dictionary can be any data type, including string, integer, object, and even other dictionary. In a single dictionary, the values ​​of the dictionary do not need to be all of the same data type, and can be mixed and matched as needed. 2 Dictionary keys are much stricter, but they can be strings, integers, and several other types (we will talk about this later). You can also mix and match key data types in a dictionary
  7. del  allows you to delete individual elements from a dictionary using keys.
  8. clear  clears all elements from a dictionary. Note that the empty set of braces represents a dictionary with no elements.

 

  1. A list is an ordered collection of elements enclosed in square brackets.
  2. List can be used as an array starting with 0 subscript. The first element of any non-empty list is always  li[0]
  3. Negative index counts forward from the end of the list to access elements. The last element of any non-empty list is always  li[-1]. 2If the negative index makes you feel confused, you can understand it like this:  li[-n] == li[len(li)-n] . So in this list,  li[-3] == li[5-3] == li[2] .
  4. You can get a subset of the list by specifying 2 indexes, which is called a " slice ". The return value is a new list, which contains everything in the list starting from the first slice index (here  li[1]), up to but not including the second slice index (here li[3] ) element.
  5. If both fragment indexes are omitted, this will include all elements of the list. But unlike the original  list named li, it is a new list that happens to have   all the same elements as li . li[:]  is a shorthand for making a complete copy of a list.
  6.  append  appends a single element to the end of the list.
  7. insert  inserts a single element into the list. The numeric parameter is the index of the insertion point. Please note that the elements in the list need not be unique, there are two independent elements with   the same value of'new' .
  8.  extend is  used to connect the list. Please be careful not to use multiple parameters to call  extend , but use one list parameter to call.
  9. The two methods of Lists,  extend  and  append,  look similar, but are actually completely different. extend  accepts one parameter, which is always a list, and adds each element in this list to the original list
  10. On the other hand,  append  accepts a parameter, which can be of any data type, and is simply appended to the end of the list. Here, the append  method is called with a list parameter containing 3 elements  .
  11. index  finds the first occurrence of a value in the list and returns the index value.
  12. To test whether a value is in the list, use  in . If the value exists, it returns  True , otherwise it returns  False  .
  13. remove  removes the first occurrence of a value from the list.
  14. Pop  is an interesting thing. It will do two things: delete the last element of the list, and then return the value of the deleted element. Please note that this is  different from  li[-1] , which returns a value without changing the list itself. It is also different from  li.remove( value ) , which changes the list but does not return a value.
  15. Lists can also be  connected with the  + operator. List  =  List  +  otherlist  corresponds  List .extend ( otherlist ) . But the  + operator returns a new (after connection) list as a value, while  extend  only modifies the existing list. In other words, for large lists,  extend  execution speed is faster.
  16.  Python  supports the  +=  operator. li += ['two'] is  equivalent to  li.extend(['two'])The +=  operator can be used for lists, strings and integers, and it can also be overloaded for use in user-defined classes.
  17. * 运算符可以作为一个重复器作用于 list。 li = [1, 2] * 3 等同于 li = [1, 2] + [1, 2] + [1, 2], 即将三个 list 连接成一个。

  1.     Tuple是不可变的list.一是创建了一个tuple就不能以任何方式改变它.
  2.     定义tuple与定义list的方式相同,除了整个元素集是用小括号包围的而不是方括号.
  3.   Tuple的元素与list一样按定义的次序进行排序.Tuples的索引与list一样从0开始,所以一个非空的tuple的第一个元素总是t[0].
  4.     负数索引与 list 一样从 tuple 的尾部开始计数。
  5.     与 list 一样分片 (slice) 也可以使用。注意当分割一个 list 时, 会得到一个新的 list ;当分割一个 tuple 时, 会得到一个新的 tuple。
  6.     Tuple 没有方法:没有 append 或 extend 方法、没有 remove 或 pop 方法、没有 index 方法、可以使用 in 来查看一个元素是否存在于 tuple 中。

   其中list()函数与tuple()函数接受可抚今迭代的对象(比如一个序列)作为参数,并通过浅拷贝数据来创建一个新的列表与元组.虽然字符串也是序列类型,但一般它们不用于list()与tuple().更多的情况下,它们用于在两种类型之间进行轩换,比如你需要把一个已有的元组转换成列表类型(然后你可以修改它的元素),反之也是.

     alist=['123','456'];

     atuple=tuple(alist);

     print atuple 

     >>>('123', '456')

    alist==atuple

    >>> False

    alist2=list(atuple)

   alist2==alist

   >>>True

   alist is alist2

   >>>False

   再说id()确认一下, [id(x)  for x in alist,atuple,alist2]

    >>>[10903800,12003900,11730280]

    所以无论是list()还是tuple()都不可能做到完全转换,也就是说传递一个元组到list不会变成真正的列表,同时传递一个列表到tuple()也不会变成一个真正的元组.虽然前后两个对象有相同的数据集合,但是变量指向的不是同一个对象.需要注意的是:它们的所有值相同,一个列表也不可能"等于"一个元组的.


字典(dict)
dict 用 {} 包围 
dict.keys(),dict.values(),dict.items() 
hash(obj)返回obj的哈希值,如果返回表示可以作为dict的key 
del 或 dict.pop可以删除一个item,clear清除所有的内容 
sorted(dict)可以吧dict排序 
dict.get()可以查找没存在的key,dict.[]不可以 
dict.setdefault() 检查字典中是否含有某键。 如果字典中这个键存在,你可以取到它的值。 如果所找的键在字典中不存在,你可以给这个键赋默认值并返回此值。 
{}.fromkeys()创建一个dict,例如: {}.fromkeys(('love', 'honor'), True) =>{'love': True, 'honor': True} 
不允许一个键对应多个值 
键值必须是哈希的,用hash()测试 
一个对象,如果实现_hash()_方法可以作为键值使用



集合(set)
集合是一个数学概念,用set()创建 
set.add(),set.update.set.remove,添加更新删除,-= 可以做set减法 
set.discard 和 set.remove不同在于如果删除的元素不在集合内,discard不报错,remove 报错 
< <= 表示 子集,> >=表示超集 
| 表示联合 & 表示交集 - 表示差集 ^ 差分集里啊


列表(list)
列表是序列对象,可包含任意的Python数据信息,如字符串、数字、列表、元组等。列表的数据是可变的,我们可通过对象方法对列表中的数据进行增加、修改、删除等操作。可以通过list(seq)函数把一个序列类型转换成一个列表。
append(x) 在列表尾部追加单个对象x。使用多个参数会引起异常。 
count(x) 返回对象x在列表中出现的次数。 
extend(L) 将列表L中的表项添加到列表中。返回None。 
Index(x) 返回列表中匹配对象x的第一个列表项的索引。无匹配元素时产生异常。 
insert(i,x) 在索引为i的元素前插入对象x。如list.insert(0,x)在第一项前插入对象。返回None。 
pop(x) 删除列表中索引为x的表项,并返回该表项的值。若未指定索引,pop返回列表最后一项。 
remove(x) 删除列表中匹配对象x的第一个元素。匹配元素时产生异常。返回None。 
reverse() 颠倒列表元素的顺序。 
sort() 对列表排序,返回none。bisect模块可用于排序列表项的添加和删除。 


Tuple (tuple)
tuple=(1,), this is a tuple representation of a single element, and an extra comma is required.
tuple=1, 2, 3, 4, this can also be a tuple. When parentheses are not used without causing confusion, Python allows tuples without parentheses.
Like lists, tuples can be indexed, sliced, joined, and repeated. You can also use len() to find the length of the tuple.  
The index of the tuple is in the form of tuple[i], not tuple(i). 
Similar to lists, use tuple(seq) to convert other sequence types into tuples.

Guess you like

Origin blog.csdn.net/michellechouu/article/details/50705235