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

歡迎訪問 生活随笔!

生活随笔

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

python

Python编程从入门到实践~操作列表

發布時間:2024/7/23 python 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python编程从入门到实践~操作列表 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

列表是什么

#列表是什么 bicycles = ["trek", "cannodale", "redline","specialized"] print(bicycles)#訪問列表元素 print(bicycles[0])#使用列表中的各個值 message = f"My first bicycle was a {bicycles[0].title()}" print(message)

修改、添加和刪除元素

#修改列表元素 motorcyles = ["honda", "yamaha", "suzuki"] motorcyles[0] = 'ducati' print(motorcyles)#在列表中添加元素 ##1.在列表末尾添加元素 motorcyles.append('benz') print(motorcyles)##2.在列表中插入元素 motorcyles.insert(1, "BMW") print(motorcyles)#從列表中刪除元素 ##1.使用del語句刪除元素 del motorcyles[0] print(motorcyles)##2.使用pop()刪除列表末尾元素 element = motorcyles.pop() print(f"delete element:{element}") print(motorcyles)##3.pop(i) 彈出指定位置元素 motorcyles = ["honda", "yamaha", "suzuki", "BMW"] element = motorcyles.pop(1) print(f"delete element:{element}") print(motorcyles)##4.根據值刪除元素 motorcyles.remove('suzuki') print(motorcyles)#清空列表 motorcyles.clear() print(motorcyles)

組織列表

#使用方法sort()對列表永久排序 cars = ["bmw", "audi", "toyota", "subaru"] cars.sort() print(cars)#使用sorted()對列表臨時排序 cars = ["bmw", "audi", "toyota", "subaru"] print(sorted(cars)) print(cars)#列表倒序 cars = ["bmw", "audi", "toyota", "subaru"] cars.reverse() print(cars)#獲取列表長度 print(f"length of list:{len(cars)}")

遍歷列表

#遍歷列表 magicians = ["alice","david","carolina"] for magic in magicians:print(magic)#在for循環中執行更多操作 magicians = ["alice","david","carolina"] for magic in magicians:print(f"{magic.title()}, that was a great trick")print(f"I can't wait to see you next trick, {magic.title()}.\n") print("Thank you, everyone. That was a great magic show!")

創建數值列表

#使用函數range() for value in range(1, 6):print(value)#range()轉列表 numbers = list(range(1, 6)) print(numbers)#對數字列表統計計算 print(min(numbers)) print(max(numbers)) print(sum(numbers))#列表解析(元素平方) squares = [value**2 for value in range(1, 6)] print(squares)

使用列表一部分

#切片 players = ["charles", "martina", "michael", "florence", "eli"] print(players[0:3])#遍歷切片 players = ["charles", "martina", "michael", "florence", "eli"] for player in players[:3]:print(player)#復制列表 my_foods = ["pizza", "falafel", "carrot", "cake"] friend_foods = my_foods[:] friend_foods.append("apple") print(f"My foods:{my_foods}") print(f"Friend foods:{friend_foods}")

元組

#定義元組 dimensions = (100, 200, 300) print(dimensions) print(dimensions[1])#遍歷元組中的所有值 for dim in dimensions:print(dim)#修改元組變量 dimensions = (100, 200, 300) print(dimensions) dimensions = (400, 500, 600) print(dimensions)

總結

以上是生活随笔為你收集整理的Python编程从入门到实践~操作列表的全部內容,希望文章能夠幫你解決所遇到的問題。

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