[Python] python中数组用法大全 →→→→→进入此内容的聊天室

来自 , 2020-05-10, 写在 Python, 查看 167 次.
URL http://www.code666.cn/view/67b878df
  1. #!/usr/bin/env python
  2. #
  3. # [SNIPPET_NAME: Lists 101]
  4. # [SNIPPET_CATEGORIES: Python Core]
  5. # [SNIPPET_DESCRIPTION: Basic and not so basic list operations]
  6. # [SNIPPET_AUTHOR: Bruno Girin <brunogirin@gmail.com>]
  7. # [SNIPPET_LICENSE: GPL]
  8.  
  9. # This snippet demonstrates how the basics on lists: how to create, add,
  10. # remove items, get items or slices, sort, etc.
  11.  
  12. #
  13. # First, let's create simple list
  14. #
  15. print "Create a simple list"
  16. simpleList = ["Karmic", "Lucid", "Hardy", "Jaunty", "Intrepid"]
  17. # print it
  18. print simpleList
  19. #
  20. # Interrogate the list
  21. #
  22. print "\nInterrogate the list"
  23. # get item 3:  lists start counting at 0 so it should be Jaunty
  24. print simpleList[3]
  25. # we can also get a slice
  26. print simpleList[2:4]
  27. # get the first three items
  28. print simpleList[:3]
  29. # or all items from number index 3 (which is the fourth item) to the end
  30. print simpleList[3:]
  31. # we can also take every other item, as a slice is defined
  32. # like this: start:stop:step
  33. print simpleList[::2]
  34. # get the length of the list
  35. print len(simpleList)
  36. # we can get the index of an item in the list
  37. print simpleList.index("Hardy")
  38. # and when the list doesn't contain the item, we get an error that we can catch
  39. try:
  40.     print simpleList.index("Feisty")
  41. except ValueError:
  42.     print "The list doesn't contain the item Feisty"
  43. #
  44. # Modify the list
  45. #
  46. print "\nModify the list"
  47. # add another item
  48. simpleList.append("Twisty")
  49. print simpleList
  50. # oops! let's sort this out by replacing in place
  51. simpleList[5] = "Gutsy"
  52. print simpleList
  53. # extend the list with another one
  54. otherList = ["Edgy", "Breezy"]
  55. simpleList.extend(otherList)
  56. print simpleList
  57. # remove an item from the list (Hardy should not be in the list anymore)
  58. del simpleList[2]
  59. print simpleList
  60. # insert an item in the middle of the list
  61. simpleList.insert(4, "Hardy")
  62. print simpleList
  63. # remove an item by its value rather than its index
  64. simpleList.remove("Edgy")
  65. print simpleList
  66. #
  67. # Create modified copies of the list
  68. #
  69. print "\nCreate modified copies of the list"
  70. # sort it
  71. print sorted(simpleList)
  72. # join it to produce a custom print
  73. print ' => '.join(sorted(simpleList))
  74. # lists can contain the same item several times so if we add Lucid again:
  75. simpleList.append("Lucid")
  76. # we have it twice in the list (easier to see if we sort it)
  77. print sorted(simpleList)
  78. # but we can get round that by transforming it into a set, which is a list
  79. # with no duplicates; and of course we can also sort the set
  80. print sorted(set(simpleList))
  81. #
  82. # Iterate over the list
  83. #
  84. print "\nIterate over the list"
  85. for i in simpleList:
  86.     print i.upper()
  87. # but if we want to create another list by applying the same expression to
  88. # each item, we can use a list comprehension
  89. upList = [i.upper() for i in sorted(set(simpleList))]
  90. print upList
  91. #//python/2370

回复 "python中数组用法大全"

这儿你可以回复上面这条便签

captcha