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

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/377302.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/377302.shtml
英文地址,請注明出處:http://en.pswp.cn/news/377302.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

紅黑樹的實現

目錄1、紅黑樹原理1、紅黑樹性質2、變換規則(從插入結點的角度來講)1.變色2.左旋3.右旋3、刪除結點需要注意的地方2、代碼1、定義結點以及構造函數2、定義紅黑樹類以及聲明它的方法3、左旋4、右旋5、插入操作6、修正操作7、刪除操作3、參考鏈接1、紅黑樹…

118 - ZOJ Monthly, July 2012

http://acm.zju.edu.cn/onlinejudge/showContestProblems.do?contestId339 都是賽后做的。。。弱爆了 A題是找由2和5組成的數字的個數 直接打個表就行了 只是比賽的時候不知道怎么打表啊。。 View Code #include<cstdio> #include<cstring> #include<algorith…

edp1.2和edp1.4_EDP??的完整形式是什么?

edp1.2和edp1.4EDP??&#xff1a;電子數據處理 (EDP: Electronic Data Processing) EDP is an abbreviation of Electronic Data Processing. It alludes to the functioning of operations of commercial data, documents processing of storing, with the use of a compute…

高效讀書心得

1.盡量閱讀中文版 雖然有人英文很強&#xff0c;有的翻譯很差&#xff0c;但AnyWay 中文閱讀與理解的時間&#xff0c;略讀與快速定位感興趣內容的速度還是要快一些。 2.即時批注、總結筆記與交流 雖然愛書&#xff0c;但發現最有效的讀書方式還是不斷的制造脂批本&…

《MySQL——增刪改查以及常用語法》

目錄登錄和退出MySQL服務器基本語法&#xff08;增刪改查&#xff09;登錄和退出MySQL服務器 # 登錄MySQL 密碼 $ mysql -u root -p12345612 # 退出MySQL數據庫服務器 exit;基本語法&#xff08;增刪改查&#xff09; -- 顯示所有數據庫 show databases;-- 創建數據庫 CREA…

WCF簡介

一、簡介 WCF是Windows Communication Foundation縮寫&#xff0c;是Microsoft為構建面向服務的應用提供的分布式通信編程框架&#xff0c;是.NET Framework 3.5的重要組成部分。使用該框架&#xff0c;開發人員可以構建跨平臺、安全、可靠和支持事務處理的企業級互聯應用解決方…

css鏈接樣式_CSS中的樣式鏈接

css鏈接樣式CSS樣式鏈接 (CSS Styling Links) The links in CSS can be styled in various ways to make our website more presentable and attractive. The links can also be styled depending on their states e.g. visited, active, hover, etc. CSS中的鏈接可以通過各種方…

《MySQL——約束》

目錄主鍵約束唯一主鍵非空約束默認約束外鍵約束主鍵約束 -- 主鍵約束 -- 使某個字段不重復且不得為空&#xff0c;確保表內所有數據的唯一性。 CREATE TABLE user (id INT PRIMARY KEY,name VARCHAR(20) );-- 聯合主鍵 -- 聯合主鍵中的每個字段都不能為空&#xff0c;并且加起…

UIControl事件

CHENYILONG BlogUIControl事件 FullscreenUIControl事件1.UIControlEventTouchDown單點觸摸按下事件&#xff1a;用戶點觸屏幕&#xff0c;或者又有新手指落下的時候。2.UIControlEventTouchDownRepeat多點觸摸按下事件&#xff0c;點觸計數大于1&#xff1a;用戶按下第二、三、…

C++ 為什么要使用#ifdef __cplusplus extern C { #endif

經常看到別人的頭文件 有這樣的代碼 #ifdef __cplusplus extern "C" { #endif// C 樣式 的函數#ifdef __cplusplus } #endif 為什么要這樣呢&#xff1f; 因為 C 語言不支持重載函數 也就是同名函數&#xff0c;參數卻不一樣,C支持&#xff0c;其編譯器對函數名的處理…

css中的媒體查詢_CSS中的媒體查詢

css中的媒體查詢CSS | 媒體查詢 (CSS | Media Queries) Creating a web page is not an easy task as it requires loads of content and data so that it becomes strongly responsive to the users. To do that various contents are even added e.g.: resources, informativ…

SharePoint2013安裝組件時AppFabric時出現1603錯誤,解決方法:

采用PowerShell命令批量下載必備組件: 下載完成后&#xff0c;采用批處理命令安裝必備組件。 注&#xff1a;SPS2013安裝必備組件及批處理下載地址&#xff1a; 需要將必備組件放在安裝文件的PrerequisiteInstallerFiles文件夾中&#xff0c;將PreReq2013.bat放在安裝文件根目錄…

《MySQL——數據表設計三大范式》

目錄數據表設計范式第一范式第二范式第三范式數據表設計范式 第一范式 數據表中的所有字段都是不可分割的原子值。 字段值還可以繼續拆分的&#xff0c;就不滿足第一范式&#xff0c;如下&#xff1a; 下面這個&#xff0c;更加貼合第一范式&#xff1a; 范式設計得越詳細&…

三道簡單樹型dp+01背包~~hdu1561,poj1947,zoj3626

以前學樹型dp就是隨便的看了幾道題&#xff0c;沒有特別注意樹型dp中的小分類的總結&#xff0c;直到上次浙大月賽一道很簡單的樹型dp都不會&#xff0c;才意識到自己太水了&#xff5e;&#xff5e;come on&#xff01; hdu1561&#xff1a;題目給出了很多棵有根樹&#xff0c…

css 字體圖標更改顏色_在CSS中更改字體

css 字體圖標更改顏色CSS字體屬性 (CSS font properties ) Font properties in CSS is used to define the font family, boldness, size, and the style of a text. CSS中的字體屬性用于定義字體系列 &#xff0c; 粗體 &#xff0c; 大小和文本樣式 。 Syntax: 句法&#xf…

深入new/delete:Operator new的全局重載

Operator new 的全局重載 原文地址&#xff1a;http://blog.csdn.net/zhenjing/article/details/4354880 我們經常看到這么一句話&#xff1a; operator new 可以重載&#xff0c; placement new 不可重載。其實此處所說的不可重載應該是指全局的 placement new 不可重載&#…

C++基礎知識點整理

基本語法 1、static關鍵字的作用 1、全局靜態變量 加了static關鍵字的全局變量只能在本文件中使用。 存儲在靜態存儲區&#xff0c;整個程序運行期間都存在。 2、局部靜態變量 作用域仍為局部作用域。 不過離開作用域之后&#xff0c;并沒有銷毀&#xff0c;而是貯存程序中&a…

Haskell學習筆記

《learn you a Haskell》這書的結構與常見的語言入門教材完全不一樣。事實上&#xff0c;即使學到第八章&#xff0c;你還是寫不出正常的程序…因為到現在為止還沒告訴你入口點模塊怎么寫&#xff0c;IO部分也留在了最后幾章才介紹。最重要的是&#xff0c;沒有系統的總結數據類…

組合問題 已知組合數_組合和問題

組合問題 已知組合數Description: 描述&#xff1a; This is a standard interview problem to make some combination of the numbers whose sum equals to a given number using backtracking. 這是一個標準的面試問題&#xff0c;它使用回溯功能將總和等于給定數字的數字進…

可變參數模板、右值引用帶來的移動語義完美轉發、lambda表達式的理解

可變參數模板 可變參數模板對參數進行了高度泛化&#xff0c;可以表示任意數目、任意類型的參數&#xff1a; 語法為&#xff1a;在class或者typename后面帶上省略號。 Template<class ... T> void func(T ... args) {// }T:模板參數包&#xff0c;args叫做函數參數包 …