python实例 列表
#!?/usr/bin/python
#?-*-?coding:?utf8?-*-
#列表類似Javascript的數組,方便易用
#定義元組
word=['a','b','c','d','e','f','g']
#如何通過索引訪問元組里的元素
a=word[2]
print?("a?is:?"+a)
b=word[1:3]
print?("b?is:?")
print?(b)?#?index?1?and?2?elements?of?word.
c=word[:2]
print?("c?is:?")
print?(c)?#?index?0?and?1?elements?of?word.
d=word[0:]
print?("d?is:?")
print?(d)?#?All?elements?of?word.
#元組可以合并
e=word[:2]+word[2:]
print?("e?is:?")
print?(e)?#?All?elements?of?word.
f=word[-1]
print?("f?is:?")
print?(f)?#?The?last?elements?of?word.
g=word[-4:-2]
print?("g?is:?")
print?(g)?#?index?3?and?4?elements?of?word.
h=word[-2:]
print?("h?is:?")
print?(h)?#?The?last?two?elements.
i=word[:-2]
print?("i?is:?")
print?(i)?#?Everything?except?the?last?two?characters
l=len(word)
print?("Length?of?word?is:?"+?str(l))
print?("Adds?new?element")
word.append('h')
print?(word)
#刪除元素
del?word[0]
print?(word)
del?word[1:3]
print?(word)
'''
知識點:
????*?列表長度是動態的,可任意添加刪除元素.
????*?用索引可以很方便訪問元素,甚至返回一個子列表
????*?更多方法請參考Python的文檔
'''
轉載于:https://www.cnblogs.com/fanweisheng/p/11243842.html
總結
以上是生活随笔為你收集整理的python实例 列表的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 支付宝瓜分9亿能得多少
- 下一篇: java读取Oracle中大字段数据(C