python内置函数多少个_每个数据科学家都应该知道的10个Python内置函数
python內(nèi)置函數(shù)多少個(gè)
Python is the number one choice of programming language for many data scientists and analysts. One of the reasons of this choice is that python is relatively easier to learn and use. More importantly, there is a wide variety of third party libraries that ease lots of tasks in the field of data science. For instance, numpy and pandas are great data analysis libraries. Scikit-learn and tensorflow are for machine learning and data preprocessing tasks. We also have python data visualization libraries such as matplotlib and seaborn. There are many more useful libraries for data science in python ecosystem.
對(duì)于許多數(shù)據(jù)科學(xué)家和分析師來說,Python是編程語言的第一選擇。 這種選擇的原因之一是python相對(duì)易于學(xué)習(xí)和使用。 更重要的是,有各種各樣的第三方庫可以減輕數(shù)據(jù)科學(xué)領(lǐng)域的許多任務(wù)。 例如, numpy和pandas是出色的數(shù)據(jù)分析庫。 Scikit-learn和tensorflow用于機(jī)器學(xué)習(xí)和數(shù)據(jù)預(yù)處理任務(wù)。 我們還有python數(shù)據(jù)可視化庫,例如matplotlib和seaborn 。 在python生態(tài)系統(tǒng)中,還有許多用于數(shù)據(jù)科學(xué)的有用庫。
In this post, we will not talk about these libraries or frameworks. We will rather cover important built-in functions of python. Some of these functions are also used in the libraries we mentioned. There is no point in reinventing the wheel.
在本文中,我們將不討論這些庫或框架。 我們寧愿介紹重要的python內(nèi)置函數(shù)。 我們提到的庫中也使用了其中一些功能。 重新發(fā)明輪子沒有意義。
Let’s start.
開始吧。
1套 (1. Set)
Set function returns a set object from an iterable (e.g. a list). Set is an unordered sequence of unique values. Thus, unlike lists, sets do not have duplicate values. One use case of sets is to remove duplicate values from a list or tuple. Let’s go over an example.
Set函數(shù)從迭代器(例如列表)返回set對(duì)象。 Set是唯一值的無序序列。 因此,與列表不同,集合不具有重復(fù)值。 集的一種用例是從列表或元組中刪除重復(fù)的值。 讓我們來看一個(gè)例子。
list_a = [1,8,5,3,3,4,4,4,5,6]set_a = set(list_a)
print(list_a)
print(set_a)
[1, 8, 5, 3, 3, 4, 4, 4, 5, 6]
{1, 3, 4, 5, 6, 8}
As you can see, duplicates are removed and the values are sorted. Sets are not subscriptable (because of not keeping an order) so we cannot access or modify the individual elements of a set. However, we can add elements to a set or remove elements from a set.
如您所見,將刪除重復(fù)項(xiàng)并對(duì)值進(jìn)行排序。 集合不可下標(biāo)(因?yàn)椴槐3猪樞?,因此我們無法訪問或修改集合的各個(gè)元素。 但是,我們可以將元素添加到集合中 或從集合中刪除元素。
set_a.add(15)print(set_a)
{1, 3, 4, 5, 6, 8, 15}
2.列舉 (2. Enumerate)
Enumerate provides a count when working with iterables. It can be placed in a for loop or directly used on an iterable to create an enumerate object. Enumerate object is basically a list of tuples.
枚舉提供了使用可迭代對(duì)象時(shí)的計(jì)數(shù)。 可以將其放在for循環(huán)中,也可以直接在可迭代對(duì)象上使用以創(chuàng)建枚舉對(duì)象。 枚舉對(duì)象基本上是一個(gè)元組列表。
cities = ['Houston', 'Rome', 'Berlin', 'Madrid']print(list(enumerate(cities)))
[(0, 'Houston'), (1, 'Rome'), (2, 'Berlin'), (3, 'Madrid')]
Using with a for loop:
與for循環(huán)一起使用:
for count, city in enumerate(cities):print(count, city)0 Houston
1 Rome
2 Berlin
3 Madrid
3.實(shí)例 (3. Isinstance)
Isinstance function is used to check or compare the classes of objects. It accepts an object and a class as arguments and returns True if the object is an instance of that class.
Isinstance函數(shù)用于檢查或比較對(duì)象的類。 它接受一個(gè)對(duì)象和一個(gè)類作為參數(shù),如果該對(duì)象是該類的實(shí)例,則返回True。
a = [1,4,3,5,5,7]b = set(a)
print(isinstance(a, list))
print(isinstance(b, list))
True
False
We can access the class of an object using type function but isinstance comes in hand when doing comparisons.
我們可以使用類型函數(shù)來訪問對(duì)象的類,但是在進(jìn)行比較時(shí)會(huì)使用isinstance。
4.倫 (4. Len)
Len function returns the length of an object or the number of items in an object. For instance, len can be used to check:
Len函數(shù)返回對(duì)象的長(zhǎng)度或?qū)ο笾械捻?xiàng)目數(shù)。 例如,len可用于檢查:
- Number of characters in a string 字符串中的字符數(shù)
- Number of elements in a list 列表中的元素?cái)?shù)
- Number of rows in a dataframe 數(shù)據(jù)框中的行數(shù)
b = set(a)
c = 'Data science'print(f'The length of a is {len(a)}')
print(f'The length of b is {len(b)}')
print(f'The length of c is {len(c)}')The length of a is 6
The length of b is 5
The length of c is 12
5.排序 (5. Sorted)
As the name clearly explains, sorted function is used to sort an iterable. By default, sorting is done in ascending order but it can be changed with reverse parameter.
顧名思義,sorted函數(shù)用于對(duì)Iterable進(jìn)行排序。 默認(rèn)情況下,排序按升序進(jìn)行,但可以使用反向參數(shù)進(jìn)行更改。
a = [1,4,3,2,8]print(sorted(a))print(sorted(a, reverse=True))[1, 2, 3, 4, 8]
[8, 4, 3, 2, 1]
We may need to sort a list or pandas series when doing data cleaning and processing. Sorted can also be used to sort the characters of a string but it returns a list.
進(jìn)行數(shù)據(jù)清理和處理時(shí),我們可能需要對(duì)列表或熊貓系列進(jìn)行排序。 Sorted也可以用于對(duì)字符串的字符進(jìn)行排序,但是它返回一個(gè)列表。
c = "science"print(sorted(c))
['c', 'c', 'e', 'e', 'i', 'n', 's']
6.郵編 (6. Zip)
Zip function takes iterables as argument and returns an iterator consists of tuples. Consider we have two associated lists.
Zip函數(shù)將Iterables作為參數(shù),并返回由元組組成的迭代器。 考慮我們有兩個(gè)關(guān)聯(lián)的列表。
a = [1,3,2,4]b = ['Rome','Houston','Madrid','Berlin']print(list(zip(a,b)))
[(1, 'Rome'), (3, 'Houston'), (2, 'Madrid'), (4, 'Berlin')]
The iterables do not have to have the same length. The first matching indices are zipped. For instance, if list a was [1,3,2,4,5] in the case above, the output would be the same. Zip function can also take more than two iterables.
可迭代對(duì)象的長(zhǎng)度不必相同。 首個(gè)匹配索引已壓縮。 例如,如果在上述情況下列表a為[1,3,2,4,5],則輸出將相同。 Zip函數(shù)也可以采用兩個(gè)以上的可迭代項(xiàng)。
7.任何和全部 (7. Any and All)
I did not want to separate any and all functions because they are kind of complement of each other. Any returns True if any element of an iterable is True. All returns True if all elements of an iterable is True.
我不想分離任何和所有功能,因?yàn)樗鼈兓檠a(bǔ)充。 如果iterable的任何元素為True,則Any返回True。 如果iterable的所有元素均為True,則All返回True。
One typical use case of any and all is checking the missing values in a pandas series or dataframe.
任何一種情況的一個(gè)典型用例是檢查熊貓系列或數(shù)據(jù)框中的缺失值。
import pandas as pdser = pd.Series([1,4,5,None,9])print(ser.isna().any())
print(ser.isna().all())True
False
8.范圍 (8. Range)
Range is an immutable sequence. It takes 3 arguments which are start, stop, and step size. For instance, range(0,10,2) returns a sequence that starts at 0 and stops at 10 (10 is exclusive) with increments of 2.
范圍是一個(gè)不變的序列。 它接受3個(gè)參數(shù),分別是start , stop和step size。 例如,range(0,10,2)返回一個(gè)序列,該序列從0開始并在10(不包括10)處停止,且增量為2。
The only required argument is stop. Start and step arguments are optional.
唯一需要的參數(shù)是stop。 開始和步驟參數(shù)是可選的。
print(list(range(5)))[0, 1, 2, 3, 4]print(list(range(2,10,2)))
[2, 4, 6, 8]print(list(range(0,10,3)))
[0, 3, 6, 9]
9.地圖 (9. Map)
Map function applies a function to every element of an iterator and returns an iterable.
Map函數(shù)將一個(gè)函數(shù)應(yīng)用于迭代器的每個(gè)元素,并返回一個(gè)可迭代的對(duì)象。
a = [1,2,3,4,5]print(list(map(lambda x: x*x, a)))[1, 4, 9, 16, 25]
We used map function to square each element of list a. This operation can also be done with a list comprehension.
我們使用map函數(shù)對(duì)列表a的每個(gè)元素求平方。 該操作也可以通過列表理解來完成。
a = [1,2,3,4,5]b = [x*x for x in a]
print(b)
List comprehension is preferable over map function in most cases due to its speed and simpler syntax. For instance, for a list with 1 million elements, my computer accomplished this task in 1.2 seconds with list comprehension and in 1.4 seconds with map function.
在大多數(shù)情況下,列表理解比map函數(shù)更可取,因?yàn)樗乃俣群驼Z法更簡(jiǎn)單。 例如,對(duì)于具有一百萬個(gè)元素的列表,我的計(jì)算機(jī)通過列表理解在1.2秒內(nèi)完成了此任務(wù),而在使用map功能時(shí)在1.4秒內(nèi)完成了此任務(wù)。
When working with larger iterables (much larger than 1 million), map is a better choice due to memory issues. Here is a detailed post on list comprehensions if you’d like to read further.
當(dāng)使用較大的可迭代對(duì)象(遠(yuǎn)大于100萬)時(shí),由于內(nèi)存問題,映射是更好的選擇。 如果您想進(jìn)一步閱讀,這是有關(guān)列表理解的詳細(xì)信息。
10.清單 (10. List)
List is a mutable sequence used to store collections of items with same or different types.
列表是一個(gè)可變序列,用于存儲(chǔ)相同或不同類型的項(xiàng)目的集合。
a = [1, 'a', True, [1,2,3], {1,2}]print(type(a))<class 'list'>print(len(a))
5
a is a list that contains an integer, a string, a boolean value, a list, and a set. For this post, we are more interested in creating list with a type constructor (list()). You may have noticed that we used list() throughout this post. It takes an iterable and converts it to a list. For instance, map(lambda x: x*x, a) creates an iterator. We converted to a list to print it.
a是一個(gè)包含整數(shù),字符串,布爾值,列表和集合的列表。 對(duì)于本文,我們對(duì)使用類型構(gòu)造函數(shù)(list())創(chuàng)建列表更感興趣。 您可能已經(jīng)注意到,本文中我們一直使用list()。 它需要一個(gè)可迭代的并將其轉(zhuǎn)換為列表。 例如,map(lambda x:x * x,a)創(chuàng)建一個(gè)迭代器。 我們將其轉(zhuǎn)換為列表以進(jìn)行打印。
We can create a list from a range, set, pandas series, numpy array, or any other iterable.
我們可以從范圍,集合,熊貓系列,numpy數(shù)組或任何其他可迭代對(duì)象創(chuàng)建列表。
import numpy as npa = {1,2,3,4}b = np.random.randint(10, size=5)
c = range(6)print(list(a))
[1, 2, 3, 4]print(list(b))
[4, 4, 0, 5, 3]print(list(c))
[0, 1, 2, 3, 4, 5]
Thank you for reading. Please let me know if you have any feedback.
感謝您的閱讀。 如果您有任何反饋意見,請(qǐng)告訴我。
翻譯自: https://towardsdatascience.com/10-python-built-in-functions-every-data-scientist-should-know-233b302abc05
python內(nèi)置函數(shù)多少個(gè)
總結(jié)
以上是生活随笔為你收集整理的python内置函数多少个_每个数据科学家都应该知道的10个Python内置函数的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 梦到老公前女友来找他什么意思
- 下一篇: 针对数据科学家和数据工程师的4条SQL技