python切片復(fù)制列表的知識點詳解
1、不指定開始和結(jié)束的索引[:],這樣得到的切片就可以包含整個列表,然后給切片一個新的變量,從而實現(xiàn)復(fù)制列表。
2、創(chuàng)建原始列表的副本,兩個列表的操作不會影響。
實例
names = ["Jerry", "Tom"]
names_copy = names[:]
names.append("Ann")
names_copy.append("Bob")
print(f"names:{names}")
print(f"names_copy:{names_copy}")
# output:
# names:['Jerry', 'Tom', 'Ann']
# names_copy:['Jerry', 'Tom', 'Bob']
Python學(xué)習(xí)筆記之列表切片代碼示例
"""切片"""
pepole = ["koulong","liding","ceshi","xiaohong"]
print(pepole[0:1])
print(pepole[:2])
print(pepole[-1:])
#訪問所有元素的切片
for people in pepole[0:1]:
print(people.title())
#復(fù)制切片
my_foods = ["香蕉","蘋果","梨子"]
my_friend_foods = my_foods[0:2]
print("我最喜歡的水果:" + str(my_foods))
print("我最喜歡的水果分別是:")
for my_foods1 in my_foods:
print(my_foods1)
print("我朋友最喜歡的水果:" + str(my_friend_foods))
print("我朋友最喜歡的水果分別是")
for my_friend_foods1 in my_friend_foods:
print(my_friend_foods1)
my_friend_foods.append("葡萄")
print("我朋友最喜歡的水果:" + str(my_friend_foods))
my_friend_foods2 = my_friend_foods.remove("葡萄")
my_friend_foods.append("西瓜")
print(my_friend_foods)
#動手練一練
my_foods.append("芒果")
print("我最喜歡的前2個水果:" + str(my_foods[0:2]))
print(my_foods)
print("我最喜歡的四個水果中的中間2個水果:" + str(my_foods[1:3]))
print("我最喜歡的最后三個水果:" + str(my_foods[1:4]))
到此這篇關(guān)于python切片復(fù)制列表的知識點詳解的文章就介紹到這了,更多相關(guān)python切片復(fù)制列表的本質(zhì)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python的SimpleHTTPServer模塊用處及使用方法簡介
這篇文章主要介紹了Python的SimpleHTTPServer模塊用處及使用方法簡介,小編覺得還是挺不錯的,具有一定借鑒價值,需要的朋友可以參考下2018-01-01
python獲取異常信息exc_info和print_exc的使用
python通過sys.exc_info獲取異常信息,通過traceback.print_exc打印堆棧信息,包括錯誤類型和錯誤位置等信息,本文就來介紹一下具體用法,感興趣的可以了解一下2023-12-12
Python正則表達式re.compile()和re.findall()詳解
re?模塊提供了不少有用的函數(shù),用以匹配字符串,下面這篇文章主要給大家介紹了關(guān)于Python正則表達式re.compile()和re.findall()的相關(guān)資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下2022-07-07
Python使用requests及BeautifulSoup構(gòu)建爬蟲實例代碼
這篇文章主要介紹了Python使用requests及BeautifulSoup構(gòu)建爬蟲,介紹了具體操作步驟和實例代碼等相關(guān)內(nèi)容,小編覺得還是挺不錯的,這里分享給大家,需要的朋友可以參考下2018-01-01

