Python(五)列表
數組
數組存儲的是同一類型的一串信息
列表
一、列表的定義
??定義一個空列表
list?=?[]
??定義一個包含元素的列表,元素可以是任意類型,包括數值類型,列表,元組,字符串等等均可。
賦值方式定義:
list?=?["fentiao",?4,?'gender']
list1?=?['fentiao',(4,'male')]
工廠函數定義:
n?=?list("hello")
In?[2]:?n=list("hello")
In?[3]:?print?n
['h',?'e',?'l',?'l',?'o']
二、支持索引、切片、拼接、重復、成員操作符
索引、切片:
In?[4]:?li=[1,1.0,True,'hello',1+4j,[1,2,"hello"]]
In?[5]:?li[0]
Out[5]:?1
In?[6]:?li[-1]
Out[6]:?[1,?2,?'hello']
In?[7]:?li[:]
Out[7]:?[1,?1.0,?True,?'hello',?(1+4j),?[1,?2,?'hello']]
In?[8]:?li[1:]
Out[8]:?[1.0,?True,?'hello',?(1+4j),?[1,?2,?'hello']]
In?[9]:?li[0:2]
Out[9]:?[1,?1.0]
In?[10]:?li[::-1]
Out[10]:?[[1,?2,?'hello'],?(1+4j),?'hello',?True,?1.0,?1]
拼接:
In?[18]:?li1=['vsftpd','apache']
In?[19]:?li2=['mariadb','nfs']
In?[20]:?li1?+?li2
Out[20]:?['vsftpd',?'apache',?'mariadb',?'nfs']
重復:
In?[21]:?li1=['vsftpd','apache']
In?[22]:?li1*2
Out[22]:?['vsftpd',?'apache',?'vsftpd',?'apache']
成員操作符:
In?[23]:?li1=['vsftpd','apache']
In?[24]:?'vsftpd'?in?li1
Out[24]:?True
In?[25]:?'vsftpd'?not?in?li1
Out[25]:?False
題目1:
查看1-10號主機的21,22,3306,80,69端口
解答:
#!/usr/bin/env?python
#?coding:utf-8
ports?=?[21,22,3306,80,69]
for?i?in?range(1,11):
????for?port?in?ports:???#可以通過?for?i?in?list進行遍歷列表中的各個元素
????????ip?=?'172.25.254.'+str(i)
????????print?"[+]?Listening?On:%s:%d"?%(ip,port)
@font-face { ? font-family: "Times New Roman"; }@font-face { ? font-family: "宋體"; }p.MsoNormal { margin: 0pt 0pt 0.0001pt; text-align: justify; font-family: 'Times New Roman'; font-size: 10.5pt; }p.p { margin: 5pt 0pt; text-align: left; font-family: 'Times New Roman'; font-size: 12pt; }span.msoIns { text-decoration: underline; color: blue; }span.msoDel { text-decoration: line-through; color: red; }div.Section0 { page: Section0; }
三、列表的常用方法
1.更新列表
? append(增加一個元素)
? extend(可以增加多個元素,可以在括號中給出一個列表,這個列表中的元素會倒入到原列表,成為他的元素)
#可以看到同樣增加一個列表,append把它當成一個元素增加進去,而extend把它當作兩個元素加了進去,達到了一次性增加多個元素的目的
如果增加一個字符,使用append表示增加這個字符串,而extend表示這個字符串的每個字母作為一個元素增加進去:
In?[52]:?li1
Out[52]:?['vsftpd',?'apache']
In?[53]:?li1.append('hello')
In?[54]:?li1
Out[54]:?['vsftpd',?'apache',?'hello']
In?[55]:?li1.extend('hello')
In?[56]:?li1
Out[56]:?['vsftpd',?'apache',?'hello',?'h',?'e',?'l',?'l',?'o']
? 在指定位置添加元素使用inert方法;
l.insert(index, object)
? 修改列表的元素:直接重新賦值;
2.查看列表
? 查看某個列表元素的下表用index方法;
? 查看某個列表元素出現的次數用count方法;
3.刪除列表
remove
pop
li.pop()表示刪除最后一個元素
li.pop(0)表示刪除第一個元素
del
#直接刪除這個列表
題目2:
??? 1. 用戶名和密碼分別保存在列表中;
?? ?2. 用戶登錄時,判斷該用戶是否注冊;
?? ?2. 用戶登錄時,為防止***暴力破解, 僅有三次機會;
?? ?3. 如果登錄成功,顯示登錄成功(exit(), break).
解答:
#!/usr/bin/env python
#coding: utf-8
users = ["user1", "user2", "user3"]
passwords = ["123", "456", "789"]
i? = 0
while i < 3:
?? ?name = raw_input("請輸入用戶名:")
?? ?if name not in users:
?? ??? ?print "用戶未注冊"
?? ??? ?break
?? ?password = raw_input("請輸入密碼:")
?? ?i += 1
?? ?index = users.index(name)
?? ?if password == passwords[index]:
?? ???????? print "恭喜登錄成功"
?? ???????? break
?? ?else:
?? ???????? print "請輸入正確的用戶名或密碼!"
else:
?? ?print "已登錄三次,請稍后再試"
題目3:
打印棧的過程
解答:
#!/usr/bin/env python
#coding:utf-8
"""
列表有容器和可變的特性,通過列表構建其他數據類型;
1. 棧(eg: 往箱子里面放書)
棧的工作方式后進先出(LIFO);
2. 隊列(eg:飯堂排隊買飯)
隊列的工作方式先進先出(FIFO)?
"""
stack = []
info =? """
?? ??? ? ?? ??? ?棧操作
?? ?1. 出棧
?? ?2. 入棧
?? ?3. 查看棧元素
?? ?4. 退出
"""
print info
while 1:
?? ?choice = raw_input("Choice:").strip()
?? ?
?? ?if choice == '1':
?? ??? ?if not stack == []:
?? ??? ??? ?stack.pop()
?? ??? ?else:
?? ??? ??? ?print "棧為空"
?? ?elif choice == '2':
?? ??? ?value = raw_input("請輸入入棧的值:").strip()
?? ??? ?stack.append(value)
?? ?elif choice == '3':
?? ??? ?print "棧元素:",
?? ??? ?for i in stack:
?? ??? ??? ?print i,
?? ??? ?print
?? ?elif choice == '4':
?? ??? ?exit()
?? ?else:
?? ??? ?print "not valid choice"
轉載于:https://blog.51cto.com/13548905/2054517
總結
以上是生活随笔為你收集整理的Python(五)列表的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 关于ueditor多图上传加水印
- 下一篇: 企业需要建设网站该如何咨询价格呢?