日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無(wú)先后,達(dá)者為師

網(wǎng)站首頁(yè) 編程語(yǔ)言 正文

Python列表1~n輸出步長(zhǎng)為3的分組實(shí)例_python

作者:Charles_TheGod ? 更新時(shí)間: 2022-07-02 編程語(yǔ)言

列表1~n輸出步長(zhǎng)為3的分組

print([[x for x in range(1,101)][i:i+3] for i in range(0,100,3)])

輸出結(jié)果

[[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, 32, 33],?
[34, 35, 36], [37, 38, 39], [40, 41, 42], [43, 44, 45], [46, 47, 48],?
[49, 50, 51], [52, 53, 54], [55, 56, 57], [58, 59, 60], [61, 62, 63],?
[64, 65, 66], [67, 68, 69], [70, 71, 72], [73, 74, 75], [76, 77, 78],?
[79, 80, 81], [82, 83, 84], [85, 86, 87], [88, 89, 90], [91, 92, 93],?
[94, 95, 96], [97, 98, 99], [100]]

Python步長(zhǎng)(字符串切片)

步長(zhǎng)格式 變量[開(kāi)始索引:結(jié)束索引:長(zhǎng)度]

步長(zhǎng)計(jì)算公式 當(dāng)前索引+ 步長(zhǎng)= 下一個(gè)索引

步長(zhǎng)同時(shí)也可以取成負(fù)數(shù)

str1 = "yesterday once more"
str2 = str1[::-1]
print(str2)

步長(zhǎng)為負(fù)數(shù)時(shí),省略開(kāi)頭,對(duì)應(yīng)的是最后一個(gè)結(jié)尾

步長(zhǎng)為負(fù)數(shù)時(shí),省略結(jié)尾,對(duì)應(yīng)的是第一個(gè)元素

str2 = str1[10::-1]
print(str2)

str2 = str1[:6:-1]
print(str2)

原文鏈接:https://blog.csdn.net/Charles_TheGod/article/details/82987670

欄目分類(lèi)
最近更新