• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

python 数组实用方法大全

python 水墨上仙 2678次浏览

python里的数组何等重要,不用多言,看完下面的代码,一定受益匪浅

#!/usr/bin/env python
#
# [代码名字: Lists 101]
# [代码分类: Python Core]
# [代码描述: Basic and not so basic list operations]
# [代码作者: Bruno Girin <brunogirin@gmail.com>]
# [代码协议: GPL]
# This snippet demonstrates how the basics on lists: how to create, add,
# remove items, get items or slices, sort, etc.
#
# First, let's create simple list
#
print "Create a simple list"
simpleList = ["Karmic", "Lucid", "Hardy", "Jaunty", "Intrepid"]
# print it
print simpleList
#
# Interrogate the list
#
print "\nInterrogate the list"
# get item 3:  lists start counting at 0 so it should be Jaunty
print simpleList[3]
# we can also get a slice
print simpleList[2:4]
# get the first three items
print simpleList[:3]
# or all items from number index 3 (which is the fourth item) to the end
print simpleList[3:]
# we can also take every other item, as a slice is defined
# like this: start:stop:step
print simpleList[::2]
# get the length of the list
print len(simpleList)
# we can get the index of an item in the list
print simpleList.index("Hardy")
# and when the list doesn't contain the item, we get an error that we can catch
try:
    print simpleList.index("Feisty")
except ValueError:
    print "The list doesn't contain the item Feisty"
#
# Modify the list
#
print "\nModify the list"
# add another item
simpleList.append("Twisty")
print simpleList
# oops! let's sort this out by replacing in place
simpleList[5] = "Gutsy"
print simpleList
# extend the list with another one
otherList = ["Edgy", "Breezy"]
simpleList.extend(otherList)
print simpleList
# remove an item from the list (Hardy should not be in the list anymore)
del simpleList[2]
print simpleList
# insert an item in the middle of the list
simpleList.insert(4, "Hardy")
print simpleList
# remove an item by its value rather than its index
simpleList.remove("Edgy")
print simpleList
#
# Create modified copies of the list
#
print "\nCreate modified copies of the list"
# sort it
print sorted(simpleList)
# join it to produce a custom print
print ' => '.join(sorted(simpleList))
# lists can contain the same item several times so if we add Lucid again:
simpleList.append("Lucid")
# we have it twice in the list (easier to see if we sort it)
print sorted(simpleList)
# but we can get round that by transforming it into a set, which is a list
# with no duplicates; and of course we can also sort the set
print sorted(set(simpleList))
#
# Iterate over the list
#
print "\nIterate over the list"
for i in simpleList:
    print i.upper()
# but if we want to create another list by applying the same expression to
# each item, we can use a list comprehension
upList = [i.upper() for i in sorted(set(simpleList))]
print upList

 


开心洋葱 , 版权所有丨如未注明 , 均为原创丨未经授权请勿修改 , 转载请注明python 数组实用方法大全
喜欢 (0)
加载中……