久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

list extend() 索引,不僅將列表元素插入到末尾

list extend() to index, inserting list elements not only to the end(list extend() 索引,不僅將列表元素插入到末尾)
本文介紹了list extend() 索引,不僅將列表元素插入到末尾的處理方法,對大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我正在尋找最 Pythonic 的方式來實(shí)現(xiàn)列表 extend 函數(shù)的一個(gè)版本,它擴(kuò)展到給定的索引而不是列表的末尾.

I'm looking for the most pythonic way to implement a version of the list extend function, where it extends to a given index instead of the end of the list.

a_list = [ "I", "rad", "list" ]                                                       
b_list = [ "am", "a" ]
a_list.my_extend( b_list, 1 ) # insert the items from b_list into a_list at index 1

print( a_list ) # would output: ['I', 'am', 'a', 'rad', 'list']

有沒有辦法在不建立新列表的情況下做到這一點(diǎn),像這樣?

Is there a way to do this without building a new list, like this?

a_list = [ "I", "rad", "list" ]
b_list = [ "am", "a" ]
c_list = []

c_list.extend( a_list[:1] )
c_list.extend( b_list     )
c_list.extend( a_list[1:] )

print( c_list ) # outputs: ['I', 'am', 'a', 'rad', 'list']

這種方法實(shí)際上并沒有那么糟糕,但我有一種預(yù)感,它可能會更容易.可以嗎?

That approach isn't actually so bad, but I have a hunch it could be easier. Could it?

推薦答案

當(dāng)然可以使用切片索引:

Sure, you can use slice indexing:

a_list[1:1] = b_list

為了演示一般算法,如果您要在假設(shè)的自定義 list 類中實(shí)現(xiàn) my_extend 函數(shù),它看起來像這樣:

Just to demonstrate the general algorithm, if you were to implement the my_extend function in a hypothetical custom list class, it would look like this:

def my_extend(self, other_list, index):
    self[index:index] = other_list

但實(shí)際上不要讓它成為一個(gè)函數(shù),只需在需要時(shí)使用切片符號即可.

But don't actually make that a function, just use the slice notation when you need to.

這篇關(guān)于list extend() 索引,不僅將列表元素插入到末尾的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Why I cannot make an insert to Python list?(為什么我不能插入 Python 列表?)
Insert a column at the beginning (leftmost end) of a DataFrame(在 DataFrame 的開頭(最左端)插入一列)
Python psycopg2 not inserting into postgresql table(Python psycopg2 沒有插入到 postgresql 表中)
How to add element in Python to the end of list using list.insert?(如何使用 list.insert 將 Python 中的元素添加到列表末尾?)
TypeError: #39;float#39; object is not subscriptable(TypeError:“浮動對象不可下標(biāo))
Insert element in Python list after every nth element(在每個(gè)第 n 個(gè)元素之后插入 Python 列表中的元素)
主站蜘蛛池模板: 中文字幕欧美日韩一区 | 国产精品毛片无码 | 日韩三级 | 欧美国产日韩在线观看 | 国产一级片一区二区三区 | 一区二区三区高清 | 日韩av免费看 | 日韩毛片免费看 | 99久久久久国产精品免费 | 国产精品一区二区三 | 中文字幕在线精品 | 国产精品久久7777777 | 亚洲免费人成在线视频观看 | 一区二区三区四区五区在线视频 | 一区二区三区精品在线视频 | 国产一区二区三区不卡av | 国偷自产av一区二区三区 | 日韩成人精品在线观看 | 免费视频99 | 久久久久国产一区二区三区 | 色视频一区二区 | 欧美又大粗又爽又黄大片视频 | 国产激情视频 | 国产亚洲一区二区三区在线观看 | 亚洲一区二区三区在线 | 精品欧美 | 一区二区三区不卡视频 | 日韩在线小视频 | 日韩精品视频在线免费观看 | 欧美国产中文 | 秋霞av国产精品一区 | 在线欧美小视频 | 福利国产 | 黄色一级特级片 | 国产精品国产a级 | 免费成人午夜 | 国产精品1区2区3区 中文字幕一区二区三区四区 | 欧美一二区 | 日韩手机视频 | 国产精品一区二区在线 | 亚洲天堂网站 |