python-list:列表-元組-字符串

列表

“列表”是一個值,它包含多個字構成的序列。術語“列表值”指的是列表本身(它作為一個值,可以保存在變量中、傳遞給函數)--:按下標取值、切片、for循環、用于len()以及in not in等

list ['aa','bb','cc','dd']是一個簡單的列表

1.用列表下標取值

  list =['aa','bb','cc','dd']

  list[0]--aa

  list[1]--bb

  list[-1]--dd

2.切片取值(從列表中取得一個或多個值,返回結果是列表)

  list =['aa','bb','cc','dd']

  list[:3]--['aa','bb','cc'] ?#顧頭不顧尾?

  list[:1]--['aa']

  list[0:-1]--['aa','bb','cc']

3.用len( )獲取列表的長度

  list ['aa','bb','cc','dd']

  len(list)--4

4.用下標改變列表中的值

  list =['aa','bb','cc','dd']

  list[0]='AA' --?['AA','bb','cc','dd']

  list[0]=list[1]----?['bb','bb','cc','dd']

5.列表連接和列表復制

  list= ['aa','bb','cc','dd']

  num=['11','22','33','44']

  list+num--['aa', 'bb', 'cc', 'dd', '11', '22', '33', '44']

  list+['AA','BB']---['aa', 'bb', 'cc', 'dd','AA','BB']

  list*2--['aa', 'bb', 'cc', 'dd' ,'aa', 'bb', 'cc', 'dd']

6.用del從列表中刪除值

  list= ['aa','bb','cc','dd']

  del list[0]--?['bb','cc','dd']

7.使用列表

cataNames = []
while True:
print ('enter the name of cat'+str(len(cataNames)) + '(or enter nothing to stop.):' )
name = str(input())
if name =='':
break
cataNames = cataNames+[name]
print ('the cat names are:')
for name in cataNames:
print (''+name)

C:\Python27\python.exe C:/Users/xiu/PycharmProjects/H5/day1/22.py
enter the name of cat0(or enter nothing to stop.):
'cat01'
enter the name of cat1(or enter nothing to stop.):
'cat02'
enter the name of cat2(or enter nothing to stop.):
'cat03'
enter the name of cat3(or enter nothing to stop.):
' '
the cat names are:
cat01
cat02
cat03

Process finished with exit code 0

?列表用于循環

list = ['cat001','cat002','cat003']
for i in range(len(list)):
print ('index ' + str(i)+ 'in list is:'+ list[i])

C:\Python27\python.exe C:/Users/xiu/PycharmProjects/H5/day1/22.py
index 0in list is:cat001
index 1in list is:cat002
index 2in list is:cat003

?

8.in和not in操作符

  list= ['aa','bb','cc','dd']

  print 'aa' in list--True

  print 'aa1' in list--False


9.對變量增強的賦值操作(+、-、*、/、%)
  aa = 10
  aa +=2 #aa = aa +2 輸出12
  aa -=2 #aa = aa-2 輸出10
  aa *=2 #aa = aa *2 輸出20
  aa /=2 #aa = aa/2 輸出10
  aa %=2 #aa = aa % 2 輸出0
10.用index()方法在列表中查找值:根據元素值查元素下標
  list= ['aa','bb','cc','dd','cc']
  print list.index('bb') #1
  print list.index('cc') #2:若列表有多個相同元素值,則返回第一個

11.用append()/insert()方法在列表中添加值
  list= ['aa','bb','cc','dd']
  list.append('ee') #['aa', 'bb', 'cc', 'dd', 'ee']
  list.insert(0,'AA') #['AA', 'aa', 'bb', 'cc', 'dd', 'ee']

12.remove()刪除列表中的值
  list= ['aa','bb','cc','dd']
  list.remove('aa') #['bb', 'cc', 'dd'] 直接刪元素值
  del list[0] #['cc', 'dd'] 通過下標刪元素值

13.用sort()方法將列表中的值排序(只能對純數字、純字母的列表進行排序)
  list= ['dd','cc','bb','aa']
  list.sort() #['aa', 'bb', 'cc', 'dd'] 默認升序排列
  list.sort(reverse=True) #['dd', 'cc', 'bb', 'aa']
  list.sort(reverse=False) #['aa', 'bb', 'cc', 'dd']
  list= ['dd','cc','bb','aa','AA','BB','CC','DD']
  list.sort() #['AA', 'BB', 'CC', 'DD', 'aa', 'bb', 'cc', 'dd'] 使用‘ASCII字符排序’,因此大寫字母會在小寫字母之前


字符串
1.列表的很多操作,也可以用于字符串
name = 'python'
print(name[0]) #p
print(name[-1]) #n
print(name[0:3]) #pyt
print(name[::-1]) #nohtyp
print('py' in name) #True
print('PJ' in name) #False
for i in name:
print(i) # p y t h o n

?

元組

列表數據類型的不可變形式

1.元組的樣子
color = ('red','blue','yellow','black')
print(color[0]) #red
print(color[0:2]) #('red', 'blue')
print(color[::-1]) #('black', 'yellow', 'blue', 'red')

2.元組只有一個值,末尾要加上英文逗號
color1=('aa')
color2=('aa',)
print(type(color1)) #<class 'str'>
print(type(color2)) #<class 'tuple'>

3.用list()和tuple函數轉換類型
list1=[1,2,3,4]
tuple1=('a','b','c','d')

print(list(tuple1)) #['a', 'b', 'c', 'd']
print(tuple(list1)) #(1, 2, 3, 4)




  
  

?

  

轉載于:https://www.cnblogs.com/ermm/p/7286465.html

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

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

相關文章

創建相似對象,就交給『工廠模式』吧

源碼&#xff1a; 源代碼C# 系列導航&#xff1a; 目錄 定義&#xff08;Factory Pattern&#xff09;&#xff1a; 用來創建目標對象的類&#xff0c;將相似對象的創建工作統一到一個類來完成。 一、簡單工廠模式&#xff1a; 代碼&#xff1a; /// <summary>/// 產品枚…

《ASP.NET Core 6框架揭秘》實例演示[26]:跟蹤應用接收的每一次請求

很多人可能對ASP.NET Core框架自身記錄的診斷日志并不關心&#xff0c;其實這些日志對糾錯排錯和性能監控提供了很有用的信息。如果需要創建一個APM&#xff08;Application Performance Management&#xff09;系統來監控ASP.NET Core應用處理請求的性能及出現的異常&#xff…

C語言循環為1404的循環,考試,求大神幫忙,C語言,小弟感激不盡

若有定義語句&#xff1a;int a10; double b3.14;&#xff0c;則表達式Aab值的類型是___________。  (1)A).char B)int C) double D)float(2)若有定義語句&#xff1a;int x12,y8,z;&#xff0c;在其后執行語句z0.9x/y;&#xff0c;則z的值為___________。A)1.9 B)1 C)2 D)2.…

js題集19

1.實現斐波那契數列。達到題目中的效果。不知道斐波那契數列是啥的請自行百度。 function fibonacci(){ } var ffibonacci(); for(var i0;i<10;i){ console.log(f()); } //output:按順序輸出斐波那契數列的數字。 eg&#xff1a; 1 2 3 5 8 13 21 34 55 89轉載于:https://ww…

阿里云Maven鏡像配置

2019獨角獸企業重金招聘Python工程師標準>>> <mirror><id>alimaven</id> <name>aliyun maven</name> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> <mirrorOf>central</mirrorOf> …

c語言中有12個球,數學老師做不出來的一道邏輯推理題

同志們 那個球不一定輕啊正確的是平分三份 取兩分稱if(平)。。。。。。在未稱過的4球中取兩個放左邊 和標準的球稱(稱過的球一定標準)。。。。。。if(平)。。。。。。。。。。。。在兩次都未稱過的球中取一個 和標準的稱。。。。。。。。。。。。if(平)。。。。。。。。。。。。…

WPF 實現彈幕效果

WPF 實現彈幕效果控件名&#xff1a;BarrageExample作者&#xff1a;WPFDevelopersOrg原文鏈接&#xff1a; https://github.com/WPFDevelopersOrg/WPFDevelopers框架使用大于等于.NET40&#xff1b;Visual Studio 2022;項目使用 MIT 開源許可協議&#xff1b;此篇代碼目的只…

js題集23

1.實現函數--defaultArguments 功能如下&#xff1a; function add(a,b) { return ab;}; var add_ defaultArguments(add,{b:9}); add_(10); // returns 19 add_(10,7); // returns 17 add_(); // returns NaN add_ defaultArguments(add_,{b:3, a:2}); add_(10); // returns…

iteritems()與items()

iteritems&#xff1a;以迭代器對象返回字典鍵值對 item:以列表形式返回字典鍵值對 >>> dic {a:3,c:1,b:2} >>> print dic.iteritems() <dictionary-itemiterator object at 0x7fa381599628> >>> print dic.items() [(a, 3), (c, 1), (b, 2)…

WPF效果第一百九十八篇之模塊對比

前面效果中分享了彩色馬蹄圖的效果和范圍內拖拽;這不大假期的時間反正沒啥事就在家擼代碼;今天又是LisBox實現的效果,看最終效果:1、剛開始一朋友說用DataGrid來實現.首先把行對象轉換成列對象,至于控制列的話,就后臺重新賦值對象來控制前臺.我是覺得太費勁直接放棄了;還是首選…

android 與后臺通信,Android后臺線程和UI線程通訊實例

本節向你展示如何在任務中發送數據給UI線程里的對象&#xff0c;這個特性允許你在后臺線程工作&#xff0c;完了在UI線程展示結果。在UI線程定義一個HandlerHandler是Android系統線程管理框架里的一部分。一個Handler對象接收消息&#xff0c;并且運行代碼來處理消息。正常情況…

saltstack的狀態文件

saltstack狀態文件設定&#xff1a;編輯/etc/salt/master&#xff0c;修改其中關于“設置文件的目錄”的設置&#xff1a;說明&#xff1a;注意語法格式&#xff0c;頂格/冒號/兩個空格state_top: top.sls # The state system uses a "top" file to tell the minions…

POJ 2798:二進制轉換十六進制

很郁悶&#xff0c;這道題一直WA&#xff0c;然而本地我測了好幾組數據都是通過的&#xff0c;上網找了網友陳宇龍加油加油加油的AC的代碼&#xff0c;http://blog.csdn.net/Since_natural_ran/article/details/51742149&#xff0c;發現沒有什么不同。。。很無語。。 #include…

【Shashlik.EventBus】.NET 事件總線,分布式事務最終一致性簡介

分布式事務、CAP定理、事件總線&#xff0c;在當前微服務、分布式、集群大行其道的架構前提下&#xff0c;是不可逃避的幾個關鍵字&#xff0c;在此不會過多闡述相關的理論知識。Shashlik.EventBus就是一個基于.NET6的開源事件總線解決方案&#xff0c;同時也是分布式事務最終一…

5個超實用的Visual Studio插件

工欲善其事&#xff0c;必先利其器,整理的一些我必裝的5款Visual Studio插件&#xff0c;希望你們能get到。01 CodeMaidCodeMaid快速整理代碼文件&#xff0c;規范你的代碼&#xff0c;提高代碼閱讀體驗。代碼自動對齊&#xff0c;格式化代碼&#xff08;ps&#xff1a;不用再按…

BZOJ1509: [NOI2003]逃學的小孩(樹的直徑)

Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 1126 Solved: 567[Submit][Status][Discuss]Description Input 第一行是兩個整數N&#xff08;3 ? N ? 200000&#xff09;和M&#xff0c;分別表示居住點總數和街道總數。以下M行&#xff0c;每行給出一條街道的信息。第i1行…

Blazor University (52)依賴注入 —— 擁有多個依賴項:正確的方式

原文鏈接&#xff1a;https://blazor-university.com/dependency-injection/component-scoped-dependencies/owning-multiple-dependencies-the-right-way/擁有多個依賴項&#xff1a;正確的方式在上一節[1]中&#xff0c;我們看到了將多個擁有的依賴項注入組件的錯誤方法。本節…

Gradle 1.12用戶指南翻譯——第五十四章. 構建原生二進制文件

其他章節的翻譯請參見&#xff1a;http://blog.csdn.net/column/details/gradle-translation.html翻譯項目請關注Github上的地址&#xff1a;https://github.com/msdx/gradledoc本文翻譯所在分支&#xff1a;https://github.com/msdx/gradledoc/tree/1.12。直接瀏覽雙語版的文檔…

android 調用c wcf服務,如何使用命名管道從c調用WCF方法?

更新&#xff1a;通過協議here,我無法弄清楚未知的信封記錄.我在網上找不到任何例子.原版的&#xff1a;我有以下WCF服務static void Main(string[] args){var inst new PlusFiver();using (ServiceHost host new ServiceHost(inst,new Uri[] { new Uri("net.pipe://loc…

VK Cup 2015 - Qualification Round 1 A. Reposts(樹)

傳送門 Description One day Polycarp published a funny picture in a social network making a poll about the color of his handle. Many of his friends started reposting Polycarps joke to their news feed. Some of them reposted the reposts and so on. These event…