国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

python3 0基礎學習----數(shù)據(jù)結構(基礎+練習)

這篇具有很好參考價值的文章主要介紹了python3 0基礎學習----數(shù)據(jù)結構(基礎+練習)。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

?? 幾種常見數(shù)據(jù)結構

列表 (List)

1. 定義

列表是一種有序的可變序列,可以包含不同類型的元素。列表可以通過方括號 [] 來表示,元素之間用逗號分隔。

注釋: 注意列表可變,字符串不可變,只能改變大小寫

2. 實例:

my_list = [1, 'hello', 3.14, True]

3. 列表中常用方法

.append(要添加內容) 向列表末尾添加數(shù)據(jù)

python3 0基礎學習----數(shù)據(jù)結構(基礎+練習),MySQL+python,學習,數(shù)據(jù)結構,windows

.extend(列表) 將可迭代對象逐個添加到列表中

python3 0基礎學習----數(shù)據(jù)結構(基礎+練習),MySQL+python,學習,數(shù)據(jù)結構,windows

.insert(索引,插入內容) 向指定位置插入內容

python3 0基礎學習----數(shù)據(jù)結構(基礎+練習),MySQL+python,學習,數(shù)據(jù)結構,windows

.remove(刪除內容) 刪除指定內容

python3 0基礎學習----數(shù)據(jù)結構(基礎+練習),MySQL+python,學習,數(shù)據(jù)結構,windows

.pop(索引) 刪除指定索引處內容并返回刪除內容

python3 0基礎學習----數(shù)據(jù)結構(基礎+練習),MySQL+python,學習,數(shù)據(jù)結構,windows

.index(要查詢內容) 返回一個與制定元素匹配的索引,不改變原列表

python3 0基礎學習----數(shù)據(jù)結構(基礎+練習),MySQL+python,學習,數(shù)據(jù)結構,windows

.count(要查詢內容) 返回列表中該元素出現(xiàn)次數(shù)

python3 0基礎學習----數(shù)據(jù)結構(基礎+練習),MySQL+python,學習,數(shù)據(jù)結構,windows

.sort() 同類型排序(默認升序),不同類型會報錯TypeError: ‘<’ not supported between instances of ‘int’ and ‘str’

python3 0基礎學習----數(shù)據(jù)結構(基礎+練習),MySQL+python,學習,數(shù)據(jù)結構,windows

.reverse() 反向排序,不分類型

python3 0基礎學習----數(shù)據(jù)結構(基礎+練習),MySQL+python,學習,數(shù)據(jù)結構,windows

元組(Tuple)

1. 定義

元組是一種有序的不可變序列,同樣可以包含不同類型的元素。元組可以通過圓括號 () 來表示,元素之間用逗號分隔。

2. 實例:

my_tuple = (1, 'hello', 3.14, True)

元組輸出的是列表的子集

python3 0基礎學習----數(shù)據(jù)結構(基礎+練習),MySQL+python,學習,數(shù)據(jù)結構,windows

3. 元組中常用方法

因為元組是不可修改的所以只能查詢,如果要修改得先轉換成列表進行修改,之后在轉換成元組

x = (1,5,'i','j')
# x.sort() #報錯 AttributeError: 'tuple' object has no attribute 'sort'
print(x[1]) #輸出: 5
x[1] = 6 #報錯 TypeError: 'tuple' object does not support item assignment

python3 0基礎學習----數(shù)據(jù)結構(基礎+練習),MySQL+python,學習,數(shù)據(jù)結構,windows

.index(element) 返回第一個與制定元素相等的元素的索引

python3 0基礎學習----數(shù)據(jù)結構(基礎+練習),MySQL+python,學習,數(shù)據(jù)結構,windows

.count(要查詢內容) 返回列表中該元素出現(xiàn)次數(shù)python3 0基礎學習----數(shù)據(jù)結構(基礎+練習),MySQL+python,學習,數(shù)據(jù)結構,windows
修改元組內容

python3 0基礎學習----數(shù)據(jù)結構(基礎+練習),MySQL+python,學習,數(shù)據(jù)結構,windows

字典(Dictionary)

1. 定義

字典是一種鍵值對的集合,鍵和值可以是任意的數(shù)據(jù)類型。字典可以通過花括號 {} 來表示,每個鍵值對使用冒號 : 分隔,鍵值對之間用逗號分隔??勺鰞热菪薷?/p>

a={age:10}
a['age']=18
print(a) #輸出 {'age': 18}

字典里邊沒有順序 ,列表有從0開始
字典是直接刪除重新加入,所以沒有順序

2. 實例:

my_dict = {'name': 'John', 'age': 25, 'city': 'New York'}

3. 字典中常用方法

in和not in方法
a={'age':10,'name':'xiaoming'}
print('name' in a)  #輸出 True
print('s' not in a)  #輸出 True
for 鍵 in 字典

可以通過dict(?。?鍵值

for in 和items()結合使用

for 健,鍵值 in 字典.items()

a = {'age':10,'name':'xiaoming'}
for (k,v) in a.items():  #()可加可不加
    print(k,v)

print(k,v) 輸出:

age 10
name xiaoming
.keys() 返回一個包含字典所有的視圖

python3 0基礎學習----數(shù)據(jù)結構(基礎+練習),MySQL+python,學習,數(shù)據(jù)結構,windows

.values() 返回一個包含字典所有的視圖

python3 0基礎學習----數(shù)據(jù)結構(基礎+練習),MySQL+python,學習,數(shù)據(jù)結構,windows

get(key, default):返回指定鍵的值,如果鍵不存在,則返回默認值(default)。

python3 0基礎學習----數(shù)據(jù)結構(基礎+練習),MySQL+python,學習,數(shù)據(jù)結構,windows

.pop(key):移除指定鍵的鍵值對,并返回鍵對應的值。

python3 0基礎學習----數(shù)據(jù)結構(基礎+練習),MySQL+python,學習,數(shù)據(jù)結構,windows

.popitem():隨機移除并返回一個鍵值對

python3 0基礎學習----數(shù)據(jù)結構(基礎+練習),MySQL+python,學習,數(shù)據(jù)結構,windows

.update(): 使用其他字典內容更新當前字典

python3 0基礎學習----數(shù)據(jù)結構(基礎+練習),MySQL+python,學習,數(shù)據(jù)結構,windows

.clear():移除字典中的所有鍵值對。

python3 0基礎學習----數(shù)據(jù)結構(基礎+練習),MySQL+python,學習,數(shù)據(jù)結構,windows

.items() : 用于以鍵-值對(key-value pairs)
my_dict = {'name': 'Alice', 'age': 25, 'city': 'New York'}
items = my_dict.items()
print(items)

輸出: dict_items([(‘name’, ‘Alice’), (‘age’, 25), (‘city’, ‘New York’)])

集合(Set)

1. 定義

集合是一種無序的、不重復的元素的集合。集合可以通過花括號 {} 或 set() 函數(shù)來創(chuàng)建

2. 實例:

my_set = {1, 2, 3, 4, 5}

3. 集合常用方法

.add(element)向集合隨機添加元素(因為無序所以隨機)

python3 0基礎學習----數(shù)據(jù)結構(基礎+練習),MySQL+python,學習,數(shù)據(jù)結構,windows

.remove(element)從集合中刪除某元素,如果該集合沒有該元素返回錯誤KeyError

python3 0基礎學習----數(shù)據(jù)結構(基礎+練習),MySQL+python,學習,數(shù)據(jù)結構,windows
python3 0基礎學習----數(shù)據(jù)結構(基礎+練習),MySQL+python,學習,數(shù)據(jù)結構,windows
python3 0基礎學習----數(shù)據(jù)結構(基礎+練習),MySQL+python,學習,數(shù)據(jù)結構,windows

.discard(element)從集合中刪除某元素,如果該集合沒有該元素也不會報錯

python3 0基礎學習----數(shù)據(jù)結構(基礎+練習),MySQL+python,學習,數(shù)據(jù)結構,windows

.pop()隨機移初一個元素,并返回該元素(集合是無序的,無法確定刪除的元素是那個)

python3 0基礎學習----數(shù)據(jù)結構(基礎+練習),MySQL+python,學習,數(shù)據(jù)結構,windows

.clear() 清除集合中所有元素,輸出set()

python3 0基礎學習----數(shù)據(jù)結構(基礎+練習),MySQL+python,學習,數(shù)據(jù)結構,windows

.copy():復制一個集合

python3 0基礎學習----數(shù)據(jù)結構(基礎+練習),MySQL+python,學習,數(shù)據(jù)結構,windows

字符串(String)

1. 定義

字符串是一種由字符組成的不可變序列,可以用單引號或雙引號括起來

2. 實例:

my_string = 'Hello, World!'

3. 集合常用方法

上篇文章有寫,跳轉地址python3 0基礎學習----基本知識
python3 0基礎學習----數(shù)據(jù)結構(基礎+練習),MySQL+python,學習,數(shù)據(jù)結構,windows

?? 常用的內置函數(shù)

1. print(): 將制定的值輸出到控制臺

print('hi~') #輸出 hi~

2. len(sequence): 返回序列的長度(元素個數(shù))

python3 0基礎學習----數(shù)據(jù)結構(基礎+練習),MySQL+python,學習,數(shù)據(jù)結構,windows

3. type(object): 返回對象的類型

python3 0基礎學習----數(shù)據(jù)結構(基礎+練習),MySQL+python,學習,數(shù)據(jù)結構,windows

4. input(‘請輸入’) : 獲取用戶輸入數(shù)據(jù)

python3 0基礎學習----數(shù)據(jù)結構(基礎+練習),MySQL+python,學習,數(shù)據(jù)結構,windows

5. range(初值, 終值, 步長)內置函數(shù),返回的是一個可迭代對象。用于創(chuàng)建數(shù)字序列

for num in range(5):
    print(num)  # 輸出: 0, 1, 2, 3, 4

for num in range(2, 7):
    print(num)  # 輸出: 2, 3, 4, 5, 6

for num in range(1, 10, 2):
    print(num)  # 輸出: 1, 3, 5, 7, 9

6. int(x)、float(x)、str(x)、bool(x) 等:將輸入值轉換為整數(shù)、浮點數(shù)、字符串或布爾值類型。

num1 = int("10")
num2 = float("3.14")
text = str(42)
flag = bool(1)

7. max(iterable)、min(iterable):返回可迭代對象中的最大值和最小值。

my_list = [3, 1, 5, 2, 4]
max_value = max(my_list)
min_value = min(my_list)
print(max_value) #輸出 5
print(min_value) #輸出 1 

python3 0基礎學習----數(shù)據(jù)結構(基礎+練習),MySQL+python,學習,數(shù)據(jù)結構,windows

8. sum() 返回可迭代對象的和(用于數(shù)組類型的對象)

python3 0基礎學習----數(shù)據(jù)結構(基礎+練習),MySQL+python,學習,數(shù)據(jù)結構,windows

9. abs(x):返回數(shù)值的絕對值。

s = abs(-10)
print(s) #輸出 10

10. round(number, ndigits):將數(shù)值四舍五入到指定的小數(shù)位數(shù)

rounded_num = round(3.14159, 2)
print(rounded_num) #輸出3.14

11. dir() 函數(shù)不帶參數(shù)時,返回當前范圍內的變量、方法和定義的類型列表;帶參數(shù)時,返回參數(shù)的屬性、方法列表

>>>dir()   #  獲得當前模塊的屬性列表
['__builtins__', '__doc__', '__name__', '__package__', 'arr', 'myslice']
>>> dir([ ])    # 查看列表的方法
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__setslice__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
>>>

?? 結合實戰(zhàn)練習

1. 列表中in和not in 的使用

some = [1,2,3,4,5,6]
print(3 in some) #輸出 True
print(3 not in some) #輸出 False

練習2 :讀取a.txt文件,對每一行使用split()方法拆分為單詞列表,對每一行單詞進行篩選去重復,添加到新的列表中。最后使用sort()進行排序。(大概意思是提取a.txt中出現(xiàn)過的單詞生成一個列表)

1. a.txt文件內容(請忽略內容是什么意思,網上是那個隨便找的)

Dear Sir/Madam,

I am writing this email to express my gratitude to you and to discuss
some matters. I hope this email finds you in good health and high
spirits.

Firstly, I would like to sincerely thank you for your generosity and
assistance. I have been facing some difficulties in pursuing my career
goals, and your support has been invaluable to me. Your advice and
guidance have helped me gain a better understanding of the challenges
I have faced and have motivated me to continue striving.

The purpose of this email is to request a meeting with you in order to
personally express my gratitude. I would like the opportunity to
showcase the progress I have made in my professional development and
to hear your valuable insights. If you are willing, I can arrange the
meeting according to your convenience, and the location and date can
be adjusted according to your preferences.

Furthermore, I wanted to inquire if there is anything else I can do
for you. Your generosity may have an impact not only on me personally
but also on other individuals I may be able to assist. Please let me
know if there is anything you need help with, as I would be more than
happy to offer my assistance.

Once again, thank you for your support and generosity, and I hold
great expectations for the future. I sincerely look forward to meeting
with you and expressing my gratitude in person. If you have any
questions or require further information regarding the meeting, please
feel free to contact me.

With heartfelt appreciation,

[Your Name]

2. 提取單詞思路

遍歷文件每行內容
拆分每行內容為單詞列表
遍歷當前行列表單詞
查找list中是否存在當前單詞,存在記錄出現(xiàn)個數(shù),不存在新增一條記錄

3. 代碼:

th = open('a.txt')
print('讀取文件內容',th)
lst = list()#空列表
for item in th:
    itemStr = item.rstrip()# 去除末尾空白符號
    pList = itemStr.split()# 以空格作為分隔符分割每行數(shù)據(jù),返回一個單詞列表  ,例如首行:['Dear', 'Sir/Madam,']
    for word in pList:
         if len(lst)==0:
            lst.append(word)
            continue
         if len(lst)>0:
            if lst.count(word)>0:
                continue
            else:
                lst.append(word)


print('列表長度',len(lst))
lst.sort()
print(lst)

4. 輸出結果

python3 0基礎學習----數(shù)據(jù)結構(基礎+練習),MySQL+python,學習,數(shù)據(jù)結構,windows

練習2 :讀取一份郵件,獲取到郵件中出現(xiàn)過的單詞生成字典,并記錄每個地址出現(xiàn)過的個數(shù)

1. a.txt內容和上一題一樣

2. 思路

讀取文件
聲明空字典
遍歷文件內容
去掉每行結尾空白符號
切割每行生成單詞字典

3. 代碼

th = open('a.txt')
dictStr = dict()#空字典
for item in th:
    itemStr = item.rstrip()# 去除末尾空白符號
    pDict = itemStr.split()# 以空格作為分隔符分割每行數(shù)據(jù),返回一個單詞列表  ,例如首行:['Dear', 'Sir/Madam,']
    for word in pDict:
         dictStr[word] = dictStr.get(word,0)+1 #查找。找到獲取對應值+1,沒找到默認為0+1
# print(dictStr.items())#items方法,返回可迭代對象的(key,value)
print(sorted([(k,v) for k,v in dictStr.items()]))

4. 運行結果

python3 0基礎學習----數(shù)據(jù)結構(基礎+練習),MySQL+python,學習,數(shù)據(jù)結構,windows

練習3 :在練習2中升級,輸出練習2中,單詞出現(xiàn)最多的建和鍵值

bigKey = None #最大鍵
bigValue = None  #最大鍵值
for k,v in dictStr.items():
    if bigKey is None or v>bigValue: # is判斷是否相等,or或
        bigValue = v
        bigKey = k
print(bigKey,':',bigValue)

輸出結果:to : 17文章來源地址http://www.zghlxwxcb.cn/news/detail-657913.html

到了這里,關于python3 0基礎學習----數(shù)據(jù)結構(基礎+練習)的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!

本文來自互聯(lián)網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。如若轉載,請注明出處: 如若內容造成侵權/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經查實,立即刪除!

領支付寶紅包贊助服務器費用

相關文章

  • Python基礎數(shù)據(jù)結構和操作

    字符串是 Python 中最常用的數(shù)據(jù)類型。我們一般使用引號來創(chuàng)建字符串。創(chuàng)建字符串很簡單,只要為變量分配一個值即可。 注意:控制臺顯示結果為 class \\\'str\\\' , 即數(shù)據(jù)類型為str(字符串)。 1.1 字符串特征 一對引號字符串 三引號字符串 注意:三引號形式的字符串支持換行。

    2024年01月20日
    瀏覽(25)
  • Python基礎數(shù)據(jù)結構入門必讀指南

    Python基礎數(shù)據(jù)結構入門必讀指南

    作者主頁:濤哥聊Python 個人網站:濤哥聊Python 大家好,我是濤哥,今天為大家分享的是Python中常見的數(shù)據(jù)結構。 含義:數(shù)組是一種有序的數(shù)據(jù)結構,其中的元素可以按照索引來訪問。數(shù)組的大小通常是固定的,一旦創(chuàng)建就不能更改。 基本操作: 含義:列表是Python中內置的

    2024年02月07日
    瀏覽(52)
  • Python實現(xiàn)數(shù)據(jù)結構的基礎操作

    目錄 一、列表(List) 二、字典(Dictionary) 三、集合(Set) 四、鏈表的實現(xiàn) 五、隊列和棧 ?? 數(shù)據(jù)結構是計算機科學中非常重要的概念,它用于存儲和組織數(shù)據(jù)以便有效地進行操作。Python作為一種功能強大且易于學習的編程語言,提供了許多內置的數(shù)據(jù)結構和相關操作。在

    2024年02月11日
    瀏覽(13)
  • 【Python】基礎數(shù)據(jù)結構:列表——元組——字典——集合

    【Python】基礎數(shù)據(jù)結構:列表——元組——字典——集合

    Python提供了多種內置的數(shù)據(jù)結構,包括列表( List )、元組( Tuple )和字典( Dictionary )。這些數(shù)據(jù)結構在Python編程中都有著廣泛的應用,但它們各有特點和適用場景。 列表是一種有序的集合,可以隨時添加和刪除其中的元素。列表是可變的,也就是說,你可以修改列表的

    2024年02月10日
    瀏覽(25)
  • Python-基礎篇-數(shù)據(jù)結構-列表、元組、字典、集合

    Python-基礎篇-數(shù)據(jù)結構-列表、元組、字典、集合

    列表、元組 字典、集合 ??正如在現(xiàn)實世界中一樣,直到我們擁有足夠多的東西,才迫切需要一個儲存東西的容器,這也是我堅持把數(shù)據(jù)結構放在最后面的原因一一直到你掌握足夠多的技能,可以創(chuàng)造更多的數(shù)據(jù),你才會重視數(shù)據(jù)結構的作用。這些儲存大量數(shù)據(jù)的容器,在

    2024年01月21日
    瀏覽(26)
  • 【軟考程序員學習筆記】——數(shù)據(jù)結構與算法基礎

    【軟考程序員學習筆記】——數(shù)據(jù)結構與算法基礎

    目錄 ???一、數(shù)據(jù)結構概念和分類 ??二、數(shù)組特點存儲方式 ??三、矩陣 特殊矩陣 非特殊矩陣 ??四、棧和隊列 ???五、二叉樹的性質 ??六、二叉樹的遍歷 (1)前序遍歷(先根遍歷,先序遍歷) (2)中遍歷(中根遍歷) (3)后序遍歷(后根遍歷,后序遍歷) ??七、二叉排序樹 ??八、

    2024年02月12日
    瀏覽(24)
  • JAVA基礎學習筆記-day14-數(shù)據(jù)結構與集合源碼2

    JAVA基礎學習筆記-day14-數(shù)據(jù)結構與集合源碼2

    博文主要是自己學習JAVA基礎中的筆記,供自己以后復習使用,參考的主要教程是B站的 尚硅谷宋紅康2023大數(shù)據(jù)教程 君以此始,亦必以終?!笄鹈鳌蹲髠鳌ば辍?7.1 List接口特點 List集合所有的元素是以一種 線性方式 進行存儲的,例如,存元素的順序是11、22、33。那

    2024年01月18日
    瀏覽(23)
  • 數(shù)據(jù)結構與算法基礎-學習-23-圖之鄰接矩陣與鄰接表

    數(shù)據(jù)結構與算法基礎-學習-23-圖之鄰接矩陣與鄰接表

    目錄 一、定義和術語 二、存儲結構 1、鄰接矩陣 1.1、鄰接矩陣優(yōu)點 1.2、鄰接矩陣缺點 2、鄰接表 3、鄰接矩陣和鄰接表的區(qū)別和用途 3.1、區(qū)別 3.2、用途 三、宏定義 四、結構體定義 1、鄰接矩陣 2、鄰接表 3、網數(shù)據(jù)類型(造測試數(shù)據(jù)) 五、函數(shù)定義 1、使用鄰接矩陣創(chuàng)建無

    2024年02月14日
    瀏覽(18)
  • 【零基礎】學python數(shù)據(jù)結構與算法筆記14-動態(tài)規(guī)劃

    【零基礎】學python數(shù)據(jù)結構與算法筆記14-動態(tài)規(guī)劃

    學習python數(shù)據(jù)結構與算法,學習常用的算法, b站學習鏈接 動態(tài)規(guī)劃在基因測序、基因比對、hmm 有應用場景。 從斐波那契數(shù)列看動態(tài)規(guī)劃 練習: 使用遞歸和非遞歸的方法來求解斐波那契數(shù)列。 這種非遞歸求斐波那契數(shù),可以看成是一個動態(tài)規(guī)劃思想,每次都會把重復子問

    2023年04月09日
    瀏覽(28)
  • <數(shù)據(jù)結構> 鏈表 - 小練習

    <數(shù)據(jù)結構> 鏈表 - 小練習

    線性表在 ▁▁▁▁ 情況下適合采用鏈式存儲結構。 A.線性表中數(shù)據(jù)元素的值需經常修改 B.線性表需經常插入或刪除數(shù)據(jù)元素 C.線性表包含大量的數(shù)據(jù)元素 D.線性表的數(shù)據(jù)元素包含大量的數(shù)據(jù)項 鏈表要求內存中可用存儲單元的地址 ▁▁▁▁▁ 。 A.必須是連續(xù)的 B.部分地址必

    2023年04月08日
    瀏覽(21)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領取紅包,優(yōu)惠每天領

二維碼1

領取紅包

二維碼2

領紅包