http://sahandsaba.com/thirty-python-language-features-and-tricks-you-may-not-know.html  感谢原作者

30 Python Language Features and Tricks You May Not Know About

Posted on Mar 05, 2014 , last modified on Mar 16, 2014

By Sahand Saba
 

1.1   Unpacking

  1. >>> a, b, c = 1, 2, 3
  2. >>> a, b, c
  3. (1, 2, 3)
  4. >>> a, b, c = [1, 2, 3]
  5. >>> a, b, c
  6. (1, 2, 3)
  7. >>> a, b, c = (2 * i + 1 for i in range(3))
  8. >>> a, b, c
  9. (1, 3, 5)
  10. >>> a, (b, c), d = [1, (2, 3), 4]
  11. >>> a
  12. 1
  13. >>> b
  14. 2
  15. >>> c
  16. 3
  17. >>> d
  18. 4

1.2   Unpacking for swapping variables

  1. >>> a, b = 1, 2
  2. >>> a, b = b, a
  3. >>> a, b
  4. (2, 1)

1.3   Extended unpacking (Python 3 only)

  1. >>> a, *b, c = [1, 2, 3, 4, 5]
  2. >>> a
  3. 1
  4. >>> b
  5. [2, 3, 4]
  6. >>> c
  7. 5

1.4   Negative indexing

  1. >>> a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
  2. >>> a[-1]
  3. 10
  4. >>> a[-3]
  5. 8

1.5   List slices (a[start:end])

  1. >>> a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
  2. >>> a[2:8]
  3. [2, 3, 4, 5, 6, 7]

1.6   List slices with negative indexing

  1. >>> a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
  2. >>> a[-4:-2]
  3. [7, 8]

1.7   List slices with step (a[start:end:step])

  1. >>> a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
  2. >>> a[::2]
  3. [0, 2, 4, 6, 8, 10]
  4. >>> a[::3]
  5. [0, 3, 6, 9]
  6. >>> a[2:8:2]
  7. [2, 4, 6]

1.8   List slices with negative step

  1. >>> a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
  2. >>> a[::-1]
  3. [10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
  4. >>> a[::-2]
  5. [10, 8, 6, 4, 2, 0]

1.9   List slice assignment

  1. >>> a = [1, 2, 3, 4, 5]
  2. >>> a[2:3] = [0, 0]
  3. >>> a
  4. [1, 2, 0, 0, 4, 5]
  5. >>> a[1:1] = [8, 9]
  6. >>> a
  7. [1, 8, 9, 2, 0, 0, 4, 5]
  8. >>> a[1:-1] = []
  9. >>> a
  10. [1, 5]

1.10   Naming slices (slice(start, end, step))

  1. >>> a = [0, 1, 2, 3, 4, 5]
  2. >>> LASTTHREE = slice(-3, None)
  3. >>> LASTTHREE
  4. slice(-3, None, None)
  5. >>> a[LASTTHREE]
  6. [3, 4, 5]

1.11   Zipping and unzipping lists and iterables

  1. >>> a = [1, 2, 3]
  2. >>> b = ['a', 'b', 'c']
  3. >>> z = zip(a, b)
  4. >>> z
  5. [(1, 'a'), (2, 'b'), (3, 'c')]
  6. >>> zip(*z)
  7. [(1, 2, 3), ('a', 'b', 'c')]

1.12   Grouping adjacent list items using zip

  1. >>> a = [1, 2, 3, 4, 5, 6]
  2. >>> zip(*([iter(a)] * 2))
  3. [(1, 2), (3, 4), (5, 6)]
  4.  
  5. >>> group_adjacent = lambda a, k: zip(*([iter(a)] * k))
  6. >>> group_adjacent(a, 3)
  7. [(1, 2, 3), (4, 5, 6)]
  8. >>> group_adjacent(a, 2)
  9. [(1, 2), (3, 4), (5, 6)]
  10. >>> group_adjacent(a, 1)
  11. [(1,), (2,), (3,), (4,), (5,), (6,)]
  12.  
  13. >>> zip(a[::2], a[1::2])
  14. [(1, 2), (3, 4), (5, 6)]
  15.  
  16. >>> zip(a[::3], a[1::3], a[2::3])
  17. [(1, 2, 3), (4, 5, 6)]
  18.  
  19. >>> group_adjacent = lambda a, k: zip(*(a[i::k] for i in range(k)))
  20. >>> group_adjacent(a, 3)
  21. [(1, 2, 3), (4, 5, 6)]
  22. >>> group_adjacent(a, 2)
  23. [(1, 2), (3, 4), (5, 6)]
  24. >>> group_adjacent(a, 1)
  25. [(1,), (2,), (3,), (4,), (5,), (6,)]

1.13   Inverting a dictionary using zip

  1. >>> m = {'a': 1, 'b': 2, 'c': 3, 'd': 4}
  2. >>> m.items()
  3. [('a', 1), ('c', 3), ('b', 2), ('d', 4)]
  4. >>> zip(m.values(), m.keys())
  5. [(1, 'a'), (3, 'c'), (2, 'b'), (4, 'd')]
  6. >>> mi = dict(zip(m.values(), m.keys()))
  7. >>> mi
  8. {1: 'a', 2: 'b', 3: 'c', 4: 'd'}

1.14   Flattening lists:

  1. >>> a = [[1, 2], [3, 4], [5, 6]]
  2. >>> list(itertools.chain.from_iterable(a))
  3. [1, 2, 3, 4, 5, 6]
  4.  
  5. >>> sum(a, [])
  6. [1, 2, 3, 4, 5, 6]
  7.  
  8. >>> [x for l in a for x in l]
  9. [1, 2, 3, 4, 5, 6]
  10.  
  11. >>> a = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]
  12. >>> [x for l1 in a for l2 in l1 for x in l2]
  13. [1, 2, 3, 4, 5, 6, 7, 8]
  14.  
  15. >>> a = [1, 2, [3, 4], [[5, 6], [7, 8]]]
  16. >>> flatten = lambda x: [y for l in x for y in flatten(l)] if type(x) is list else [x]
  17. >>> flatten(a)
  18. [1, 2, 3, 4, 5, 6, 7, 8]

Note: according to Python's documentation on sumitertools.chain.from_iterable is the preferred method for this.

1.15   Generator expressions

  1. >>> g = (x ** 2 for x in xrange(10))
  2. >>> next(g)
  3. 0
  4. >>> next(g)
  5. 1
  6. >>> next(g)
  7. 4
  8. >>> next(g)
  9. 9
  10. >>> sum(x ** 3 for x in xrange(10))
  11. 2025
  12. >>> sum(x ** 3 for x in xrange(10) if x % 3 == 1)
  13. 408

1.16   Dictionary comprehensions

  1. >>> m = {x: x ** 2 for x in range(5)}
  2. >>> m
  3. {0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
  4.  
  5. >>> m = {x: 'A' + str(x) for x in range(10)}
  6. >>> m
  7. {0: 'A0', 1: 'A1', 2: 'A2', 3: 'A3', 4: 'A4', 5: 'A5', 6: 'A6', 7: 'A7', 8: 'A8', 9: 'A9'}

1.17   Inverting a dictionary using a dictionary comprehension

  1. >>> m = {'a': 1, 'b': 2, 'c': 3, 'd': 4}
  2. >>> m
  3. {'d': 4, 'a': 1, 'b': 2, 'c': 3}
  4. >>> {v: k for k, v in m.items()}
  5. {1: 'a', 2: 'b', 3: 'c', 4: 'd'}

1.18   Named tuples (collections.namedtuple)

  1. >>> Point = collections.namedtuple('Point', ['x', 'y'])
  2. >>> p = Point(x=1.0, y=2.0)
  3. >>> p
  4. Point(x=1.0, y=2.0)
  5. >>> p.x
  6. 1.0
  7. >>> p.y
  8. 2.0

1.19   Inheriting from named tuples:

  1. >>> class Point(collections.namedtuple('PointBase', ['x', 'y'])):
  2. ... __slots__ = ()
  3. ... def __add__(self, other):
  4. ... return Point(x=self.x + other.x, y=self.y + other.y)
  5. ...
  6. >>> p = Point(x=1.0, y=2.0)
  7. >>> q = Point(x=2.0, y=3.0)
  8. >>> p + q
  9. Point(x=3.0, y=5.0)

1.20   Sets and set operations

  1. >>> A = {1, 2, 3, 3}
  2. >>> A
  3. set([1, 2, 3])
  4. >>> B = {3, 4, 5, 6, 7}
  5. >>> B
  6. set([3, 4, 5, 6, 7])
  7. >>> A | B
  8. set([1, 2, 3, 4, 5, 6, 7])
  9. >>> A & B
  10. set([3])
  11. >>> A - B
  12. set([1, 2])
  13. >>> B - A
  14. set([4, 5, 6, 7])
  15. >>> A ^ B
  16. set([1, 2, 4, 5, 6, 7])
  17. >>> (A ^ B) == ((A - B) | (B - A))
  18. True

1.21   Multisets and multiset operations (collections.Counter)

  1. >>> A = collections.Counter([1, 2, 2])
  2. >>> B = collections.Counter([2, 2, 3])
  3. >>> A
  4. Counter({2: 2, 1: 1})
  5. >>> B
  6. Counter({2: 2, 3: 1})
  7. >>> A | B
  8. Counter({2: 2, 1: 1, 3: 1})
  9. >>> A & B
  10. Counter({2: 2})
  11. >>> A + B
  12. Counter({2: 4, 1: 1, 3: 1})
  13. >>> A - B
  14. Counter({1: 1})
  15. >>> B - A
  16. Counter({3: 1})

1.22   Most common elements in an iterable (collections.Counter)

  1. >>> A = collections.Counter([1, 1, 2, 2, 3, 3, 3, 3, 4, 5, 6, 7])
  2. >>> A
  3. Counter({3: 4, 1: 2, 2: 2, 4: 1, 5: 1, 6: 1, 7: 1})
  4. >>> A.most_common(1)
  5. [(3, 4)]
  6. >>> A.most_common(3)
  7. [(3, 4), (1, 2), (2, 2)]

1.23   Double-ended queue (collections.deque)

  1. >>> Q = collections.deque()
  2. >>> Q.append(1)
  3. >>> Q.appendleft(2)
  4. >>> Q.extend([3, 4])
  5. >>> Q.extendleft([5, 6])
  6. >>> Q
  7. deque([6, 5, 2, 1, 3, 4])
  8. >>> Q.pop()
  9. 4
  10. >>> Q.popleft()
  11. 6
  12. >>> Q
  13. deque([5, 2, 1, 3])
  14. >>> Q.rotate(3)
  15. >>> Q
  16. deque([2, 1, 3, 5])
  17. >>> Q.rotate(-3)
  18. >>> Q
  19. deque([5, 2, 1, 3])

1.24   Double-ended queue with maximum length (collections.deque)

  1. >>> last_three = collections.deque(maxlen=3)
  2. >>> for i in xrange(10):
  3. ... last_three.append(i)
  4. ... print ', '.join(str(x) for x in last_three)
  5. ...
  6. 0
  7. 0, 1
  8. 0, 1, 2
  9. 1, 2, 3
  10. 2, 3, 4
  11. 3, 4, 5
  12. 4, 5, 6
  13. 5, 6, 7
  14. 6, 7, 8
  15. 7, 8, 9

1.25   Ordered dictionaries (collections.OrderedDict)

  1. >>> m = dict((str(x), x) for x in range(10))
  2. >>> print ', '.join(m.keys())
  3. 1, 0, 3, 2, 5, 4, 7, 6, 9, 8
  4. >>> m = collections.OrderedDict((str(x), x) for x in range(10))
  5. >>> print ', '.join(m.keys())
  6. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
  7. >>> m = collections.OrderedDict((str(x), x) for x in range(10, 0, -1))
  8. >>> print ', '.join(m.keys())
  9. 10, 9, 8, 7, 6, 5, 4, 3, 2, 1

1.26   Default dictionaries (collections.defaultdict)

  1. >>> m = dict()
  2. >>> m['a']
  3. Traceback (most recent call last):
  4. File "<stdin>", line 1, in <module>
  5. KeyError: 'a'
  6. >>>
  7. >>> m = collections.defaultdict(int)
  8. >>> m['a']
  9. 0
  10. >>> m['b']
  11. 0
  12. >>> m = collections.defaultdict(str)
  13. >>> m['a']
  14. ''
  15. >>> m['b'] += 'a'
  16. >>> m['b']
  17. 'a'
  18. >>> m = collections.defaultdict(lambda: '[default value]')
  19. >>> m['a']
  20. '[default value]'
  21. >>> m['b']
  22. '[default value]'

1.27   Using default dictionaries to represent simple trees

  1. >>> import json
  2. >>> tree = lambda: collections.defaultdict(tree)
  3. >>> root = tree()
  4. >>> root['menu']['id'] = 'file'
  5. >>> root['menu']['value'] = 'File'
  6. >>> root['menu']['menuitems']['new']['value'] = 'New'
  7. >>> root['menu']['menuitems']['new']['onclick'] = 'new();'
  8. >>> root['menu']['menuitems']['open']['value'] = 'Open'
  9. >>> root['menu']['menuitems']['open']['onclick'] = 'open();'
  10. >>> root['menu']['menuitems']['close']['value'] = 'Close'
  11. >>> root['menu']['menuitems']['close']['onclick'] = 'close();'
  12. >>> print json.dumps(root, sort_keys=True, indent=4, separators=(',', ': '))
  13. {
  14. "menu": {
  15. "id": "file",
  16. "menuitems": {
  17. "close": {
  18. "onclick": "close();",
  19. "value": "Close"
  20. },
  21. "new": {
  22. "onclick": "new();",
  23. "value": "New"
  24. },
  25. "open": {
  26. "onclick": "open();",
  27. "value": "Open"
  28. }
  29. },
  30. "value": "File"
  31. }
  32. }

(See https://gist.github.com/hrldcpr/2012250 for more on this.)

1.28   Mapping objects to unique counting numbers (collections.defaultdict)

  1. >>> import itertools, collections
  2. >>> value_to_numeric_map = collections.defaultdict(itertools.count().next)
  3. >>> value_to_numeric_map['a']
  4. 0
  5. >>> value_to_numeric_map['b']
  6. 1
  7. >>> value_to_numeric_map['c']
  8. 2
  9. >>> value_to_numeric_map['a']
  10. 0
  11. >>> value_to_numeric_map['b']
  12. 1

1.29   Largest and smallest elements (heapq.nlargest and heapq.nsmallest)

  1. >>> a = [random.randint(0, 100) for __ in xrange(100)]
  2. >>> heapq.nsmallest(5, a)
  3. [3, 3, 5, 6, 8]
  4. >>> heapq.nlargest(5, a)
  5. [100, 100, 99, 98, 98]

1.30   Cartesian products (itertools.product)

  1. >>> for p in itertools.product([1, 2, 3], [4, 5]):
  2. (1, 4)
  3. (1, 5)
  4. (2, 4)
  5. (2, 5)
  6. (3, 4)
  7. (3, 5)
  8. >>> for p in itertools.product([0, 1], repeat=4):
  9. ... print ''.join(str(x) for x in p)
  10. ...
  11. 0000
  12. 0001
  13. 0010
  14. 0011
  15. 0100
  16. 0101
  17. 0110
  18. 0111
  19. 1000
  20. 1001
  21. 1010
  22. 1011
  23. 1100
  24. 1101
  25. 1110
  26. 1111

1.31   Combinations and combinations with replacement (itertools.combinations anditertools.combinations_with_replacement)

  1. >>> for c in itertools.combinations([1, 2, 3, 4, 5], 3):
  2. ... print ''.join(str(x) for x in c)
  3. ...
  4. 123
  5. 124
  6. 125
  7. 134
  8. 135
  9. 145
  10. 234
  11. 235
  12. 245
  13. 345
  14. >>> for c in itertools.combinations_with_replacement([1, 2, 3], 2):
  15. ... print ''.join(str(x) for x in c)
  16. ...
  17. 11
  18. 12
  19. 13
  20. 22
  21. 23
  22. 33

1.32   Permutations (itertools.permutations)

  1. >>> for p in itertools.permutations([1, 2, 3, 4]):
  2. ... print ''.join(str(x) for x in p)
  3. ...
  4. 1234
  5. 1243
  6. 1324
  7. 1342
  8. 1423
  9. 1432
  10. 2134
  11. 2143
  12. 2314
  13. 2341
  14. 2413
  15. 2431
  16. 3124
  17. 3142
  18. 3214
  19. 3241
  20. 3412
  21. 3421
  22. 4123
  23. 4132
  24. 4213
  25. 4231
  26. 4312
  27. 4321

1.33   Chaining iterables (itertools.chain)

  1. >>> a = [1, 2, 3, 4]
  2. >>> for p in itertools.chain(itertools.combinations(a, 2), itertools.combinations(a, 3)):
  3. ... print p
  4. ...
  5. (1, 2)
  6. (1, 3)
  7. (1, 4)
  8. (2, 3)
  9. (2, 4)
  10. (3, 4)
  11. (1, 2, 3)
  12. (1, 2, 4)
  13. (1, 3, 4)
  14. (2, 3, 4)
  15. >>> for subset in itertools.chain.from_iterable(itertools.combinations(a, n) for n in range(len(a) + 1))
  16. ... print subset
  17. ...
  18. ()
  19. (1,)
  20. (2,)
  21. (3,)
  22. (4,)
  23. (1, 2)
  24. (1, 3)
  25. (1, 4)
  26. (2, 3)
  27. (2, 4)
  28. (3, 4)
  29. (1, 2, 3)
  30. (1, 2, 4)
  31. (1, 3, 4)
  32. (2, 3, 4)
  33. (1, 2, 3, 4)

1.34   Grouping rows by a given key (itertools.groupby)

  1. >>> import itertools
  2. >>> with open('contactlenses.csv', 'r') as infile:
  3. ... data = [line.strip().split(',') for line in infile]
  4. ...
  5. >>> data = data[1:]
  6. >>> def print_data(rows):
  7. ... print '\n'.join('\t'.join('{: <16}'.format(s) for s in row) for row in rows)
  8. ...
  9.  
  10. >>> print_data(data)
  11. young myope no reduced none
  12. young myope no normal soft
  13. young myope yes reduced none
  14. young myope yes normal hard
  15. young hypermetrope no reduced none
  16. young hypermetrope no normal soft
  17. young hypermetrope yes reduced none
  18. young hypermetrope yes normal hard
  19. pre-presbyopic myope no reduced none
  20. pre-presbyopic myope no normal soft
  21. pre-presbyopic myope yes reduced none
  22. pre-presbyopic myope yes normal hard
  23. pre-presbyopic hypermetrope no reduced none
  24. pre-presbyopic hypermetrope no normal soft
  25. pre-presbyopic hypermetrope yes reduced none
  26. pre-presbyopic hypermetrope yes normal none
  27. presbyopic myope no reduced none
  28. presbyopic myope no normal none
  29. presbyopic myope yes reduced none
  30. presbyopic myope yes normal hard
  31. presbyopic hypermetrope no reduced none
  32. presbyopic hypermetrope no normal soft
  33. presbyopic hypermetrope yes reduced none
  34. presbyopic hypermetrope yes normal none
  35.  
  36. >>> data.sort(key=lambda r: r[-1])
  37. >>> for value, group in itertools.groupby(data, lambda r: r[-1]):
  38. ... print '-----------'
  39. ... print 'Group: ' + value
  40. ... print_data(group)
  41. ...
  42. -----------
  43. Group: hard
  44. young myope yes normal hard
  45. young hypermetrope yes normal hard
  46. pre-presbyopic myope yes normal hard
  47. presbyopic myope yes normal hard
  48. -----------
  49. Group: none
  50. young myope no reduced none
  51. young myope yes reduced none
  52. young hypermetrope no reduced none
  53. young hypermetrope yes reduced none
  54. pre-presbyopic myope no reduced none
  55. pre-presbyopic myope yes reduced none
  56. pre-presbyopic hypermetrope no reduced none
  57. pre-presbyopic hypermetrope yes reduced none
  58. pre-presbyopic hypermetrope yes normal none
  59. presbyopic myope no reduced none
  60. presbyopic myope no normal none
  61. presbyopic myope yes reduced none
  62. presbyopic hypermetrope no reduced none
  63. presbyopic hypermetrope yes reduced none
  64. presbyopic hypermetrope yes normal none
  65. -----------
  66. Group: soft
  67. young myope no normal soft
  68. young hypermetrope no normal soft
  69. pre-presbyopic myope no normal soft
  70. pre-presbyopic hypermetrope no normal soft
  71. presbyopic hypermetrope no normal soft
 

[ZZ] python 语言技巧的更多相关文章

  1. python语言技巧

    一 在写之前 最好指定python的路径: #!/usr/bin/python python 在linux中需要添加编码方式:以免出现中文乱码 # -*- coding: UTF-8 –*-   二 ...

  2. Python语言防坑小技巧

    Python语言防坑小技巧 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.赋值即定义  1>.运行以下代码会出现报错 #!/usr/bin/env python #_*_ ...

  3. 如何系统地自学一门Python 语言(转)

    转自:http://www.phpxs.com/post/4521 零基础情况下,学一门语言充实下自己,Python,简洁.优美.容易使用,是一个很好的选择.那么如何系统地自学Python呢? 有的人 ...

  4. Python语言and-or的用法

    [原]python语言的 and-or 常常被用来实现类C语言中的三元运算符 : ?   , 更为骚气的写法是  xxx and xxx or xxx and xxx or xxx,这样就可以可以做到 ...

  5. python代码优化技巧

    转自:http://www.douban.com/group/topic/31478102/ 这个资料库还有些不错的好文章: http://www.ibm.com/developerworks/cn/ ...

  6. Python语言学习之Python入门到进阶

    人们常说Python语言简单,编写简单程序时好像也确实如此.但实际上Python绝不简单,它也是一种很复杂的语言,其功能特征非常丰富,能支持多种编程风格,在几乎所有方面都能深度定制.要想用好Pytho ...

  7. python的技巧和方法你了解多少?

    学了这些你的python代码将会改善与你的技巧将会提高. 1. 路径操作 比起os模块的path方法,python3标准库的pathlib模块的Path处理起路径更加的容易. 获取当前文件路径 前提导 ...

  8. 一些你需要知道的Python代码技巧

    被人工智能捧红的 Python 已是一种发展完善且非常多样化的语言,其中肯定有一些你尚未发现的功能.本文或许能够让你学到一些新技巧.   Python 是世界上最流行.热门的编程语言之一,原因很多,比 ...

  9. 掌握这些Python代码技巧,编程至少快一半!

    被人工智能捧红的 Python 已是一种发展完善且非常多样化的语言,其中肯定有一些你尚未发现的功能.本文或许能够让你学到一些新技巧. ​ Python 是世界上最流行.热门的编程语言之一,原因很多,比 ...

随机推荐

  1. C++习题 复数类--重载运算符2+

    Description 定义一个复数类Complex,重载运算符"+",使之能用于复数的加法运算.参加运算的两个运算量可以都是类对象,也可以其中有一个是整数,顺序任意.例如,c1+ ...

  2. Eclipse+Maven命令创建webapp项目<三>

    1.使用maven命令:mvn archetype:create -DgroupId=xxxxx -DartifactId=web-sample -DarchetypeArtifactId=maven ...

  3. UVA 674 (入门DP, 14.07.09)

     Coin Change  Suppose there are 5 types of coins: 50-cent, 25-cent, 10-cent, 5-cent, and 1-cent. We ...

  4. Thinkphp编辑器扩展类kindeditor用法

    一, 使用前的准备. 使用前请确认你已经建立好了一个Thinkphp站点项目. 1,Keditor.class.php和JSON.class.php 是编辑器扩展类文件,将他们拷贝到你的站点项目的Th ...

  5. Android MVC MVP

    从.NET的宠物商店到Android MVC MVP   1 一些闲话 记得刚进公司的时候,我们除了做常规的Training Project外,每天还要上课,接受各种技术培训和公司业务介绍.当时第一次 ...

  6. jQuery整理笔记2----jQuery选择整理

    一个.基本的选择 1.ID选择器 JavaScript提供了原生方法实如今DOM中选择指定ID值得元素. 使用方法例如以下: var element=document.getElementById(& ...

  7. Lua中的weak表——weak table(转)

    弱表(weak table)是一个很有意思的东西,像C++/Java等语言是没有的.弱表的定义是:A weak table is a table whose elements are weak ref ...

  8. android得知----overridePendingTransition

    1 Activity动画是指从一个切换activity跳到另一个activity随着电影. 它由两部分组成:第一部分是一个activity动画出口:中的第二个另一部分activity动画被访问: 于A ...

  9. 【Android进阶】判断网络连接状态并自动界面跳转

    用于判断软件打开时的网络连接状态,若无网络连接,提醒用户跳转到设置界面 /** * 设置在onStart()方法里面,可以在界面每次获得焦点的时候都进行检测 */ @Override protecte ...

  10. POST和Get辨析

    在Form里面,能够使用post也能够使用get.它们都是method的合法取值,可是两者也有不同,主要差别在于传递和获取參数的方式不同 一.Get方法: 1.參数的传递方式: 通过URL请求来传递用 ...