[轉載] Python列表操作

參考鏈接: Python中的基本運算符

Python列表: 序列是Python中最基本的數據結構。序列中的每個元素都分配一個數字 - 它的位置,或索引,第一個索引是0,第二個索引是1,依此類推; Python有6個序列的內置類型,但最常見的是列表和元組; 序列都可以進行的操作包括索引,切片,加,乘,檢查成員; 此外,Python已經內置確定序列的長度以及確定最大和最小的元素的方法; 列表是最常用的Python數據類型,它可以作為一個方括號內的逗號分隔值出現; 列表的數據項不需要具有相同的類型; 列表(list)是Python以及其他語言中最常用到的數據結構之一。python使用中括號[]來解析列表。列表是可變的(mutable)–可以改變列表的內容;?

本章涉及到: append(),inster(),remove(),del,pop,count,extend,index,reverse,sort,tup元祖?

常用的列表操作符 1)+:它主要實現的是多個列表之間的拼接 2)*:主要實現的是列表的復制和添加 3)比較>,<:主要進行數據型列表的元素比較 4)and等:;邏輯運算符,可以進行列表之間的邏輯判斷 一、增加元素: 1、append() append()對于列表的操作主要實現的是在特定的列表最后添加一個元素,并且只能一次添加一個元素,并且只能在列表最后;?

>>> a = ['zhangsan','lisi','wangwu','zhaoliu']

>>> a.append('xuan')

>>> print(a)

['zhangsan', 'lisi', 'wangwu', 'zhaoliu', 'xuan']

?

2、inster() inster() 對于列表的操作主要是在列表的特定位置添加想要添加的特定元素,也就是將對象插入到列表中;?

>>> a.insert(2,'jiang')

>>> print(a)

['zhangsan', 'lisi', 'jiang', 'wangwu', 'zhaoliu', 'xuan']

?

二、刪除元素: 1、a.remove() a.remove的作用是移除掉列表a里面的特定元素;?

>>> a = ['zhangsan','lisi','wangwu','zhaoliu']

>>> a.remove('lisi')

>>> print(a)

['zhangsan', 'wangwu', 'zhaoliu']

?

2、del a[n] 它的作用是刪除掉列表里面的索引號位置為n 的元素,這里需要注意的是del是一種操作語句;?

>>> a = ['zhangsan','lisi','wangwu','zhaoliu']

>>> del a[3]

>>> print(a)

['zhangsan', 'lisi', 'wangwu']

?

3、a.pop() 它的作用是將列表a的最后一個元素返回,并且在此基礎上進行刪除掉;?

>>> a = ['zhangsan','lisi','wangwu','zhaoliu']

>>> a.pop()

'zhaoliu'

>>> print(a)

['zhangsan', 'lisi', 'wangwu']

?

三、修改(重新賦值):?

>>> a = ['zhangsan','lisi','wangwu','zhaoliu']

>>> a[3]='xuan'

>>> a[0:2]=['hello','world']

>>> print(a)

['hello', 'world', 'wangwu', 'xuan']

?

四、查詢實例:?

>>> a = ['zhangsan','lisi','wangwu','zhaoliu']

>>> print(a[2])

wangwu

>>> print(a[0:3])

['zhangsan', 'lisi', 'wangwu']

>>> print(a[-1])

zhaoliu

>>> print(a[2:3])

['wangwu']

>>> print(a[0:3:1])

['zhangsan', 'lisi', 'wangwu']

>>> print(a[3:0:-1])

['zhaoliu', 'wangwu', 'lisi']

>>> print(a[:])

['zhangsan', 'lisi', 'wangwu', 'zhaoliu']

?

五、count: count方法統計某個元素在列表中出現的次數;?

>>> a = ['one','one','root','and','or','one']

>>> a.count('one')

3

>>> x = [[1,2],1,1,[2,[1,2]]]

>>> x.count(1)

2

>>> x.count([1,2])

1

?

六、extend: extend方法可以在列表的末尾一次性追加另一個序列中的多個值;?

>>> a = [1,2,3]

>>> b = [4,5,6]

>>> a.extend(b)

>>> a

[1, 2, 3, 4, 5, 6]

>>> b

[4, 5, 6]

?

extend方法修改了被擴展的列表,而原始的連接操作(+)則不然,它會返回一個全新的列表;?

>>> a = [1,2,3]

>>> b = [4,5,6]

>>> a + b

[1, 2, 3, 4, 5, 6]

>>> a

[1, 2, 3]

>>> b

[4, 5, 6]

?

七、index: index方法用于從列表中找出某個值第一個匹配項的索引位置;?

>>> a = ['zhangsan','lisi','wangwu','zhaoliu']

>>> a.index('lisi')

1

>>> a.index('zhaoliu')

3

?

八、reverse: reverse方法將列表中的元素反向存放;?

>>> a = ['zhangsan','lisi','wangwu','zhaoliu']

>>> a.reverse()

>>> print(a)

['zhaoliu', 'wangwu', 'lisi', 'zhangsan']

?

九、sort: sort方法用于在原位置對列表進行排序;?

>>> x = [4,6,2,8,3,9,0]

>>> x.sort()

>>> print(x)

[0, 2, 3, 4, 6, 8, 9]

>>> x.sort(reverse = True)? ? ? ? #sort和reverse組合

>>> print(x)

[9, 8, 6, 4, 3, 2, 0]

?

十、tuple(元祖),不可變的,但可以包括可變對象; tup1 = () #空元祖; tup2 = (19,) #一個元素,需要在元素后添加逗號; 1,對于一些不希望被修改的數據可以使用元祖; 2、元祖可以映射(和集合的成員)中當做鍵使用–而列表則不行; 元祖作為很多內鍵函數的方法的返回值存在;

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

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

相關文章

「原創」從馬云、馬化騰、李彥宏的對話,看出三人智慧差在哪里?

在今年中國IT領袖峰會上&#xff0c;馬云、馬化騰、李彥宏第一次單獨合影&#xff0c;同框畫面可以說很難得了。BAT關心的走勢一直是同行們競相捕捉的熱點&#xff0c;所以三位大Boss在這次大會上關于人工智能的見解&#xff0c;也受到廣泛關注與多方解讀。馬云認為機器比人聰明…

python 注釋含注釋_Python注釋

python 注釋含注釋Python注釋 (Python comments) Comments in Python are used to improve the readability of the code. It is useful information given by the programmer in source code for a better understanding of code and logic that they have used to solve the …

C2的完整形式是什么?

C2&#xff1a;核心2 (C2: Core 2) C2 is an abbreviation of "Core 2" or "Intel Core 2". C2是“ Core 2”或“ Intel Core 2”的縮寫 。 It is a family of Intels processor which was launched on the 27th of July, 2006. It comprises a series of…

scala特性_Scala | 特性應用

scala特性特性應用 (Trait App) Scala uses a trait called "App" which is used to convert objects into feasible programs. This conversion is done using the DelayedInit and the objects are inheriting the trait named App will be using this function. T…

[轉載] Python3中的表達式運算符

參考鏈接&#xff1a; Python中的除法運算符 1&#xff1a;Python常用表達式運算符 yield 生成器函數send協議 lambda args:expression 創建匿名函數 x if y else z 三元選擇表達式(當y為真時&#xff0c;x才會被計算) x or y 邏輯或(僅但x為假時y才會被計算) x and …

字符串矩陣轉換成長字符串_字符串矩陣

字符串矩陣轉換成長字符串Description: 描述&#xff1a; In this article, we are going to see how backtracking can be used to solve following problems? 在本文中&#xff0c;我們將看到如何使用回溯來解決以下問題&#xff1f; Problem statement: 問題陳述&#xf…

pythonchallenge_level2

level2 地址&#xff1a;http://www.pythonchallenge.com/pc/def/ocr.html。 源碼&#xff1a;gitcode.aliyun.com:qianlizhixing12/PythonChallenge.git。 問題&#xff1a;找出頁面源碼一點提示注釋中的稀有字符。 #!/usr/bin/env python3 # -*- coding:UTF-8 -*-# Level 2im…

[轉載] python類運算符的重載

參考鏈接&#xff1a; Python中的運算符重載 alist input().split() blist input().split() n float(input()) class Vector: def __init__(self, x0, y0, z0): # 請在此編寫你的代碼(可刪除pass語句) self.X x self.Y y self.Z z # 代碼結束 def __add__(self, other):…

r語言 運算符_R語言運算符

r語言 運算符R語言中的運算符 (Operators in R Language) Generally speaking, an operator is a symbol that gives proper commands to the compiler regarding a specific action to be executed. The operators are used for carrying out the mathematical or logical cal…

[轉載] Python基礎之類型轉換與算術運算符

參考鏈接&#xff1a; Python中的運算符函數| 1 一、注釋 1.注釋&#xff1a;對程序進行標注和說明&#xff0c;增加程序的可讀性。程序運行的時候會自動忽略注釋。 2.單行注釋&#xff1a;使用#的形式。但是#的形式只能注釋一行&#xff0c;如果有多行&#xff0c;就不方便…

java awt 按鈕響應_Java AWT按鈕

java awt 按鈕響應The Button class is used to implement a GUI push button. It has a label and generates an event, whenever it is clicked. As mentioned in previous sections, it extends the Component class and implements the Accessible interface. Button類用于…

解決“由于應用程序的配置不正確,應用程序未能啟動,重新安裝應用程序可能會糾正這個問題”...

在VS2005下用C寫的程序&#xff0c;在一臺未安裝VS2005的系統上&#xff0c; 用命令行方式運行&#xff0c;提示&#xff1a; “系統無法執行指定的程序” 直接雙擊運行&#xff0c;提示&#xff1a; “由于應用程序的配置不正確&#xff0c;應用程序未能啟動&#xff0c;重新安…

qgis在地圖上畫導航線_在Laravel中的航線

qgis在地圖上畫導航線For further process we need to know something about it, 為了進一步處理&#xff0c;我們需要了解一些有關它的信息&#xff0c; The route is a core part in Laravel because it maps the controller for sending a request which is automatically …

Logistic回歸和SVM的異同

這個問題在最近面試的時候被問了幾次&#xff0c;讓談一下Logistic回歸&#xff08;以下簡稱LR&#xff09;和SVM的異同。由于之前沒有對比分析過&#xff0c;而且不知道從哪個角度去分析&#xff0c;一時語塞&#xff0c;只能不知為不知。 現在對這二者做一個對比分析&#xf…

[轉載] python學習筆記2--操作符,數據類型和內置功能

參考鏈接&#xff1a; Python中的Inplace運算符| 1(iadd()&#xff0c;isub()&#xff0c;iconcat()…) 什么是操作符&#xff1f; 簡單的回答可以使用表達式4 5等于9&#xff0c;在這里4和5被稱為操作數&#xff0c;被稱為操符。 Python語言支持操作者有以下幾種類型。 算…

scala bitset_Scala中的BitSet

scala bitsetScala BitSet (Scala BitSet) Set is a collection of unique elements. 集合是唯一元素的集合。 Bitset is a set of positive integers represented as a 64-bit word. 位集是一組表示為64位字的正整數。 Syntax: 句法&#xff1a; var bitset : Bitset Bits…

構建安全網絡 比格云全系云產品30天內5折購

一年之計在于春&#xff0c;每年的三、四月&#xff0c;都是個人創業最佳的起步階段&#xff0c;也是企業采購最火熱的時期。為了降低用戶的上云成本&#xff0c;讓大家能無門檻享受到優質高性能的云服務&#xff0c;比格云從3月16日起&#xff0c;將上線“充值30天內&#xff…

python中 numpy_Python中的Numpy

python中 numpyPython中的Numpy是什么&#xff1f; (What is Numpy in Python?) Numpy is an array processing package which provides high-performance multidimensional array object and utilities to work with arrays. It is a basic package for scientific computati…