(32).轉置矩陣
X=[[12,7],[4,5],[3,8]]
result=[[0,0,0],[0,0,0]]
for i in range(len(X)):
for j in range(len(X[0])):
result[j][i]=X[i][j]
for r in result:
print(r)
[12, 4, 3]
[7, 5, 8]
(33).產生隨機亂數
import random
for i in range(1,10+1):
print(random.randint(1,100),end=' ')
82 27 59 8 99 66 38 5 23 11
>>> dir (random)
['BPF', 'LOG4', 'NV_MAGICCONST', 'RECIP_BPF', 'Random', 'SG_MAGICCONST', 'SystemRandom', 'TWOPI', '_Sequence', '_Set', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_accumulate', '_acos', '_bisect', '_ceil', '_cos', '_e', '_exp', '_inst', '_log', '_os', '_pi', '_random', '_repeat', '_sha512', '_sin', '_sqrt', '_test', '_test_generator', '_urandom', '_warn', 'betavariate', 'choice', 'choices', 'expovariate', 'gammavariate', 'gauss', 'getrandbits', 'getstate', 'lognormvariate', 'normalvariate', 'paretovariate', 'randint', 'random', 'randrange', 'sample', 'seed', 'setstate', 'shuffle', 'triangular', 'uniform', 'vonmisesvariate', 'weibullvariate']
(34).發牌程式
import random
flower='♥♠♦♣'
nstr='A23456789TJQK'
c=[]
for i in range(0,52):
c.append(i)
for i in range(0,51):
s=random.randint(0,51)
temp=c[i];c[i]=c[s];c[s]=temp
for i in range(0,52):
f=c[i]//13;n=c[i]%13
print(flower[f:f+1]+nstr[n],end=' ')
if (i%13==12):
print()
♥Q ♠Q ♥K ♣5 ♠4 ♣7 ♠2 ♥6 ♦6 ♦2 ♠A ♠J ♠6
♠7 ♥4 ♥8 ♠8 ♠9 ♣J ♦4 ♥7 ♥2 ♥5 ♥A ♠5 ♣K
♠T ♦5 ♦T ♦8 ♦7 ♣8 ♥T ♣3 ♣T ♠3 ♦A ♥J ♦9
♣Q ♣9 ♥9 ♣2 ♣A ♦Q ♦J ♦K ♠K ♥3 ♣4 ♣6 ♦3
#UTF-8中文內碼: 9829、9824、9830、9827
>>> chr(9829)
'♥'
>>> chr(9824)
'♠'
>>> chr(9830)
'♦'
>>> chr(9827)
'♣'
>>> print(ord('♣'))
9827
>>> print(ord('♦'))
9830
>>> print(ord('♠'))
9824
>>> print(ord('♥'))
9829
------亂數發牌花色邏輯說明
import random
flower=['H','S','D','C']
num=['A','2','3','4','5','6','7','8','9','T','J','Q','K']
c=[]
#填數字
for i in range(52):
c.append(i)
print('%2d' %c[i],end=' ')
print('\n\n')
#產生亂數
for i in range(52):
r=random.randint(0,51)
print(r,end=' ')
print('\n\n')
#印出數字
for i in range(52):
print(c[i],end=' ')
print('\n\n')
#13張下跳
for i in range(52):
print('%2d' %i,end=' ')
if(i+1)%13==0:
print()
print('\n\n')
#印出花色 數字
for i in range(52):
print(c[i]//13,c[i]%13,end=' ')
if (i+1)%13==0:
print()
print('\n\n')
#印出花色 SHDC 和 數字
for i in range(52):
f=c[i]//13
n=c[i]%13
print(flower[f],num[n],end=' ')
if (i+1)%13==0:
print()
print('\n\n')
#印亂數
for i in range(51):
r=random.randint(0,52)
c[i],c[r]=c[r],c[i]
print(c[i],end=' ')
print('\n\n')
#印出花色 和 數字
for i in range(52):
f=c[i]//13
m=c[i]%13
print(f,m,end=' ')
if (i+1)%13==0:
print()
print('\n\n')
#印出結果
for i in range(52):
f=c[i]//13
m=c[i]%13
print(flower[f],num[m],end=' ')
if (i+1)%13==0:
print()
print('\n\n')
num='A23456789TJQK'
for i in range(13):
print(num[i:i+1],end=' ')
print()
print(list(num))
pokernumlist=list(num)
print()
for i in pokernumlist:
print(i,end=' ')
(35).顯示月曆
y=2019
m=1
md=[0,31,28,31,30,31,30,31,31,30,31,30,31]
blk24=24*' '
#算1月1日到輸入月份1日前共幾天
sunday=0
for i in range(0,m):
sunday=sunday+md[i]
#算輸入月份的1日星期幾?
w=y+y/4-y/100+y/400+sunday
week=int(w%7)
print(' ',y,'年',m,'月')
print(" Sun Mon Tue Wed Thu Fri Sat")
print(blk24[0:week*4],end='')
for i in range(1,md[m]+1):
print('%4d' %i,end='')
if ((week+i)%7 ==0):
print()
2019 年 1 月
Sun Mon Tue Wed Thu Fri Sat
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
import calendar
yy=2019
mm=1
print(calendar.month(yy,mm))
January 2019
Mo Tu We Th Fr Sa Su
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
(36).兩個矩陣相加
X=[[2,7,3],[4,5,6],[1,8,9]]
Y=[[4,8,1],[6,7,3],[2,5,9]]
r=[[0,0,0],[0,0,0],[0,0,0]]
for i in range(len(X)):
for j in range(len(X[0])):
r[i][j]=X[i][j]+Y[i][j]
for r in r:
print(r)
----------------或者
for i in r:
print(i)
[6, 15, 4]
[10, 12, 9]
[3, 13, 18]
# 兩個矩陣相乘 (值得思考 有點難度)
'''
https://goo.gl/pP2KbL
可到這裡驗證,本題矩陣相乘 結果
https://goo.gl/2Kjg9i
X =
(0,0) (0,1) (0,2)
(1,0) (1,1) (1,2)
(2,0) (2,1) (2,2)
Y =
(0,0) (0,1) (0,2)
(1,0) (1,1) (1,2)
(2,0) (2,1) (2,2)
X*Y=
x(0,0)*y(0,0)+x(0,1)*y(1,0)+x(0,2)*y(2,0) x(0,0)*y(0,1)+x(0,1)*y(1,1)+x(0,2)*y(2,1) x(0,0)*y(0,2)+x(0,1)*y(1,2)+x(0,2)*y(2,2)
x(1,0)*y(0,0)+x(1,1)*y(1,0)+x(1,2)*y(2,0) x(1,0)*y(0,1)+x(1,1)*y(1,1)+x(1,2)*y(2,1) x(1,0)*y(0,2)+x(1,1)*y(1,2)+x(1,2)*y(2,2)
x(2,0)*y(0,0)+x(2,1)*y(1,0)+x(2,2)*y(2,0) x(2,0)*y(0,1)+x(2,1)*y(1,1)+x(2,2)*y(2,1) x(2,0)*y(0,2)+x(2,1)*y(1,2)+x(2,2)*y(2,2) ... ...
'''
# 設定 X 陣列
X = [[1,7,3],
[4 ,5,6],
[2 ,8,9]]
# 設定 Y 陣列
Y = [[5,8,1],
[6,7,3],
[4,2,9]]
result = [[0,0,0],
[0,0,0],
[0,0,0]]
print('印出 X')
for i in range(3):
for j in range (3):
print(X[i][j], end=' ')
print()
print()
print('印出 Y')
for i in range(3):
for j in range (3):
print(Y[i][j], end=' ')
print()
print()
m1=0;m2=0;m3=0
for i in range(3):
for j in range(3):
m1=m1+X[i][j]*Y[j][0] ; m2=m2+X[i][j]*Y[j][1] ; m3=m3+X[i][j]*Y[j][2]
result[i][0]=m1 ; result[i][1]=m2 ; result[i][2]=m3
print('%3d %3d %3d' % (m1,m2,m3)) # 印出結果
m1=0;m2=0;m3=0
print()
for r in result: # 印出陣列值
print(r)
印出 X
1 7 3
4 5 6
2 8 9
印出 Y
5 8 1
6 7 3
4 2 9
59 63 49
74 79 73
94 90 107
[59, 63, 49]
[74, 79, 73]
[94, 90, 107]