python中for _ in range(10)與for i in range(10)有何區別
下劃線表示 臨時變數, 僅用一次,後面無需再用到
列表:
import random
# 生成一個隨機數列表
#方法一:
# l =
# for i in range(10):
# print(l)
#列表生成式,迴圈十次,要生成的列表的內容放最前邊。
data = [random.randint(-10,10) for i in range(10)]
print(data)
# filter內建函式過濾
# ret = filter(lambda x:x>=0,data) # 返回一個物件
# for i in ret:
# print(i)
# filter()函式接收一個函式 f 和一個list,這個函式 f 的作用是對每個元素
# 進行判斷,返回 true或 false,filter()根據判斷結果自動過濾掉不符合條件
# 的元素,返回由符合條件元素組成的新list。
# 列表推導式
l1 = [i for i in data if i > 0]
print(l1)

字典:
# 隨機生成一個字典
d =
print(d)
# 字典生成式
dic =
print(dic)
集合:
data = [random.randint(-10,10) for _ in range(10)]
s = set(data)
print(s)
s1 =
print(s1)
# 方法一 變數定義
name,age,male,email = range(4)
# print(name)
# 方法二
from collections import namedtuple
#namedtuple('名稱', [屬性list])
from collections import namedtuple
point = namedtuple('point', ['x', 'y'])
print(point) # p = point(1, 2)
print(p.x)
# 生成一個隨機列表
from random import randint
# l = [randint(1,11) for _ in range(20)]
# print(l)
## d = dict.fromkeys(l,0)
# # print(d)
## for i in l:
# d[i] += 1
# print(d)
# # 根據字典的值進行排序
# 第一種方法
import random
# d =
# print(d) #
# 從小到大排序.總體思路:sorted函式可以對列表進行從小到大排序,對於字典{}
# dict,sorted函式只比較dict的key進行排序,所以要對dict進行調整變形。
# ret = sorted(d)
# print(ret) # [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# z=zip(d.values(),d.keys()) # z 是可迭代物件
# print(list(z))
# z = list(z)
# 在 python 3.x 中為了減少記憶體,
# zip() 返回的是一個物件。如需展示列表,需手動 list() 轉換。
# ret = sorted(z,reverse=true)
# print(ret)
# 第二種方法
# [(a1,b1,c1),(a2,b2,c2),(a3,b3,c3),(a4,b4,c4)……]
# sorted函式可以對這種集合進行指定元素排序。
# sorted(d.items(),key=lambda x:x[1]),第一個從引數是需要排序的列表,
# 第二個引數是指定key(列表中每一項的第幾個元素)來進行排序。
# print(d.items())
## ret = sorted(d.items(),key=lambda x:x[1],reverse=true)
# print(ret)
# 第三種方法
# 利用collections的子類counter從大到小排序
from collections import counter
d =
ret = counter(d).most_common()
print(ret)
ret = counter(d).most_common(3)
print(ret)
import random
from collections import counter
l = [random.randint(1,11) for _ in range(1,30)]
print(l) # [5, 9, 11, 11, 2, 8, 11, 2, 3, 5, 4, 7, 9, 9, 5, 11, 10, 6, 8, 11, 6, 11, 3, 11, 3, 11, 11, 5, 9]
obj = counter(l)
print(obj) # counter()
print(obj.most_common(3)) # [(11, 9), (5, 4), (9, 4)]
英文詞頻的統計
import re
from collections import counter
data = open('name').read()
l = re.split('\w+',data)
print(l)
c = counter(l)
print(c.most_common(5))
如何快速找到多個字典中的公共鍵?
from random import randint,sample
name = ['hou','liu','cluo','meixi','wu','alex']
# print(sample(name,randint(3,6)))
s1 =
print(s1)
s2 =
print(s2)
s3 =
print(s3)
ret = s1.keys() &s2.keys() &s3.keys()
print(ret)
多個字典:
l = [s1,s2,s3]
l3 = l[-1].keys()
l2 = [l3]
s_and = ['none',]
while l:
s = l.pop()
print(s.keys())
s_and[-1] = s.keys() & l2[-1]
l2[-1] = s.keys()
print(s_and)
使用deque,它是一個雙端迴圈佇列
程式退出時候,可以使用pickle將佇列物件存入檔案,再次執行程式是將其匯入。
Yaconf 1 1 40 速度提升版
yaconf是一個高效能的php配置容器, 它在php啟動的時候把格式為ini的配置檔案parse後儲存在php的常駐記憶體中,不需要每次請求的時候都來分析配置檔案,並且在獲取配置項的時候 zero copy ,具體的介紹可以看我2015年釋出的時候寫的介紹 yaconf 一個高效能的配置管理擴充套件。 ...
python 1 1 語法
例如 example a 1 2 3 4 5 1 去掉最後一個陣列元素 及為1 2 3 4 1 指從最後一個開始反向遍歷 即5 4 3 2 1 此外還有幾種用法 n 去掉最後的n個字元 1 去掉最後一個元素 i j 指取從i到j 1的陣列元素 i 1 指從第一個字元到第i個字元反向遍歷 應用 迴文字...
python day 1 homework 1
作業一要求 1 輸入使用者名稱密碼 2 認證成功後顯示歡迎資訊 3 輸錯三次後鎖定 importos 生成儲存使用者資訊的字典 d userinfo 儲存使用者登入字典 input login username 儲存鎖定使用者列表 lock user 從userinfo中獲取使用者登入資訊 使用者名稱稱 口令 us...