python網絡爬蟲畢業論文,[轉載] python 需求清單_Python清單操作摘要

 2023-11-19 阅读 28 评论 0

摘要:參考鏈接: Python清單 python網絡爬蟲畢業論文、python 需求清單 ? 5個常用python標準庫,?? ? ? ? ?? ? ? ? 列出功能和方法,理解和性能特征 (List Functions & Methods, Comprehension and Performance Characteristics)? ? ?? ? ? ? ? ? ?? ? ? ?The lis

參考鏈接: Python清單

python網絡爬蟲畢業論文、python 需求清單

?

5個常用python標準庫,??

? ?

? ??

? ? ? 列出功能和方法,理解和性能特征 (List Functions & Methods, Comprehension and Performance Characteristics)?

? ??

? ?

? ?

? ??

? ? ?The list is the common multipurpose data type available in Python 3, which can be listed as a list of comma-marked values(items) within square bracket. The items in a list need not be the same type and it is the important thing about a list.

? ? ? 該列表是Python 3中可用的通用多用途數據類型,可以作為方括號內的逗號標記值(項目)列表列出。 列表中的項目不必是同一類型,這對于列表來說很重要。?

? ??

? ?

??

??

? ?

? ??

? ? ? 訪問列表中的值 (Accessing Values in Lists)?

? ? ?To enter values in list, apply the square brackets for slicing along with the index or indices to get value available at that index

? ? ? 要在列表中輸入值,請將方括號與一個或多個索引一起切??片,以獲取該索引處的可用值??

? ? ?list1 = ['Monday','Tuesday','10','11']list2 = [8,9,10,15]list3 = ["j","i","t"]list1[0]list2[1:3]OutputMonday[9, 10]

? ? ? 更新清單 (Updating Lists)?

? ? ?By using the slice on the left-hand side of the assignment operator we can update single or multiple elements of list. We can also add a element in the list by using append() method.

? ? ? 通過使用賦值運算符左側的切片,我們可以更新列表的單個或多個元素。 我們還可以使用append()方法在列表中添加一個元素。??

? ? ?list1 = ['Monday','Tuesday','10','11']list1[2]list1[2] = 500list1Output10['Monday', 'Tuesday', 500, '11']

? ? ? 刪除列表元素 (Delete List Elements)?

? ? ?We can delete a element in a list using the del if we know the element. We can also use the remove() method if we dont know the position.del can also be used to delete entire variables.

? ? ? 如果我們知道元素,可以使用del刪除列表中的元素。 如果我們不知道位置,我們也可以使用remove()方法。 del也可以用于刪除整個變量。??

? ? ?list1 = ['Monday','Tuesday','10','11']del list1[3]list1Output['Monday', 'Tuesday', '10']

? ? ? 基本列表操作 (Basic List Operations)?

? ? ?Just like the strings, we can also add and multiply using + and * operators. The output is a new list and not a string

? ? ? 就像字符串一樣,我們也可以使用+和*運算符進行加法和乘法運算。 輸出是一個新列表,而不是字符串??

? ? ?------------------------------Lengthlen([1,2,3,])Output3------------------------------Concatenation[1,2,3]+[4,5,6]Output[1,2,3,4,5,6]------------------------------Repetition['Hi!']*4Output['Hi!', 'Hi!', 'Hi!', 'Hi!']------------------------------Membership3 in [1,2,3]OutputTrue------------------------------Iterationfor a in [1,2,3]: print(a,end='')Output123

? ? ? 索引,切片 (Indexing, Slicing)?

? ? ?Indexing and slicing works the same way for lists as they do for strings because lists are sequences

? ? ? 列表的索引和切片與字符串的工作方式相同,因為列表是序列??

? ? ?list = ["Mango", 'Apple', 'Banana']list[2]? ? ?//Offset starts at 0list[-2]? ? //Negative: count from the rightlist[1:]? ? //Slicing fetches selectionOutput'Banana''Apple'['Apple','Banana']

? ??

? ?

??

??

? ?

? ??

? ? ? 內置列表功能和方法 (Built in List functions and Methods)?

? ? ? 清單功能 (List Functions)?

? ? ?Python List functions

? ? ? Python列表功能??

? ? ? 清單長度 (Length of a List)?

? ? ?The len(list) function gives the length of a list.

? ? ? len(list)函數給出len(list)的長度。??

? ? ?list = ["Mango", 'Apple', 'Banana']len(list)Output3

? ? ? 列表中的最大值 (Max Value in a list)?

? ? ?The max(list) function gives the maximum value in a list.

? ? ? max(list)函數給出max(list)的最大值。??

? ? ?list = [1,2,3,4,5,6]max(list)Output6

? ? ? 列表中的最小值 (Min Value in a list)?

? ? ?The min(list) function gives the minimum value in a list.

? ? ? min(list)函數給出min(list)的最小值。??

? ? ?list = [1,2,3,4,5,6]min(list)Output1

? ? ? 將元組轉換為列表 (Convert tuple to list)?

? ? ?The list(seq) function can be used to convert a tuple to list.

? ? ? list(seq)函數可用于將元組轉換為list。??

? ? ?tup1 = (1,2,3,4,5)type(tup1)tup1list(tup1)Output<class 'tuple'>(1, 2, 3, 4, 5)[1,2,3,4,5]

? ? ? 清單方法 (List Methods)?

? ? ?Python List methods

? ? ? Python List方法??

? ? ? 附加 (Append)?

? ? ?The list.append(obj) method is used to append the object to listEquivalent to a[len(a):] = [x]

? ? ? list.append(obj)方法用于將對象追加到list a[len(a):] = [x] e]等效于a[len(a):] = [x]??

? ? ?list1 = [1,2,3,4,5,6]list1.append(7)list1Output[1, 2, 3, 4, 5, 6, 7]

? ? ? 計數 (Count)?

? ? ?The list.count(obj) method is used to count the number of object in the list

? ? ? list.count(obj)方法用于計算列表中對象的數量??

? ? ?list1 = [1,2,3,4,5,6,2]list1.count(2)Output2

? ? ? 將序列附加到列表 (Attach Sequence to list)?

? ? ?The list.extend(seq) method is used to append the contents of sequence to list. Equivalent to a[len(a):] = iterable

? ? ? list.extend(seq)方法用于將序列的內容追加到list。 等效于a[len(a):] = iterable??

? ? ?list1 = [1,2,3]list1.extend([4,5,6])Output[1, 2, 3, 4, 5, 6]

? ? ? 對象索引 (Index of the Object)?

? ? ?The list.index(obj) returns the lowest index in the list.

? ? ? list.index(obj)返回列表中的最低索引。??

? ? ?list1 = [1,2,3]list1.index(2)Output1

? ? ? 插入物件 (Insert a Object)?

? ? ?The list.insert(index, obj) inserts object obj into list at offset index.#1 a.insert(0, x) — Insert at front of the list#2 a.insert(len(a), x) —Insert at the end is equivalent to a.append(x)

? ? ? list.insert(index, obj)將對象obj插入偏移量為index的列表中。#1 a.insert(0, x) -插入列表的最前面? #2 a.insert(len(a), x)最后插入等效于a.append(x)??

? ? ?list1 = [1,2,3]list1.index(2,4)Output[1, 2, '4', 3]

? ? ? 排序清單 (Sort a list)?

? ? ?The list.sort(key=None, reverse=False) is used to sort the items of the list in place.

? ? ? list.sort( key=None , reverse=False )用于對列表中的項目進行排序。??

? ? ?list1 = [2,1,5,0]list1.sort()Output[0, 1, 2, 5]

? ? ? 刪除最后一個值 (Remove last value)?

? ? ?The list.pop(obj=list[-1]) removes and returns the last object or obj from list.

? ? ? list.pop(obj=list[-1])從列表中刪除并返回最后一個對象或obj。??

? ? ?list1 = [1,2,3,4,5]list1.pop()list1.pop(0)Output51

? ? ? 復制清單 (Copy a list)?

? ? ?The list.copy() is used to return a shallow copy of the list. Equivalent to a[:]

? ? ? list.copy()用于返回列表的淺表副本。 相當于a[:]??

? ? ?list1 = [1,2,3,4,5]list1.copy()Output[1, 2, 3, 4, 5]

? ? ? 明確 (Clear)?

? ? ?The list.clear() is used to remove all objects from the list. Equivalent to del a[:]

? ? ? list.clear()用于從列表中刪除所有對象。 等效于del a[:]??

? ? ?list1 = [1,2,3,4,5]list1.clear()Output[]

? ? ? 去掉 (Remove)?

? ? ?The list.remove(obj) removes the first object from the list.

? ? ? list.remove(obj)從列表中刪除第一個對象。??

? ? ?list1 = [1,2,3,4,5]list1.remove(2)list1Output[1, 3, 4, 5]

? ? ? 逆轉 (Reverse)?

? ? ?The list.reverse() is used to reverse the objects of list in place.

? ? ? list.reverse()用于在適當位置反轉list的對象。??

? ? ?list1 = [1,2,3,4,5]list1.reverse()list1Output[5, 4, 3, 2, 1]

? ??

? ?

??

??

? ?

? ??

? ? ? 將列表用作隊列 (Using Lists as Queues)?

? ? ?It is also possible to use a list as a queue, where the first element added is the first element retrieved (“first-in, first-out”); however, lists are not efficient for this purpose. While appends and pops from the end of list are fast, doing inserts or pops from the beginning of a list is slow (because all of the other elements have to be shifted by one).

? ? ? 也可以使用列表作為隊列,其中添加的第一個元素是檢索到的第一個元素(“先進先出”); 但是,列表對于此目的并不有效。 盡管從列表末尾開始的添加和彈出很快速,但是從列表開頭進行插入或彈出是很慢的(因為所有其他元素都必須移位一個)。??

? ? ?from collections import dequequeue = deque(["Apple", "Mango", "Banana"])queue.append("Grapes")? ? ? ? ?# Grapes is appendedqueue.append("Orange")? ? ? ? ?# Orange is appendedqueue.popleft()? ? ? ? ? ? ? ? # The first to arrive now leaves'Apple'queue.popleft()? ? ? ? ? ? ? ? # The second to arrive now leaves'Mango'queue? ? ? ? ? ? ? ? ? ? ? ? ? # Remaining queue in order of arrivaldeque(['Banana', 'Grapes', 'Orange'])

? ? ? 使用列表作為堆棧 (Using Lists as Stacks)?

? ? ?The list methods make it very easy to use a list as a stack, where the last element added is the first element retrieved (“last-in, first-out”). To add an item to the top of the stack, use append(). To retrieve an item from the top of the stack, use pop() without an explicit index.

? ? ? 使用list 方法可以很容易地將列表用作堆棧,其中最后添加的元素是檢索到的第一個元素(“后進先出”)。 要將項目添加到堆棧的頂部,請使用append() 。 要從堆棧頂部檢索項目,請使用沒有顯式索引的pop() 。??

? ? ?stack = [3, 4, 5]stack.append(6)stack.append(7)stack[3, 4, 5, 6, 7]stack.pop()7stack[3, 4, 5, 6]stack.pop()6stack.pop()5stack[3, 4]

? ? ? 清單理解 (List Comprehensions)?

? ? ?List comprehensions provide a concise way to create lists. Common applications are to make new lists where each element is the result of some operations applied to each member of another sequence or iterable, or to create a subsequence of those elements that satisfy a certain condition.

? ? ? 列表理解為創建列表提供了一種簡潔的方法。 常見的應用是創建新列表,其中每個元素是應用于另一個序列的每個成員或可迭代的某些操作的結果,或者創建滿足特定條件的那些元素的子序列。??

? ? ?Example: Creating a List of Squares.

? ? ? 示例:創建正方形列表。??

? ? ?-----------------------------------------------Method 1:Program-----------------------------------------------squares = []for x in range(10):...? ? ?squares.append(x**2)...squaresOutput[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]-----------------------------------------------Method 2: Using Map and lamda functions-----------------------------------------------squares = list(map(lambda x: x**2, range(10)))squaresOutput[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]-----------------------------------------------Method 3:List Comprehension-----------------------------------------------squares = [x**2 for x in range(10)]squaresOutput[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]-----------------------------------------------

? ? ?Below are some of the List Comprehension.

? ? ? 以下是一些列表理解 。??

? ? ?-----------------------------------------------# create a new list with the values doubleda = [1,2,3,-4][x*2 for x in a]Output[2, 4, 6, -8]-----------------------------------------------# filter the list to exclude negative numbers[x for x in a if x >= 0]Output[1, 2, 3]-----------------------------------------------# apply a function to all the elements[abs(x) for x in a]Output[1, 2, 3, 4]-----------------------------------------------# call a method on each elementDays = ['? ? ? Sunday', 'Monday? ? ?', 'Tuesday'][blank.strip() for blank in Days]Output['Sunday', 'Monday', 'Tuesday']-----------------------------------------------# create a list of 2-tuples like (number, square)[(x, x**2) for x in range(6)]Output[(0, 0), (1, 1), (2, 4), (3, 9), (4, 16), (5, 25)]-----------------------------------------------# flatten a list using a listcomp with two 'for'a = [[1,2,3], [4,5,6], [7,8,9]][num for elem in a for num in elem]Output[1, 2, 3, 4, 5, 6, 7, 8, 9]-----------------------------------------------#Extract vowels from sentencesentence = 'the is a sample sentence'vowels = [i for i in sentence if i in 'aeiou']vowelsOutput['e', 'i', 'a', 'a', 'e', 'e', 'e', 'e']-----------------------------------------------#Show the first letter of each wordWords = ["this","is","a","list","of","words"]items = [ word[0] for word in Words ]print(items)Output['t', 'i', 'a', 'l', 'o', 'w']-----------------------------------------------#Lower/Upper case converter[x.lower() for x in ["A","B","C"]][x.upper() for x in ["a","b","c"]]Output['a', 'b', 'c']['A', 'B', 'C']-----------------------------------------------#Print numbers only from stringstring = "Hello 12345 World"numbers = [x for x in string if x.isdigit()]print numbersOutput['1', '2', '3', '4', '5']-----------------------------------------------#Parsing a file using list comprehensionfh = open("test.txt", "r")result = [i for i in fh if "line3" in i]print resultOutputthis is line3-----------------------------------------------#Find matching elements in two listlist1 = [1,2,3,4,5,6,6,5]list2 = [3, 5, 7, 9]list(set(list1).intersection(list2))Output[3, 5]-----------------------------------------------# Compound Listslist_one = list_two = list_three = -----------------------------------------------# Print Odd numbers from the listlist1 = [1,2,3,4,5,6,7,8,9,10]print(list1)new_list = [ x for x in list1 if x%2 ]print(new_list)Output[1, 2, 3, 4, 5, 6, 7, 8, 9, 10][1, 3, 5, 7, 9]-----------------------------------------------# Print multiplication tabletable = [[x*y for y in range(1,11)] for x in range(4,7)]print(table)Output[[4, 8, 12, 16, 20, 24, 28, 32, 36, 40], [5, 10, 15, 20, 25, 30, 35, 40, 45, 50], [6, 12, 18, 24, 30, 36, 42, 48, 54, 60]]-----------------------------------------------# Finding Primesnoprimes = [j for i in range(2, 8) for j in range(i*2, 50, i)]primes = [x for x in range(2, 50) if x not in noprimes]Output[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]-----------------------------------------------# Get a list of txt files in a directoryimport osfiles = [f for f in os.listdir('./my_dir') if f.endswith('.txt')]#Get a list of the relative pathsimport osfiles = [os.path.join('./my_dir', f) for f in os.listdir('./my_dir') if f.endswith('.txt')]-----------------------------------------------#Read a csv into a listed dictionaryimport csvdata = [ x for x in csv.DictReader(open('file.csv', 'rU'))]-----------------------------------------------

? ? ? 嵌套列表推導 (Nested List Comprehensions)?

? ? ?The initial expression in a list comprehension can be any arbitrary expression, including another list comprehension.

? ? ? 列表推導中的初始表達式可以是任意表達式,包括另一個列表推導。??

? ? ?# Transposing a 3*3 Matrixmatrix = [...? ? ?[1, 2, 3, 4],...? ? ?[5, 6, 7, 8],...? ? ?[9, 10, 11, 12],... ][[row[i] for row in matrix] for i in range(4)]list(zip(*matrix))? ? ? ? ? ? ? ? ? ? ? ? ?//Using Built in functionOutput[[1, 3, 6], [2, 4, 7], [3, 5, 8]][[1, 3, 6], [2, 4, 7], [3, 5, 8]]

? ? ? 最后的想法 (Final Thoughts)?

? ? ?The list has the following performance characteristics. The below performance characteristics was taken from here.

? ? ? 該列表具有以下性能特征。 以下性能特征是從此處得出的。??

? ? ?The list object stores pointers to objects, not the actual objects, memory used depends on the number of objects in the list and not the size of the objects itself. 列表對象存儲指向對象(而不是實際對象)的指針,所使用的內存取決于列表中對象的數量,而不是對象本身的大小。 The time needed to get or set an individual item is constant, no matter what the size of the list is (also known as “O(1)” behaviour). 無論列表的大小如何,獲取或設置單個項目所需的時間都是恒定的(也稱為“ O(1)”行為)。 The time needed to append an item to the list is “amortized constant”; whenever the list needs to allocate more memory, it allocates room for a few items more than it actually needs, to avoid having to reallocate on each call (this assumes that the memory allocator is fast; for huge lists, the allocation overhead may push the behaviour towards O(n*n)). 將項目追加到列表所需的時間為“攤余常數”; 每當列表需要分配更多的內存時,它就會為超出實際需要的空間分配更多的空間,以避免每次調用都需要重新分配(這假定內存分配器是快速的;對于大型列表,分配開銷可能會增加對O(n * n)的行為)。 The time needed to insert an item depends on the size of the list, or more exactly, how many items that are to the right of the inserted item (O(n)). In other words, inserting items at the end is fast, but inserting items at the beginning can be relatively slow, if the list is large. 插入項目所需的時間取決于列表的大小,或更確切地說,取決于插入的項目(O(n))右邊的項目數。 換句話說,如果列表很大,則在末尾插入項目很快,但是在開始處插入項目可能相對較慢。 The time needed to remove an item is about the same as the time needed to insert an item at the same location; removing items at the end is fast, removing items at the beginning is slow. 刪除項目所需的時間與在同一位置插入項目所需的時間大約相同。 在結尾處刪除項目很快,而在開頭處刪除項目很慢。 The time needed to reverse a list is proportional to the list size (O(n)). 反轉列表所需的時間與列表大小(O(n))成正比。 The time needed to sort a list varies; the worst case is O(n log n), but typical cases are often a lot better than that. 排序列表所需的時間各不相同; 最壞的情況是O(n log n),但典型情況通常要好得多。?

? ??

? ?

??

?

?

? 翻譯自: https://medium.com/python-in-plain-english/python-list-operation-summary-262f40a863c8

?

?python 需求清單

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/4/181171.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息