新聞中心
創(chuàng)新互聯(lián)Python教程:

10年積累的成都做網(wǎng)站、成都網(wǎng)站建設(shè)、成都外貿(mào)網(wǎng)站建設(shè)經(jīng)驗(yàn),可以快速應(yīng)對(duì)客戶(hù)對(duì)網(wǎng)站的新想法和需求。提供各種問(wèn)題對(duì)應(yīng)的解決方案。讓選擇我們的客戶(hù)得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識(shí)你,你也不認(rèn)識(shí)我。但先制作網(wǎng)站后付款的網(wǎng)站建設(shè)流程,更有永春免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。
編寫(xiě)一個(gè) Python 程序,將一個(gè)項(xiàng)目附加到列表中。在 Python 中,我們有多個(gè)選項(xiàng)可以向列表中追加或添加一個(gè)項(xiàng)目,這里,我們使用 append 方法。list append 方法在列表的末尾添加一個(gè)新元素。
【追加(項(xiàng))
numbers = [10, 20, 30, 40, 50]
print("Numeric List Before Appending Item")
print(numbers)
numbers.append(65)
print("Numeric List After Appending First Item")
print(numbers)
value = int(input("Please enter the List Item = "))
numbers.append(value)
print("Numeric List After Appending Second Item")
print(numbers)
這個(gè) python 程序使用索引函數(shù)將一個(gè)項(xiàng)目附加到列表中。list index 方法在指定的索引處添加一個(gè) list 元素。
list.insert(索引,項(xiàng)目)
countries = ["India", "USA", "UK", "Italy"]
print("List Before Appending Item")
print(countries)
countries.insert(3, "Japan")
print("\nList After Appending Japan at 3rd Index Position")
print(countries)
countries.insert(0, "China")
print("\nList After Appending China at 0 Index Position")
print(countries)
countries.insert(6, "France")
print("\nList After Appending France at 6 Index Position")
print(countries)
List Before Appending Item
['India', 'USA', 'UK', 'Italy']
List After Appending Japan at 3rd Index Position
['India', 'USA', 'UK', 'Japan', 'Italy']
List After Appending China at 0 Index Position
['China', 'India', 'USA', 'UK', 'Japan', 'Italy']
List After Appending France at 6 Index Position
['China', 'India', 'USA', 'UK', 'Japan', 'Italy', 'France']
這個(gè) Python 示例允許輸入列表索引和列表值,并使用索引方法添加新項(xiàng)目。
numbers = [11, 22, 33, 44, 55, 66]
print("Numeric List Before Inserting Item")
print(numbers)
index = int(input("Please enter Index Position = "))
value = int(input("Please enter the List Value = "))
numbers.insert(index, value)
print("Numeric List After Appending Item")
print(numbers)
Numeric List Before Inserting Item
[11, 22, 33, 44, 55, 66]
Please enter Index Position = 4
Please enter the List Value = 111
Numeric List After Appending Item
[11, 22, 33, 44, 111, 55, 66]
Python 程序使用擴(kuò)展方法將一個(gè)項(xiàng)目附加到列表中
list extend 方法將列表、元組、字符串等任何可條目添加到現(xiàn)有列表的末尾。
list.extend(迭代器)
countries = ["India", "USA", "UK", "Italy"]
print("List Before Appending Item")
print(countries)
tuple1 = ("Japan", "China", "France")
countries.extend(tuple1)
print("\nList After Appending Tuple using extend")
print(countries)
list1 = [19, 17, 39, 55]
countries.extend(list1)
print("\nList After Appending List sing extend")
print(countries)
countries.extend((11.5, 19.2))
print("\nList After Appending 11.5, 19.2 using extend")
print(countries)
List Before Appending Item
['India', 'USA', 'UK', 'Italy']
List After Appending Tuple using extend
['India', 'USA', 'UK', 'Italy', 'Japan', 'China', 'France']
List After Appending List sing extend
['India', 'USA', 'UK', 'Italy', 'Japan', 'China', 'France', 19, 17, 39, 55]
List After Appending 11.5, 19.2 using extend
['India', 'USA', 'UK', 'Italy', 'Japan', 'China', 'France', 19, 17, 39, 55, 11.5, 19.2] 當(dāng)前名稱(chēng):Python程序:將項(xiàng)目附加到列表
本文地址:http://www.fisionsoft.com.cn/article/cogpceh.html


咨詢(xún)
建站咨詢(xún)
