python內置函數多少個_每個數據科學家都應該知道的10個Python內置函數

python內置函數多少個

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.

對于許多數據科學家和分析師來說,Python是編程語言的第一選擇。 這種選擇的原因之一是python相對易于學習和使用。 更重要的是,有各種各樣的第三方庫可以減輕數據科學領域的許多任務。 例如, numpypandas是出色的數據分析庫。 Scikit-learntensorflow用于機器學習和數據預處理任務。 我們還有python數據可視化庫,例如matplotlibseaborn 。 在python生態系統中,還有許多用于數據科學的有用庫。

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內置函數。 我們提到的庫中也使用了其中一些功能。 重新發明輪子沒有意義。

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函數從迭代器(例如列表)返回set對象。 Set是唯一值的無序序列。 因此,與列表不同,集合不具有重復值。 集的一種用例是從列表或元組中刪除重復的值。 讓我們來看一個例子。

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}
Image for post

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.

如您所見,將刪除重復項并對值進行排序。 集合不可下標(因為不保持順序),因此我們無法訪問或修改集合的各個元素。 但是,我們可以元素添加到集合中 或從集合中刪除元素。

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.

枚舉提供了使用可迭代對象時的計數。 可以將其放在for循環中,也可以直接在可迭代對象上使用以創建枚舉對象。 枚舉對象基本上是一個元組列表。

cities = ['Houston', 'Rome', 'Berlin', 'Madrid']
print(list(enumerate(cities)))
[(0, 'Houston'), (1, 'Rome'), (2, 'Berlin'), (3, 'Madrid')]

Using with a for loop:

與for循環一起使用:

for count, city in enumerate(cities):
print(count, city)0 Houston
1 Rome
2 Berlin
3 Madrid
Image for post

3.實例 (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函數用于檢查或比較對象的類。 它接受一個對象和一個類作為參數,如果該對象是該類的實例,則返回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.

我們可以使用類型函數來訪問對象的類,但是在進行比較時會使用isinstance。

Image for post

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函數返回對象的長度或對象中的項目數。 例如,len可用于檢查:

  • Number of characters in a string

    字符串中的字符數
  • Number of elements in a list

    列表中的元素數
  • Number of rows in a dataframe

    數據框中的行數
a = [1,4,3,5,5,7]
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
Image for post

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函數用于對Iterable進行排序。 默認情況下,排序按升序進行,但可以使用反向參數進行更改。

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.

進行數據清理和處理時,我們可能需要對列表或熊貓系列進行排序。 Sorted也可以用于對字符串的字符進行排序,但是它返回一個列表。

c = "science"
print(sorted(c))
['c', 'c', 'e', 'e', 'i', 'n', 's']
Image for post

6.郵編 (6. Zip)

Zip function takes iterables as argument and returns an iterator consists of tuples. Consider we have two associated lists.

Zip函數將Iterables作為參數,并返回由元組組成的迭代器。 考慮我們有兩個關聯的列表。

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.

可迭代對象的長度不必相同。 首個匹配索引已壓縮。 例如,如果在上述情況下列表a為[1,3,2,4,5],則輸出將相同。 Zip函數也可以采用兩個以上的可迭代項。

Image for post

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.

我不想分離任何所有功能,因為它們互為補充。 如果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.

任何一種情況的一個典型用例是檢查熊貓系列或數據框中的缺失值。

import pandas as pd
ser = pd.Series([1,4,5,None,9])print(ser.isna().any())
print(ser.isna().all())True
False
Image for post

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.

范圍是一個不變的序列。 它接受3個參數,分別是startstopstep size。 例如,range(0,10,2)返回一個序列,該序列從0開始并在10(不包括10)處停止,且增量為2。

The only required argument is stop. Start and step arguments are optional.

唯一需要的參數是stop。 開始和步驟參數是可選的。

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]
Image for post

9.地圖 (9. Map)

Map function applies a function to every element of an iterator and returns an iterable.

Map函數將一個函數應用于迭代器的每個元素,并返回一個可迭代的對象。

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函數對列表a的每個元素求平方。 該操作也可以通過列表理解來完成。

a = [1,2,3,4,5]
b = [x*x for x in a]
print(b)
Image for post

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.

在大多數情況下,列表理解比map函數更可取,因為它的速度和語法更簡單。 例如,對于具有一百萬個元素的列表,我的計算機通過列表理解在1.2秒內完成了此任務,而在使用map功能時在1.4秒內完成了此任務。

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.

當使用較大的可迭代對象(遠大于100萬)時,由于內存問題,映射是更好的選擇。 如果您想進一步閱讀,這是有關列表理解的詳細信息。

10.清單 (10. List)

List is a mutable sequence used to store collections of items with same or different types.

列表是一個可變序列,用于存儲相同或不同類型的項目的集合。

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是一個包含整數,字符串,布爾值,列表和集合的列表。 對于本文,我們對使用類型構造函數(list())創建列表更感興趣。 您可能已經注意到,本文中我們一直使用list()。 它需要一個可迭代的并將其轉換為列表。 例如,map(lambda x:x * x,a)創建一個迭代器。 我們將其轉換為列表以進行打印。

We can create a list from a range, set, pandas series, numpy array, or any other iterable.

我們可以從范圍,集合,熊貓系列,numpy數組或任何其他可迭代對象創建列表。

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]
Image for post

Thank you for reading. Please let me know if you have any feedback.

感謝您的閱讀。 如果您有任何反饋意見,請告訴我。

翻譯自: https://towardsdatascience.com/10-python-built-in-functions-every-data-scientist-should-know-233b302abc05

python內置函數多少個

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

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

相關文章

C#使用TCP/IP與ModBus進行通訊

C#使用TCP/IP與ModBus進行通訊1. ModBus的 Client/Server模型 2. 數據包格式及MBAP header (MODBUS Application Protocol header) 3. 大小端轉換 4. 事務標識和緩沖清理 5. 示例代碼 0. MODBUS MESSAGING ON TCP/IP IMPLEMENTATION GUIDE 下載地址&#xff1a;http://www.modb…

Hadoop HDFS常用命令

1、查看hdfs文件目錄 hadoop fs -ls / 2、上傳文件 hadoop fs -put 文件路徑 目標路徑 在瀏覽器查看:namenodeIP:50070 3、下載文件 hadoop fs -get 文件路徑 保存路徑 4、設置副本數量 -setrep 轉載于:https://www.cnblogs.com/chaofan-/p/9742633.html

SAP UI 搜索分頁技術

搜索分頁技術往往和另一個術語Lazy Loading&#xff08;懶加載&#xff09;聯系起來。今天由Jerry首先介紹S/4HANA&#xff0c;CRM Fiori和S4CRM應用里的UI搜索分頁的實現原理。后半部分由SAP成都研究院菜園子小哥王聰向您介紹Twitter的懶加載實現。 關于王聰的背景介紹&#x…

萬彩錄屏服務器不穩定,萬彩錄屏 云服務器

萬彩錄屏 云服務器 內容精選換一換內網域名是指僅在VPC內生效的虛擬域名&#xff0c;無需購買和注冊&#xff0c;無需備案。云解析服務提供的內網域名功能&#xff0c;可以讓您在VPC中擁有權威DNS&#xff0c;且不會將您的DNS記錄暴露給互聯網&#xff0c;解析性能更高&#xf…

針對數據科學家和數據工程師的4條SQL技巧

SQL has become a common skill requirement across industries and job profiles over the last decade.在過去的十年中&#xff0c;SQL已成為跨行業和職位描述的通用技能要求。 Companies like Amazon and Google will often demand that their data analysts, data scienti…

C# 讀取CAD文件縮略圖(DWG文件)

//C# 讀取CAD文件縮略圖&#xff08;DWG文件&#xff09; 楊航收集技術資料&#xff0c;分享給大家 //2010-09-04 16:34:58| 分類&#xff1a; C# |字號 訂閱//在不使用任務插件的情況下讀取DWG文件的縮略圖&#xff0c;以便在沒有安裝AutoCAD的計算機上瀏覽。using System;u…

全排列算法實現

版權聲明&#xff1a;本文為博主原創文章&#xff0c;未經博主允許不得轉載。 https://blog.csdn.net/summerxiachen/article/details/605796231.全排列的定義和公式&#xff1a; 從n個數中選取m&#xff08;m<n&#xff09;個數按照一定的順序進行排成一個列&#xff0c;叫…

14.并發容器之ConcurrentHashMap(JDK 1.8版本)

1.ConcurrentHashmap簡介 在使用HashMap時在多線程情況下擴容會出現CPU接近100%的情況&#xff0c;因為hashmap并不是線程安全的&#xff0c;通常我們可以使用在java體系中古老的hashtable類&#xff0c;該類基本上所有的方法都采用synchronized進行線程安全的控制&#xff0c;…

modbus注意幾點

1、 在利用Modbus通訊的過程中&#xff0c;遇到這樣一個問題&#xff0c;即浮點數的傳輸問題。因為一般浮點數都是32位&#xff0c;而Modbus總線中只能傳輸最多16位的數據。解決方法&#xff1a;可以利用兩個整形數傳送一個浮點數&#xff08;即將一個32位的二進制數分割成兩個…

服務器虛擬化網口,服務器安裝虛擬網口

服務器安裝虛擬網口 內容精選換一換Atlas 800 訓練服務器(型號 9010)安裝上架、服務器基礎參數配置、安裝操作系統等操作請參見《Atlas 800 訓練服務器 用戶指南 (型號9010)》。Atlas 800 訓練服務器(型號 9010)適配操作系統如表1所示。請參考表2下載驅動和固件包。Atlas 800 訓…

芒果云接嗎_芒果糯米飯是生產力的關鍵嗎?

芒果云接嗎Would you like to know how your mood impact your sleep and how your parents influence your happiness levels?您想知道您的心情如何影響您的睡眠以及您的父母如何影響您的幸福感嗎&#xff1f; Become a data nerd, and track it!成為數據書呆子&#xff0c;…

hdoj4283 You Are the One

題意&#xff1a;每個人出場時獲得等待時間*值的unhappy值。有個棧換出場順序。問怎樣最小&#xff1f; 一開始的時候覺得在中間取斷點&#xff0c;dp[i][j]表示區間全出場后的最小值。那么dp[i][j]dp[i][k]dp[k1][j]&#xff0c;但這樣是不行的。因為有可能最優解是[i][k]只出…

laravel-admin 開發 bootstrap-treeview 擴展包

laravel-admin 擴展開發文檔https://laravel-admin.org/doc... 效果圖&#xff1a; 開發過程&#xff1a; 1、先創建Laravel項目&#xff0c;并集成laravel-admin&#xff0c;教程&#xff1a; http://note.youdao.com/notesh... 2、生成開發擴展包 php artisan admin:extend c…

怎么看服務器上jdk安裝位置,查看云服務器jdk安裝路徑

查看云服務器jdk安裝路徑 內容精選換一換用戶可以在公有云MRS集群以外的節點上使用客戶端&#xff0c;在使用客戶端前需要安裝客戶端。如果集群外的節點已安裝客戶端且只需要更新客戶端&#xff0c;請使用安裝客戶端的用戶例如root。針對MRS 3.x之前版本的集群&#xff0c;需要…

公司生日會生日禮物_你的生日有多受歡迎?

公司生日會生日禮物In the years before 2020, it was common for a large number of school children (20–30 or more) to physically colocate for their math lessons. And in many a class, students were asked to compute the probability that two of them had the sam…

Django思維導圖

轉載于:https://www.cnblogs.com/liangying666/p/9744477.html

XebiaLabs DevOps平臺推出軟件發布風險和合規性管理功能

XebiaLabs是DevOps和持續交付軟件工具供應商&#xff0c;通過其DevOps平臺推出了用于軟件版本發布的監管、安全和合規風險評估跟蹤功能。 這些新功能旨在幫助組織跟蹤應用程序的發布狀態信息&#xff0c;了解跨多個應用程序、團隊和環境的安全性和合規性風險。XebiaLabs表示&am…

wp7開發環境搭建

簡介 本文通過step by step的模式講述如何從0開始搭建Window Phone 7開發環境&#xff0c;如果開發簡單的Windows Phone 7程序。只是一篇介紹性的文章,但是邁進Windows Phone 7開發之路其實就那么簡單,一起來開發Windows Phone 7吧。 Windows 7安裝 目前Windows Phone 7開發…

舊金山字體_舊金山建筑業的興衰。 施工趨勢與歷史

舊金山字體This series of articles is devoted to the study of the construction activity of the main city of Silicon Valley — San Francisco. Charts and calculations were built with the help of Jupyter Notebook (Kaggle)該系列文章專門研究硅谷主要城市舊金山的建…