導航:首頁 > 編程語言 > python中list轉為數組

python中list轉為數組

發布時間:2024-08-12 14:31:03

python 如何將for循環 的結果寫成一個數組

代碼:
list=[]
foriinrange(10):
list.append(i)
print(list)

過程:

>>> for i in range(10):

... list.append(i)

...

>>> print(list)

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

>>>


⑵ python中的list和array的不同之處

在Python中,list和array都可以根據索引來取其中的元素,但是list可以用append或者+來新增元素或者添加數組,而array不行。具體區別如下:
1、作用不同
list是處理一組有序項目的數據結構;
array數組存儲單一數據類型的多維數組;
2、內置數據類型
list是Python的內置數據類型;
array數組需要導入標准庫才行,不屬於內置類型;
3、數據類型是否相同
list中的數據類不必相同的,即每個元素可以是不同的數據類型;
array則是由Numpy封裝,存放的元素都是相同的數據類型;
4、運算
列表list不可以進行數學四則運算;
數組array可以進行數學四則運算;

⑶ python怎麼將用戶輸入的字元串轉為數組

在python的IDLE中輸入input_list = list(map(str,input())),回車,輸入:dsfjd,再回車,輸入print(input_list),列印的結果就是['d', 's', 'f', 'j', 'd']了。

⑷ python裡面列表和數組區別是什麼

python裡面的列表用list表示,它非常類似我們js中的數組,使用中括弧來表示。
例如 list3 = ["a", "b", "c", "d"]
python中默認沒有提供數組類型,不過有個元組類型,它類似列表,但是不能修改。
tup1 = ('physics', 'chemistry', 1997, 2000)
在python中有個numpy包,它裡面提供了數組array
import numpy as np
print(np.array([2,3,4]))

⑸ Python中list,tuple,dict,set的區別和用法

python中list,tuple,dict,set是最常用的集合類型。

list列表,相當於一個數組,不過list的長度是自動變化的而且列表元素自由的,不必每個元素都是同一種類型。它的簡潔的定義方式是a=[]。有序組合

tuple也是一個組合。不過tuple在定義好之後就不能再變化。它的簡潔的定義方式是a=1,3也可以是a=(1,3).有序組合。

dict是字典類型。也就是鍵值對類型。鍵名不可以重復,並且不可以變化(字元串就符合這個要求,常用字元串作為鍵名)。它的簡潔的定義方式是a={}.無序組合(意思就是你無法按照添加的順序對他進行遍歷)。

set是set類型(不好翻譯,用的也少)。也是一個無序的組合,元素是互斥的,也就不會出現相同的元素。可以把一個序列轉換成無重復元素的set.無序組合。

以下是使用的示例代碼。

a_tuple=(1,3423,'34')
a_list=[12,12.34,'sds']
a_dict={'key1':1,'key2':2}
a_set=set('2323')
fortina_tuple:
print('%sintuple'%t)
print('*'*10)
forlina_list:
print('%sinlist'%l)
print('*'*10)
fork,vina_dict.items():
print('key=%s,value=%sindict'%(k,v))
print('*'*10)
forsina_set:
print('%sinset'%s)
print('*'*10)

不明白可追問。

⑹ python如何將list中的字元轉為數字

python裡面好像只能直接轉一維的list,以python 3.6為例:

問題 1:

list=['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']

轉化為:list=[0, 1 ,2, 3, 4, 5, 6, 7, 8, 9]

代碼如下:

list_to_float=list(map(lambdax:float(x),list))

問題2:(對於二維數組,需要加個循環,變成一維數組)

list=[['0', '1', '2'], ['3', '4', '5'], ['6', '7', '8']]

轉化為:list=[[0, 1 ,2], [3, 4, 5], [6, 7, 8]]

代碼如下:

list_to_float=[]
foreachinlist:
each_line=list(map(lambdax:float(x),each))
list_to_float.append(each_line)

總之:關鍵還是map函數映射,如果是python 2.x的話,你可以試試

list_to_float=map(lambdax:float(x),list)

⑺ Python怎麼將數字數組轉為字元數組

用map函數

文檔


map(function,iterable,...)

Applyfunctionto every item ofiterableand return a list of the results. If additionaliterablearguments are passed,functionmust take that many arguments and is applied to the items from all iterables in parallel. If one iterable is shorter than another it is assumed to be extended withNoneitems. IffunctionisNone, the identity function is assumed; if there are multiple arguments,map()returns a list consisting of tuples containing the corresponding items from all iterables (a kind of transpose operation). Theiterablearguments may be a sequence or any iterable object; the result is always a list.

a=[1,2,3,4]
s=map(str,a)
閱讀全文

與python中list轉為數組相關的資料

熱點內容
壽司解壓系列全集視頻 瀏覽:911
物體三維重建演算法 瀏覽:980
fuli直播app哪個好 瀏覽:918
租辦公室用什麼app 瀏覽:104
醫師定期考核刷題app哪個好 瀏覽:336
導出dmp文件命令 瀏覽:286
手機百度網盤怎麼解壓密碼文件 瀏覽:583
索引重新編譯 瀏覽:604
命令與征服4免cd補丁完美版 瀏覽:426
kotlin編譯為native 瀏覽:140
家用編譯機 瀏覽:549
電子加密貨幣最新政策 瀏覽:379
androidcanvas撤銷 瀏覽:269
安卓手機怎麼把圖標全部下移 瀏覽:185
飢荒被伺服器踢出怎麼進 瀏覽:171
c編譯器哪款好 瀏覽:732
快手寶哥發明什麼app 瀏覽:822
張艷玲編譯 瀏覽:66
android展開收起動畫 瀏覽:237
linuxxz文件 瀏覽:160