python读html代码_5个基本的python技巧,使您的代码更清晰易读

本文介绍了5个关键的Python技巧,帮助您更清晰、易读地处理HTML代码,提升代码的优雅性。内容源自一篇翻译文章。
摘要由CSDN通过智能技术生成

python读html代码

Python is such an expressive language because there are so many different ways to represent the same idea. Hence, while Python code can be abhorrently messy, it’s not hard to write beautiful code. Using these five tricks will help make your Python code more clean, readable, and elegant!

Python是一种富有表现力的语言,因为有很多不同的方式来表达相同的想法。 因此,尽管Python代码可能令人生厌,但编写漂亮的代码并不难。 使用这五个技巧将有助于使您的Python代码更清晰,可读性和更好!

1 | 用if / continue替换嵌套的ifs (1 | Replacing nested ifs with if/continue)

Consider the following setup: we want to loop through each element in a 3 by 3 matrix, which is done with two for loops (one looping through each row, one looping through each column — element — in each row). If the element satisfies a certain condition, we’ll do something with the numbers.

考虑以下设置:我们要遍历3 x 3矩阵中的每个元素,这是通过两个for循环完成的(一个遍历每一行,一个遍历每一列(元素)在每一行)。 如果元素满足特定条件,我们将对数字进行处理。

Although the example is watered down, writing an if statement this way can make your code a lot less readable. Especially if there are already many indents before and after, it’s safe to say that it is almost always better to reduce the amount of indenting necessary in our code.

尽管示例很繁琐,但以这种方式编写if语句会使代码的可读性大大降低。 尤其是如果前后都有很多缩进,可以肯定地说,减少代码中必要的缩进数量总是更好。

If there is only an if statement and not an elif/else component, we can write the code another way: if the condition is not true, we call the continue keyword, which skips anything left in the iteration and continues to the next.

如果只有if语句而不是elif / else组件,则可以用另一种方式编写代码:如果条件成立,则调用continue关键字,该关键字将跳过迭代中剩下的所有内容并继续进行下一个操作。

This is very helpful in making complex code more readable!

这对于使复杂的代码更具可读性很有帮助!

2 | 大数值放置值 (2 | Place values for large numbers)

If I were to ask you to tell me the number below, which one would you be able to say and understand faster?

如果我要您告诉我下面的电话号码,您能说什么并更快地理解?

  • 5683273847

    5683273847
  • 1,984,738,928

    1,984,738,928

Obviously, the answer is the second, because the number is broken down into thirds. Everyone knows that the fourth chunk is the billions, so the number is one billion, nine hundred eighty four million, seven hundred thirty eight thousand, nine hundred twenty eight. It’s so much faster than having to figure out the place values from the very start.

显然,答案是第二,因为这个数字被分解为三分之二。 大家都知道第四大块是十亿,所以这个数字是十亿,九亿八千四百万,七十三万八千,九十二万。 这比从一开始就确定位置值要快得多。

In Python, you can place underscores anywhere in numbers, and they will be treated as valid, which means that you can also use it for some other notation besides thirds (for whatever reason you would need to).

在Python中,您可以将下划线放在数字的任何位置,它们将被视为有效,这意味着除三分之二外,您还可以将其用于其他表示法(无论您出于何种原因)。

  • var = 394_293_103

    var = 394_293_103

  • print(var*2 + 98_293)

    print(var*2 + 98_293)

  • i = (3_5_7_8) #don’t know why you would do this

    i = (3_5_7_8) #don't know why you would do this

3 | 内联条件语句 (3 | Inline conditional statements)

If the statement following a condition is only one line, it can simply follow the colon on the same line.

如果条件后的语句仅占一行,则可以简单地在同一行后跟冒号。

We can also use inline statements to set variable values. Traditionally, this would be done in a very spread-out manner like such:

我们还可以使用内联语句来设置变量值。 传统上,这将以非常分散的方式完成,例如:

It can be written in one line using the syntax var = value if condition else other_value.

var = value if condition else other_value ,则可以使用var = value if condition else other_value语法将其写成一行。

If you are constructing a list of values (in this case all the numbers divisible by three from 1 to 100), you can also use inline if statements. This is much more efficient than using a regular fanned-out for loop.

如果要构造一个值列表(在这种情况下,所有数字都可以从1到100分为三),则还可以使用内联if语句。 这比使用常规的扇出循环更好。

Additionally, we can incorporate inline if statements into the return statements of functions and into lambda functions.

此外,我们可以将内联if语句合并到函数的return语句和lambda函数中。

4 | 多变量分配 (4 | Multiple variable assignment)

If you’re migrating from another language that isn’t as expressive, you may be used to several lines of the header taken up with variable initializations. Granted, this is the cost of more efficient languages, but as a Python-native programmer it’s just so annoying.

如果您要从另一种表达能力不强的语言进行迁移,则可能会习惯于用可变的初始化来占用标题的几行。 当然,这是更高效的语言的代价,但是作为Python本地程序员,这真是令人讨厌。

Java developers need to type essentially the same thing twice in order to allocate memory for large data types:

为了为大型数据类型分配内存,Java开发人员需要键入两次基本相同的内容:

In Python, variables of several different datatypes can be set in one line:

在Python中,可以在一行中设置几种不同数据类型的变量:

The values of variables can also be swapped simultaneously, instead of one-by-one sequentially, which can be problematic. For example, below the Fibonacci sequence (each number is the sum of the previous two if generated for a certain number of steps using variable swapping.

变量的值也可以同时交换,而不是依次一一交换,这可能会出现问题。 例如,在斐波那契数列下方(如果使用变量交换为一定数量的步骤生成,则每个数字都是前两个的总和。

If we were to write it as a=b \n b = a+b (on two lines) the formula wouldn’t work properly because the value of b is dependent on the value of a, which has already been updated. Try it yourself! — the result of writing it on two lines are powers of two. This helpful feature prevents us from needing to store previous values.

如果我们把它写为a=b \nb = a+b (两行),因为价值的公式将无法正常工作b是依赖于价值a ,它已经被更新。 自己尝试! —将它们写在两行上的结果是2的幂。 这项有用的功能使我们无需存储以前的值。

If variables are to be initialized with the same value, the equals operator can be chained. In this case, a, b, c, and d are all initialized to 0.

如果要使用相同的值初始化变量,则可以链接equals运算符。 在这种情况下, abcd都初始化为0。

It’s always a good day to be programming in Python!

用Python进行编程永远是美好的一天!

5 | 任何和所有 (5 | Any and All)

any() and all(), as their names suggest, return True if one of the items in a collection is True, and if all the items in a collection is True (respectively). These functions, in conjunction with other helpful tools, can collapse several loops required in standard searching.

any()all()因为它们的名称所暗示的,返回True ,如果集合中的项目之一是True的,如果所有集合中的物品是True (分别)。 这些功能与其他有用的工具一起可以折叠标准搜索中所需的多个循环。

For example, in this case a one-liner is used to determine if a list has any items divisible by thirteen, using any() and list comprehension.

例如,在这种情况下,使用any()和列表推导,使用单线来确定列表是否具有可被13整除的任何项目。

Or, in this case, the below code uses all() to ensure that no test score is above 100 points or below 40 points (which we can assume is a minimum score), which would indicate an error somewhere.

或者,在这种情况下,下面的代码使用all()来确保任何测试分数都不会高于100分或低于40分(我们可以假设这是最低分),这将表明某个地方存在错误。

A bonus used above— we can chain operators, in this example 40<a<100. As another example, if we want to confirm that a and b are less than c, we can do something like a < c > b, which is incredibly also valid!

上面使用的一个好处是,我们可以将运算符链接起来,在本例中为40<a<100 。 再举一个例子,如果我们要确认ab小于c ,我们可以做类似a < c > b事情,这同样是有效的!

The trick to using any() and all() is to think of everything in terms of Booleans. Creatively used, they can remove lots of the handwritten loops and nested structures required to conduct conditional searches.

使用any()all()的技巧是用布尔值来考虑所有事物。 创造性地使用它们可以删除执行条件搜索所需的许多手写循环和嵌套结构。

Thanks for reading! As a bonus, check out :=, the ‘walrus operator’, which was recently introduced in Python 3.8 and can shorten your code.

谢谢阅读! 另外,请查看:= ,这是“海象运算符”,最近在python 3.8中引入,可以缩短您的代码。

翻译自: https://towardsdatascience.com/5-essential-python-tricks-to-make-your-code-more-clean-readable-elegant-33db96d99e84

python读html代码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值