1.列表快速生成
cubes = [ x ** 3 + 100 for x in range(1,11)] #列表推导 2的3次方 + 100
print(cubes)
matrix =[ [0]*8 ] * 10 #[0]*8 将[0]里的元素重复8次,生成一个新列表,[ [0]*8 ] * 10是将列表重复10次
print(matrix)
matrix =[[r*c for c in range(8)] for r in range(10)] # for r in range(10) -> (0循环到9)
print(matrix) #生成8行10列
values = [x+y for x in "abc" for y in "0123"]
print(values)
==等同==
values = []
for x in "abc":
for y in "0123":
values.append(x+y)
print(values)
2.列表运算
lans = ["AAA","BBB","CCC"]
print(["SSS","XXX"] + lans)
print([1,2,3] * 2)
print(["ZZZ","EEE"] * 2)
3.查询序列是否包含指定元素
print("a" in "wderdf","eac" in "teacher")
print("aw" not in "reawer")
4.成员函数
dummy = ["ccc","hen","huiy","ccc","sad","ccc"]
print("出现了 %d 次 ccc 在列表里"%(dummy.count("ccc")))
dummy.clear()
print("列表清理之后:",dummy) #清理为 空
dummy.append("ccc") #添加
print(dummy)
dummy.extend(["huiy","ccc"])
print(dummy) #扩展
dummy.index("huiy")
print(dummy) #下标
dummy.remove("ccc")
print(dummy) #移除,但只移除第一个
5.列表复制
names = ["saca","det","kom","humm","dplk"]
namesAssigned = names
namesCopy = names.copy()
namesCopy[1] = "DET"
print("id:",id(names),"names:",names)
print("id:",id(namesAssigned),"names:",namesAssigned) #id 地址相同
print("id:",id(namesCopy),"names:",namesCopy) #id地址值不同,元素不会同时改变
6.切片
numbers = [x for x in range(10)]
print("numbers:",numbers)
print("number[3:9]:",numbers[3:9]) #从3——8 (x<9)
print("number[3:]:",numbers[3:]) #从3到最后一个元素
print("number[:9]:",numbers[:9]) #从第一个元素到9,不包含9
print("number[-6:-1]:",numbers[-6:-1]) #等同于从4——9
print("number[1:9:2]:",numbers[1:9:2]) #1到9(不含9)间隔2
print("number[-1:1:-2]:",numbers[-1:1:-2]) #等同于9——1,间隔-2,不包含1
numbers = [x for x in range(10)]
print("number[::1]:",numbers[::1]) #完整输出,x位置可加0
print("number[::-1]:",numbers[::-1]) #反序输出,y位置可加0
7.判断生肖
def myZodiac(year):
zod = ["monkey","rooster","dog","pig","rat","ox","tiger","rabbir","dragon","snake","hours","sheep"]
return zod[year % 12] #能否被12整除,返回为对应下标,mokey为0
def main():
y = eval(input("请输入出生年份:"))
print("你的生肖是:",myZodiac(y))
main() #执行程序
8. 随机抓扑克牌
扑克牌有54张牌,其中52张是正牌,另2张是副牌(大王和小王)。52张正牌又均分为
13张1组,共有黑桃、红桃、梅花、方块四组。每组花色的牌包括从2-10以及A、J Q. K标.
示的1 3张牌。在比较流行的斗地主游戏中,三个玩家中除庄家外,另外两个玩家一人要拿17
张牌。对于玩家而言,拿到哪十七张牌是完全随机的。
import random #随机数模块
suits = ["spades","Hearts","Diamonds","clubs"] #四种花色
ranks = ["A","2","3","4","5","6","7","8","9","10","J","Q","K"]
cards = [x + y for x in suits for y in ranks] + ["Red Joker","Black Joker"]
cardsHold = [] #获得的手牌列表
for i in range(17):
idx = random.randint(0,len(cards)-1) #获得随机整数,范围为(0——列表-1)
c = cards.pop(idx) #弹出对应下标的元素
cardsHold.append(c) #弹出的元素加入 列表
print(cardsHold)
第二种:
import random #随机数模块
suits = ["spades","Hearts","Diamonds","clubs"] #四种花色
ranks = ["A","2","3","4","5","6","7","8","9","10","J","Q","K"]
cards = [x + y for x in suits for y in ranks] + ["Red Joker","Black Joker"]
cardsHold = [] #获得的手牌列表
random.shuffle(cards) #shuffle(打乱洗牌)
for x in range(17):
cardsHold.append(cards.pop())
print(cardsHold)
9.bytes 字节串 只读类型 不可修改 可哈希
x = 65534
bufferLittle = x.to_bytes(2,"little")
print(bufferLittle)
bufferBig = x.to_bytes(2,"big")
print(bufferBig)
x = 65534
y = int.from_bytes(b"\xfe\xff","little") #0xfe存在低地址,oxff在高地址
print(y)
y2 = int.from_bytes(b"\xfe\xff","big") # 0xfeff 254 * 255
print(y2)
10.bytearry 字节数组 可修改类型 不可哈希
buffer = bytearray(0x00 for x in range(10))
print(type(buffer),buffer)
buffer = bytearray(b"abcdefghijklmn")
buffer[1] = ord("B") #ord 返回字符的整数
print(buffer)
print(buffer[3:11:3])
11.数据类型及名字绑定
x = 2
print("id: ",id(x))
x = 3
print("id: ",id(x))
y = x
print("a id :",id(x)," y id : ",id(y))
x = 3
y = 3.0
if (x == y):
print("x==y")
if x is y:
print("x is y")
else:
print("x is not y")
print(id(x),id(y))
#只读数据类型:int,float,str,bool,bytes,tuple
#可修改数据类型:list,set,bytearray
Values = (3,2,1)
valuesCopy = Values
valuesCopy = (1,2,1)
print(Values,valuesCopy)
Values = [3,2,1]
valuesCopy = Values
valuesCopy[1] = 55
print(Values,valuesCopy)
import copy
chars = bytearray(b"ABCDEFJ")
charsCopy = chars.copy()
charsCopy2 = chars[:]
print(id(chars),id(charsCopy),id(charsCopy2))