1、key值迭代
d = dict(a=1,b=2)
for i in d:
print(i)
2、key->value值迭代
for i in d:
print("%s -> %s" %(i,d[i]))
3、key ->value迭代
for key,value in d.items():
print('%s -> %s' %(key,value))
需要匯入iterable模組
from collections import iterable ##匯入模組
print(isinstance(1,int)) ##判斷1是否是整形
print(isinstance(1,iterable)) ##判斷整形是否可迭代
print(isinstance(1.02,iterable)) ##判斷浮點型是否可迭代
print(isinstance(2e-9,iterable)) ##判斷浮點型是否可迭代
print(isinstance(true,iterable)) ##判斷bool型是否可迭代
print(isinstance('hello',iterable)) ##判斷字串是否可迭代
print(isinstance(,iterable))#判斷字典是否可迭代
for index,value in enumerate('hello'): ##enumerate 索引,講hello字串的索引值和元素對應列印
print(index,value)
1、傳統方式生成:
import random
ips =
for i in range(100):
ip = '172.25.254.'+ str(random.randint(1,255))
print(ips)
print(len(ips))
2.列表生成式:
import random
li = [ '172.25.254.'+str(random.randint(1,255)) for i in range(100)]
print(li)
print(len(li))
def is_div_therr(num):
return num%3 ==0
print([num**2 for num in range(1,101) if is_div_therr(num)])
1、顯示列表,1-1–只見的所有數求平方
print([i**2 for i in range(1,1011)])
2、找出/var/log/目錄中所有以.log結尾的檔案
import os
print([ filename for filename in os.listdir('/var/log') if filename.endswith('.log')])
3、找出/etc/目錄中的所有以.conf結尾的檔案,並把檔名轉化為大寫的檔名
print([ filename.upper() for filename in os.listdir('/etc/') if filename.endswith('.conf')])
4、判斷10以內的所有質數,並找出質數相加等於10的質數對
def is_prime(num):
if num<=0:
return false
elif num==1 and num ==2:
return true
else:
for i in range(2,num):
if num%i ==0:
return false
else:
return true
n = 10
primes_list = [ num for num in range(10) if is_prime(num)]
primes_pair = [(num1,n-num1) for num1 in primes_list if n - num1 in primes_list and num1<= n-num1]
print(len(primes_pair))
print(primes_pair)
print()
d = dict(a=6,b=2,c=3,e=6)
print()
service =
需求1、將字典裡面的key值變大寫
##傳統方法:
new_service = {}
for key,value in service.items():
new_service[key.upper()] = value
print(new_service)
##字典生成式:
print()
需求2、key值變成大寫, value值在原有基礎上加1
print()
大小寫合併,統一以小寫key值輸出
d = dict(a=2,b=10,b=4,e=1,a=1)
print(
li = [ i for i in range(100) if i%2 ==0]
##生成器
g = (i for i in range(100) if i%2 ==0)
python3中 g.__next__方法(), python2.x中g.next()方法;
print(g.__next__())
for i in g:
print(i)
g1 = (i for i in range(3))
while true:
try:
print(g1.__next__())
except stopiteration:
break
fib是值後乙個值是前兩個值相加:1,1,2,3,5,8…
def fib(num): # num=5
a, b, count = 0, 1, 1 # a=0, b=1
while count <= num:
yield b # 1
a, b = b, a + b # a=1, b=1
count += 1
g = fib(10) # 如果函式中有yield關鍵字, 返回乙個生成器物件;目前不執行函式內容;
print(g.__next__()) # 當遇到g.__next__方法時, 執行函式, 當遇到yield停止執行; print(g.__next__())時yield後面的值;
print(g.__next__()) # 再次呼叫g.__next__方法時, 從上次停止的地方繼續執行, 遇到yield停止;
g.close() # 關閉生成器
#全部列印
for i in g:
print(i)
def chat_robot():
res = ''
while true:
received = yield res
if 'age' in received or '年齡' in received:
res = '年齡保密'
elif 'name' in received or '姓名' in received:
res = '我是小冰'
elif 'money' in received or '錢' in received:
res = '沒錢'
else:
res = '我不知道你在說什麼,請換種方法'
def main():
robot = chat_robot()
robot.__next__()
while true:
send_data = input("my>>")
if send_data=='q' or send_data == 'quit':
print('我也要休息了')
break
robot_data = robot.send(send_data)
print('小冰》',robot_data)
main()
Python高階特性
l csx sarah tracy bob jack print l 0 l 1 l 2 輸出 csx sarah tracy l 0 3 輸出 csx sarah tracy 如果第乙個索引是0,還可以省略 print l 2 輸出 bob jack print l 2 1 輸出 bob d fo...
Python高階特性
python 高階特性 from collections import iterable from collections import iterator 切片 list切片 l list range 100 print l 0 3 取前3個元素 print l 3 取前3個元素 print l 4...
Python高階特性
切片 slice 非常靈活,體現了python的簡便性 1.取乙個list和tuple的部分元素時的簡化操作 l michael sarah tracy bob jack l 0 3 michael sarah tracy 從0開始不包括3,第乙個索引是0可省略,也支援負數按倒數的順序取值,什麼都不...