日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

Python中的append()和extend()

發布時間:2023/12/1 python 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python中的append()和extend() 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

列出append()方法 (List append() method)

append() method is used to insert an element or a list object to the list and length of the List increased by the 1.

append()方法用于將元素或列表對象插入列表,并且列表長度增加1。

Syntax:

句法:

List.append(element/object)

Example 1:

范例1:

# declaring a list list1 = [10, 20, 30, 40, 50]# printing the list before appending print("list1: ", list1) print("len(list1): ", len(list1))# appending an element to the list list1.append(60)# printing the list after appending print("list1: ", list1) print("len(list1) ", len(list1))

Output

輸出量

list1: [10, 20, 30, 40, 50] len(list1): 5 list1: [10, 20, 30, 40, 50, 60] len(list1) 6

Example 2:

范例2:

# declaring a list list1 = [10, 20, 30, 40, 50]# printing the list before appending print("list1: ", list1) print("len(list1) ", len(list1))# appending a list object to the list list2 = [60, 70, 80] list1.append(list2)# printing the list after appending print("list1: ", list1) print("len(list1) ", len(list1))

Output

輸出量

list1: [10, 20, 30, 40, 50] len(list1) 5 list1: [10, 20, 30, 40, 50, [60, 70, 80]] len(list1) 6

列出extend()方法 (List extend() method)

extend() method is used to extend the list by inserting the given number of elements or given list and the length of the List increased by the total number of elements added.

extend()方法用于通過插入給定數量的元素或給定列表來擴展列表,并且List的長度增加所添加元素的總數。

Syntax:

句法:

List.extend(another_list)

Note: If we append a list, then the list appended as an element to the List, thus, the length of the list increased by 1. While, if we extend a list by a list, then the list extended by the given number of elements in the list (which is passed as an argument), thus, the length of the list increased by the given number of elements in the list which is going to be added.

注意:如果我們添加一個列表,那么該列表作為元素添加到列表中,因此,列表的長度增加了1。而如果我們將列表擴展了一個列表,則該列表擴展了給定數量的列表中的元素(作為參數傳遞),因此,列表的長度增加了列表中要添加的元素的給定數量。

Example:

例:

# declaring a list list1 = [10, 20, 30, 40, 50]# printing the list before extending print("list1: ", list1) print("len(list1) ", len(list1))# extending the list with another list list2 = [60, 70, 80] list1.extend(list2)# printing the list after extending print("list1: ", list1) print("len(list1) ", len(list1))



翻譯自: https://www.includehelp.com/python/append-and-extend.aspx

總結

以上是生活随笔為你收集整理的Python中的append()和extend()的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。