當前位置:首頁 » 編程語言 » pythonlist0

pythonlist0

發布時間: 2022-05-02 18:36:42

① 為什麼python list的索引從0開始

如果你的l是如此定義的
List l=new ArrayList();
那麼拋出此異常是非常正確的!因為java源碼如下:
public void add(int index, E element) {
if (index > size || index < 0)
throw new IndexOutOfBoundsException(
"Index: "+index+", Size: "+size);
//你的程序就符合index>size,所以就拋出IndexOutOfBoundsException
ensureCapacity(size+1); // Increments modCount!!
System.array(elementData, index, elementData, index + 1,
size - index);
elementData[index] = element;
size++;
}

② python包含不同長度的list的一維數組用0填充統一長度

matrix=[[1],
[1,2],
[1,2,3],
[1,2,3,4],
[1,2,3,4,5],
[3,4,5],
[2,3,4,5],
]
#現在需要將矩陣中所有的列表長度對齊到最長的列表的長度5,末尾全部用0填充
max_len=max((len(l)forlinmatrix))
new_matrix=list(map(lambdal:l+[0]*(max_len-len(l)),matrix))
print(new_matrix)

③ Python 想讓一個list裡面所有大於零的數字組成新li

a=[1,2,0,4,5] b=[] for i in range(len(a)): c=int(a[i]) if c>0: bappend(c)Python 想讓一個list裡面所有大於零的數字組成新li

④ python替換list元素,列表alist中有很多0數字,要替換成1

alist=[1,3,5,2,0,3,7,0,0,3,7]
foriinrange(len(alist)):
ifalist[i]==0:
alist[i]=1

printalist

⑤ 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中列表全部賦0

>>> list=[0]
>>> list=list*100
>>> list
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
>>> len(list)
100

⑦ python list哪些為1

創建一個列表,只要把逗號分隔的不同的數據項使用方括弧括起來即可。如下所示:
list1 = ['physics', 'chemistry', 1997, 2000];
list2 = [1, 2, 3, 4, 5 ];
list3 = ["a", "b", "c", "d"];

與字元串的索引一樣,列表索引從0開始。列表可以進行截取、組合等。
訪問列表中的值
使用下標索引來訪問列表中的值,同樣你也可以使用方括弧的形式截取字元,如下所示:
#!/usr/bin/python

list1 = ['physics', 'chemistry', 1997, 2000];
list2 = [1, 2, 3, 4, 5, 6, 7 ];

print "list1[0]: ", list1[0]
print "list2[1:5]: ", list2[1:5]

⑧ 如何在python中列出由一定個數0和1組成的list的排列組合

importitertools
src=[0,0,1]
printset(itertools.permutations(src,len(src)))

這個是正解!

⑨ Python 想讓一個list裡面所有大於零的數字組成新list怎麼辦

lista=[1,3,5,-1,-2,9]
listb=[]

i=len(lista)-1
while i>=0:

if lista[i]>0:
listb.append(list[i])
del lista[i]
i+=1

這樣就可以了。
也可以減化一下
listb=[a for a in lista if a>0]
lista=[a for a in lista if a<=0]
這樣就更簡單了。

⑩ python list

for i, elem in enumerate(list1):
list4[list5[i]] = elem

f = open('your_output_file.txt','w')
for elem in list4:
f.write('%d %d\n'%(elem[0],elem[1]))

f.close()

熱點內容
優酷怎麼給視頻加密 發布:2025-05-14 19:31:34 瀏覽:633
夢三國2副本腳本 發布:2025-05-14 19:29:58 瀏覽:859
phpxmlhttp 發布:2025-05-14 19:29:58 瀏覽:432
Pua腳本 發布:2025-05-14 19:24:56 瀏覽:448
蘋果像素低為什麼比安卓好 發布:2025-05-14 19:13:23 瀏覽:460
安卓機微信怎麼設置紅包提醒 發布:2025-05-14 19:00:15 瀏覽:271
androidsystem許可權設置 發布:2025-05-14 18:56:02 瀏覽:970
mq腳本 發布:2025-05-14 18:45:37 瀏覽:25
仙境傳說ro解壓失敗 發布:2025-05-14 18:45:01 瀏覽:868
betweenand的用法sql 發布:2025-05-14 18:39:25 瀏覽:250