Python note

0. Object and Value
>>> a = 'banana'
>>> b = 'banana'
>>> a is b
True

In this example, Python only created one string object, and both a and b refer to it.
  >>> a = [1, 2, 3]
  >>> b = [1, 2, 3]
  >>> a is b
  False

  In this case we would say that the two lists are equivalent, because they have the same elements, but not identical, because they are not the same object. If two objects are identical, they are also equivalent, but if they are equivalent, they are not necessarily identical.

>>> a = [1, 2, 3]
>>> b = a
>>> b is a
True
>>> b.append(4)
>>> a is b
True
>>> print a
[1, 2, 3, 4]


1.
>>> eng2sp = dict()
>>> eng2sp['one'] = 'uno'
>>> vals = eng2sp.values()
>>> 'uno' in vals
True

The in operator uses different algorithms for lists and dictionaries. For lists, it uses a search algorithm. As the list gets longer, the search time gets longer in direct proportion. For dictionaries, Python uses an algorithm called a hashtable that has a re-markable property: the in operator takes about the same amount of time no matter how
many items there are in a dictionary.

猜你喜欢

转载自fengjiangtao.iteye.com/blog/2211016