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

Python中数组用法大全

发布时间:2020-05-24 23:40:13 所属栏目:Python 来源:互联网
导读:Python中数组用法大全

下面是脚本之家 jb51.cc 通过网络收集整理的代码片段。

脚本之家小编现在分享给大家,也给大家做个参考。

#!/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 <[emailprotected]>]
# [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
 

以上是脚本之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

如果觉得脚本之家网站内容还不错,欢迎将脚本之家网站推荐给程序员好友。

(编辑:安卓应用网)

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

    推荐文章
      热点阅读