代码段时间 python_最节省时间的有用python代码段

代码段时间 python

程式设计 (PROGRAMMING)

Here are my favorite 29 Python snippets that actually save me time as a developer

这是我最喜欢的29个Python代码段,它们实际上为我节省了开发人员的时间

Python is one of the most popular languages ​​used by many in data science and machine learning, web development, scripting, automation, and more. One of the reasons for this popularity is its simplicity and ease of learning.

Python是数据科学和机器学习,Web开发,脚本,自动化等领域中许多人使用的最受欢迎的语言之一。 如此受欢迎的原因之一是其简单易学。

If you are reading this, you are most likely already using Python, or at least interested in it.

如果您正在阅读本文,则很可能已经在使用Python,或者至少对此感兴趣。

In this article, we’ll take a quick look at 29 short code snippets that you can understand and master incredibly quickly. Go!

在本文中,我们将快速浏览29个简短的代码片段,您可以快速理解和掌握它们。 走!

👉1.检查唯一性 (👉 1. Checking for uniqueness)

The next method checks if there are duplicate items in the given list. It uses a property set()that removes duplicate items from the list:

下一个方法检查给定列表中是否存在重复项。 它使用属性set()从列表中删除重复项:

👉2.字谜 (👉 2. Anagram)

This method can be used to check if two strings are anagrams. An Anagram is a word or phrase formed by rearranging the letters of another word or phrase, usually using all the original letters exactly once:

此方法可用于检查两个字符串是否为字谜。 字谜是通过重新排列另一个单词或短语的字母而形成的单词或短语,通常只使用所有原始字母一次:

👉3.记忆 (👉 3. Memory)

And this can be used to check the memory usage of an object:

这可用于检查对象的内存使用情况:

👉4.字节大小 (👉 4. Size in bytes)

The method returns the length of the string in bytes:

该方法以字节为单位返回字符串的长度:

👉5.打印字符串N次 (👉 5. Print the string N times)

This snippet can be used to output a string nonce without the need to use loops for this:

此代码段可用于输出一次字符串n ,而无需为此使用循环:

👉6.使单词的前几个字母变大 (👉 6. Makes the first letters of words large)

And here is the register. The snippet uses a method title()to capitalize each word in a string:

这是寄存器。 该代码段使用方法title()将字符串中的每个单词大写:

👉7.分离 (👉 7. Separation)

This method splits the list into smaller lists of the specified size:

此方法将列表分为指定大小的较小列表:

👉8.删除错误值 (👉 8. Removing false values)

So you remove the false values ( False, None, 0and «») from the list using filter():

因此,您可以使用filter()从列表中删除错误的值( FalseNone0«» filter()

👉9.计数 (👉 9. Counting)

The following code can be used to transpose a 2D array:

以下代码可用于转置2D数组:

👉10.链比较 (👉 10. Chain comparison)

You can do multiple comparisons with all kinds of operators in one line:

您可以在一行中与各种运算符进行多次比较:

👉11.以逗号分隔 (👉 11. Separate with comma)

The following snippet can be used to convert a list of strings to a single string, where each item from the list is separated by commas:

以下代码段可用于将字符串列表转换为单个字符串,其中列表中的每个项目均以逗号分隔:

👉12.计算元音 (👉 12. Count the vowels)

This method counts the number of vowels (“a”, “e”, “i”, “o”, “u”) found in the string:

此方法计算在字符串中找到的元音(“ a”,“ e”,“ i”,“ o”,“ u”)的数量:

👉13.将字符串的首字母转换为小写 (👉 13. Converting the first letter of a string to lowercase)

Use to convert the first letter of your specified string to lowercase:

用于将指定字符串的首字母转换为小写:

👉14.抗锯齿 (👉 14. Anti-aliasing)

The following methods flatten out a potentially deep list using recursion:

下列方法使用递归方法展平了可能更深的列表:

👉15.差异 (👉 15. Difference)

The method finds the difference between the two iterations, keeping only the values ​​that are in the first:

该方法查找两次迭代之间的差异,仅保留第一次迭代中的值:

👉16.列表之间的区别 (👉 16. The difference between lists)

The following method returns the difference between the two lists after applying this function to each element of both lists:

在将此函数应用于两个列表的每个元素之后,以下方法返回两个列表之间的差:

👉17.链式函数调用 (👉 17. Chained function call)

You can call multiple functions on one line:

您可以在一行上调用多个函数:

👉18.查找重复项 (👉 18. Finding Duplicates)

This code checks to see if there are duplicate values ​​in the list using the fact that set()it only contains unique values:

该代码使用set()仅包含唯一值的事实来检查列表中是否存在重复值:

👉19.合并两个字典 (👉 19. Combine two dictionaries)

The following method can be used to combine two dictionaries:

可以使用以下方法来组合两个字典:

👉20.将两个列表转换成字典 (👉 20. Convert two lists to a dictionary)

Now let’s get down to converting two lists into a dictionary:

现在让我们开始将两个列表转换成字典:

👉21.使用`enumerate` (👉 21. Using `enumerate`)

The snippet shows what you can use enumerate()to get both values ​​and indices of lists:

该片段显示了您可以使用enumerate()获取列表的值和索引的内容:

👉22.花时间 (👉 22. Time spent)

Use to calculate the time it takes for a specific code to run:

用于计算特定代码运行所花费的时间:

👉23.尝试/否则 (👉 23. Try / else)

You can use elseas part of a block try:

您可以将else用作块try一部分:

👉24.最常出现的元素 (👉 24. The element that appears most often)

This method returns the most frequent item that appears in the list:

此方法返回列表中出现频率最高的项目:

👉25.回文 (👉 25. Palindrome)

The method checks if the given string is a palindrome:

该方法检查给定的字符串是否是回文:

👉26.不带if-else的计算器 (👉 26. Calculator without if-else)

The following snippet shows how to write a simple calculator without the need for conditions if-else:

以下代码段显示了如何编写简单的计算器而无需使用条件if-else

👉27.随机播放 (👉 27. Shuffle)

This code can be used to randomize the order of items in a list. Note that shuffleworks in place and returns None:

此代码可用于使列表中项目的顺序随机化。 请注意, shuffle在适当的地方起作用并返回None

👉28.更改值 (👉 28. Change values)

A really quick way to swap two variables without the need for an extra one:

交换两个变量的真正快捷方法,无需额外的变量:

👉29.获取缺少键的默认值 (👉 29. Get default value for missing keys)

The code shows how you can get the default value if the key you are looking for is not included in the dictionary:

该代码显示了如果字典中未包含您要查找的键,则如何获取默认值:

阅读更多 (Read More)

If you found this article helpful, click the💚 or 👏 button below or share the article on Facebook so your friends can benefit from it too.

如果您认为本文有帮助,请单击下面的click或💚按钮或在Facebook上分享该文章,以便您的朋友也可以从中受益。

More from my stories:

我的故事中有更多内容:

翻译自: https://medium.com/swlh/top-useful-python-snippets-that-save-time-38958f256822

代码段时间 python

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值