加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

Python中数组用法大全

发布时间:2020-12-17 17:25:45 所属栏目:Python 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 #!/usr/bin/env python## [SNIPPET_NAME: Lists 101]# [SNIPPET_CATEGORIES: Python Core]# [SNIPPET_DESCRIPTION: Basic and not so basic list oper

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

#!/usr/bin/env python
#
# [SNIPPET_NAME: Lists 101]
# [SNIPPET_CATEGORIES: Python Core]
# [SNIPPET_DESCRIPTION: Basic and not so basic list operations]
# [SNIPPET_AUTHOR: Bruno Girin <[email?protected]>]
# [SNIPPET_LICENSE: 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
 

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读