pythonmultiply
1. python里怎麼樣用二進制來做乘法
下面是代碼,N是全局變數,表示二進制整數有多少位,默認是32,你可以改成其他的。
這個代碼沒有考慮溢出問題,而且僅用於無符號整數。
N = 32 #the number of bits for an integer
def int2b(n, bit=N):
return [(n >> i) & 1 for i in range(bit)[::-1]]
def b_add(l1, l2, bit=N):
result = [0]*N
carry = 0
for i in range(N)[::-1]:
half_sum = l1[i] ^ l2[i]
b_sum = (half_sum ^ carry)
half_carry = (l1[i] & l2[i])
carry = (carry & half_sum) | half_carry
result[i] = b_sum
# print half_sum,b_sum,carry,result
return result
def b_multiply(l1, l2, bit=N):
result = [0]*N
for i in range(N):
if l2[i]:
result = b_add(result[:],l1[N-i-1:]+[0]*(N-i-1))
return result
def b2int(l, bit=N):
result = 0
for i in range(bit):
if l[i]:
result += (l[i]<<(N-i-1))
return result
def main(x, y):
print b2int(b_multiply(int2b(x), int2b(y)))
if __name__ == '__main__':
main(5,7)
2. Python 調試時出現 TypeError: can't multiply sequence by non-int of type 'str' 是什麼原因呢
print (int(x)*int(y))
3.0以上版本input 返回值的類型是字元串 需用要用int轉換為整數
3. Python 源程序編碼注意事項
默認情況下,Python 源文件是 UTF-8 編碼。在此編碼下,全世界大多數語言的字元可以同時用在字元串、標識符和注釋中 — 盡管 Python 標准庫僅使用 ASCII 字元作為標識符,這只是任何可移植代碼應該遵守的約定。如果要正確的顯示所有的字元,你的編輯器必須能識別出文件是 UTF-8 編碼,並且它使用的字體能支持文件中所有的字元。
你也可以為源文件制定不同的字元編碼。為此,在 #! 行(首行)後插入至少一行特殊的注釋行來定義源文件的編碼:
# -*- coding: encoding -*-
通過此聲明,源文件中所有的東西都會被當作用 encoding 指代的 UTF-8 編碼對待。在 Python 庫參考手冊 codecs 一節中你可以找到一張可用的編碼列表。
例如,如果你的編輯器不支持 UTF-8 編碼的文件,但支持像 Windows-1252 的其他一些編碼,你可以定義:
# -*- coding: cp-1252 -*-
這樣就可以在源文件中使用 Windows-1252 字元集中的所有字元了。這個特殊的編碼注釋必須在文件中的 第一或第二 行定義。
4. Python實現輸出1*2*3*4*5......100的和
究竟是和還是積呢?如果是積的話,我們設計的代碼如下,有注釋
multi_num=1#乘法結果初始化
foriinrange(1,101):
multi_num*=i#依次相乘
#列印結果
print('Theresultof1*2*3*4...*100is%e'%multi_num)
執行後,結果如下:
C:。。。
Theresultof1*2*3*4...*100is9.332622e+157
Processfinishedwithexitcode0
5. Python小白求教,怎樣從這個列表中提取元素
因為你的「orderStrategyVOS」是一個列表類型的,所以你需要通過下標來訪問,或者對應的列表項後才能獲取「trigger_side」
所以,正確的寫法是:trigger_side=result_win_loss_info["orderStrategyVOS"][0]["trigger_side"]
6. python中稀疏矩陣的怎麼用numpy處理
NumPy是一個關於矩陣運算的庫,熟悉Matlab的都應該清楚,這個庫就是讓python能夠進行矩陣話的操作,而不用去寫循環操作。
下面對numpy中的操作進行總結。
numpy包含兩種基本的數據類型:數組和矩陣。
數組(Arrays)
>>> from numpy import *>>> a1=array([1,1,1]) #定義一個數組>>> a2=array([2,2,2])>>> a1+a2 #對於元素相加array([3, 3, 3])>>> a1*2 #乘一個數array([2, 2, 2])##>>> a1=array([1,2,3])>>> a1
array([1, 2, 3])>>> a1**3 #表示對數組中的每個數做平方array([ 1, 8, 27])##取值,注意的是它是以0為開始坐標,不matlab不同>>> a1[1]2##定義多維數組>>> a3=array([[1,2,3],[4,5,6]])>>> a3
array([[1, 2, 3],
[4, 5, 6]])>>> a3[0] #取出第一行的數據array([1, 2, 3])>>> a3[0,0] #第一行第一個數據1>>> a3[0][0] #也可用這種方式1##數組點乘,相當於matlab點乘操作>>> a1=array([1,2,3])>>> a2=array([4,5,6])>>> a1*a2
array([ 4, 10, 18])
Numpy有許多的創建數組的函數:
import numpy as np
a = np.zeros((2,2)) # Create an array of all zerosprint a # Prints "[[ 0. 0.]
# [ 0. 0.]]"b = np.ones((1,2)) # Create an array of all onesprint b # Prints "[[ 1. 1.]]"c = np.full((2,2), 7) # Create a constant arrayprint c # Prints "[[ 7. 7.]
# [ 7. 7.]]"d = np.eye(2) # Create a 2x2 identity matrixprint d # Prints "[[ 1. 0.]
# [ 0. 1.]]"e = np.random.random((2,2)) # Create an array filled with random valuesprint e # Might print "[[ 0.91940167 0.08143941]
# [ 0.68744134 0.87236687]]"
數組索引(Array indexing)
矩陣
矩陣的操作與Matlab語言有很多的相關性。
#創建矩陣
>>> m=mat([1,2,3])
>>> m
matrix([[1, 2, 3]])
#取值
>>> m[0] #取一行
matrix([[1, 2, 3]])
>>> m[0,1] #第一行,第2個數據2>>> m[0][1] #注意不能像數組那樣取值了
Traceback (most recent call last):
File "<stdin>", line 1, in <mole>
File "/usr/lib64/python2.7/site-packages/numpy/matrixlib/defmatrix.py", line 305, in __getitem__
out = N.ndarray.__getitem__(self, index)
IndexError: index 1 is out of bounds for axis 0 with size 1#將Python的列表轉換成NumPy的矩陣
>>> list=[1,2,3]
>>> mat(list)
matrix([[1, 2, 3]])
#矩陣相乘
>>> m1=mat([1,2,3]) #1行3列
>>> m2=mat([4,5,6])
>>> m1*m2.T #注意左列與右行相等 m2.T為轉置操作
matrix([[32]])
>>> multiply(m1,m2) #執行點乘操作,要使用函數,特別注意
matrix([[ 4, 10, 18]])
#排序
>>> m=mat([[2,5,1],[4,6,2]]) #創建2行3列矩陣
>>> m
matrix([[2, 5, 1],
[4, 6, 2]])
>>> m.sort() #對每一行進行排序
>>> m
matrix([[1, 2, 5],
[2, 4, 6]])
>>> m.shape #獲得矩陣的行列數
(2, 3)
>>> m.shape[0] #獲得矩陣的行數2>>> m.shape[1] #獲得矩陣的列數3#索引取值
>>> m[1,:] #取得第一行的所有元素
matrix([[2, 4, 6]])
>>> m[1,0:1] #第一行第0個元素,注意左閉右開
matrix([[2]])
>>> m[1,0:3]
matrix([[2, 4, 6]])
>>> m[1,0:2]
matrix([[2, 4]])35363738394
擴展矩陣函數tile()
例如,要計算[0,0,0]到一個多維矩陣中每個點的距離,則要將[0,0,0]進行擴展。
tile(inX, (i,j)) ;i是擴展個數,j是擴展長度
實例如下:
>>>x=mat([0,0,0])
>>> x
matrix([[0, 0, 0]])
>>> tile(x,(3,1)) #即將x擴展3個,j=1,表示其列數不變
matrix([[0, 0, 0],
[0, 0, 0],
[0, 0, 0]])
>>> tile(x,(2,2)) #x擴展2次,j=2,橫向擴展
matrix([[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0]])1234567891011121314
7. python中的multiply函數怎麼用
numpy.multiply
numpy.multiply(x1, x2[, out]) = <ufunc 'multiply'>
Multiply arguments element-wise.
Parameters:
x1, x2 : array_like
Input arrays to be multiplied.
Returns:
y : ndarray
The proct of x1 and x2, element-wise. Returns a scalar if
both x1 and x2 are scalars.
Notes
Equivalent to x1 * x2 in terms of array broadcasting.
Examples
>>>
>>> np.multiply(2.0, 4.0)
8.0
>>>
>>> x1 = np.arange(9.0).reshape((3, 3))
>>> x2 = np.arange(3.0)
>>> np.multiply(x1, x2)
array([[ 0., 1., 4.],
[ 0., 4., 10.],
[ 0., 7., 16.]])
8. python實現圖片變亮或者變暗的方法
python實現圖片變亮或者變暗的方法
這篇文章主要介紹了python實現圖片變亮或者變暗的方法,涉及Python中Image模塊操作圖片的相關技巧,分享給大家供大家參考。具體實現方法如下:
import Image
# open an image file (.jpg or.png) you have in the working folder
im1 = Image.open("angelababy.jpg")
# multiply each pixel by 0.9 (makes the image darker)
# works best with .jpg and .png files, darker < 1.0 < lighter
# (.bmp and .gif files give goofy results)
# note that lambda is akin to a one-line function
im2 = im1.point(lambda p: p * 0.5)
# brings up the modified image in a viewer, simply saves the image as
# a bitmap to a temporary file and calls viewer associated with .bmp
# make certain you have associated an image viewer with this file type
im2.show()
# save modified image to working folder as Audi2.jpg
im2.save("angelababy2.jpg")
運行效果如下所示:
希望本文所述對大家的Python程序設計有所幫助。
9. python編程測試中出現can't multiply sequence by non-int of type 'float'是什麼原因有什麼解決辦法
類型錯誤,檢查操作數類型吧,你沒有給出錯誤的代碼,不好判定.
例如
print( '1' * 3.5 )
就會出現
can't multiply sequence by non-int of type 'float'
原因是字元串的乘法只支持int類型(3.5個字元串是神馬東東)
這個是數據約束拋出的錯誤
10. 一個關於Python乘法的問題,為什麼老是出錯,要怎麼才可以正確
因為你的input裡面的輸入的值沒有規定輸入的類型,應該規定好a為int類型 大概這么寫int(input(「a:」))
望採納