python技术介绍python列表理解

为什么要🙄?(WHY LOOPS 🙄?)

Loops essentially allow you to repeat a process over and over without having you write the same expression or condition, each time you want the program to perform a specific task.

基本上,循环使您每次希望程序执行特定任务时都无需重复编写相同的表达式或条件就可以重复执行一个过程。

Image for post

Photo by Tine Ivanič on Unsplash

TineIvanič摄Unsplash

Suppose, we want to separate the letters of the string “programming” and add the letters as items of a new list. The first thing that comes to mind, might be writing a “for loop” 😀 for this operation.

假设我们要分隔字符串“ programming”的字母,并将字母添加为新列表的项目。 首先想到的可能是为此操作编写“ for循环”😀。

Example 1: Iterating through a string Using for Loop

示例1:使用for循环遍历字符串

p_letters = [] word = 'programming' for letter in word: p_letters.append(letter) print(p_letters)

When we run the program, the output will be:

当我们运行程序时,输出将是:

> ['p', 'r', 'o', 'g', 'r', 'a', 'm', 'm', 'i', 'n', 'g']

However, Python has an easier way to solve this issue using “List Comprehension”.

但是,Python使用“列表理解”可以更轻松地解决此问题。

List comprehension is an elegant and concise way to define and create lists, based on existing lists, tuples or strings 😎. Its powerful because it can auto detect the type of iterable passed to it, which can be string, tuple or list.

列表理解是一种基于现有列表,元组或字符串define定义和创建列表的简洁明了的方法。 它的功能强大,因为它可以自动检测传递给它的可迭代类型,可以是字符串,元组或列表。

Syntax of List Comprehension

列表理解的语法

[expression for item in list]

Let’s see how the above program can be written using list comprehensions.

让我们看看如何使用列表推导编写上面的程序。

Example 2: Iterating through a string using list comprehension technique

示例2:使用列表推导技术遍历字符串

p_letters = [ letter for letter in 'programming' ]
print( p_letters)

When we run the program, the output will be the same as example 1.

当我们运行该程序时,输出将与示例1相同。

In the above example, a new list is assigned to variable p_letters, as indicated by the square brackets. this new list contains the items of the iterable and in this case its a string ‘programming’. Then We call a print() function to show the output.

在上面的示例中,将新列表分配给变量p_letters,如方括号所示。 这个新列表包含可迭代项,在这种情况下为字符串“ programming”。 然后,我们调用print()函数以显示输出。

另一个提示某种形式的迭代的用例可能是获取一个单词的所有子字符串,并将它们作为新列表的项追加。 (Another use-case that prompts some form of iteration, might be getting all the sub-strings of a word and appending them as the items of a new list 🤔.)

Implementing this operation in python code using a rather traditional technique, while working with lterables, can grow to be quite verbose and less readable 😬.

使用lterables时,使用相当传统的技术在python代码中实现此操作可能会变得相当冗长且可读性较差。

Let’s see how the above program can be written using list comprehension .

让我们看看如何使用列表理解来编写上述程序。

Example 2: Get all sub-strings of given string, using list comprehension + string slicing

示例2:使用列表推导+字符串切片,获取给定字符串的所有子字符串

word = "programming"
subs_list=[ word[i: j]
for i in range(len(word))
for j in range(i + 1, len(word)+1) ]
print(subs_list)

In the above example, a new list is assigned to variable subs_list, as indicated by the square brackets. the expression here, is a string slicing method fitted with changing starting and ending indexes, to get all possibilities of the sub-strings from the word “programming”. Then We call print() function to show the output.

在上面的示例中,将新列表分配给变量subs_list,如方括号所示。 这里的表达式是一种字符串切片方法,它具有变化的起始索引和结束索引,以便从“编程”一词中获得所有子字符串的可能性。 然后,我们调用print()函数以显示输出。

True, you can certainly do all of these operations using traditional loops 🤷‍♂️. However, not every loop can be rewritten as list comprehension . But list comprehensions,

没错,您当然可以使用传统循环🤷‍♂️来完成所有这些操作。 但是,并非每个循环都可以重写为列表理解。 但是列表理解,

  • Can replace verbose loops and unreadable code with maintainable logic.

    可以用可维护的逻辑代替冗长的循环和不可读的代码。
  • With its elegant syntax it can increase performance as opposed to normal functions that create lists.

    凭借其优雅的语法,与创建列表的常规函数​​相比,它可以提高性能。
  • It can also be used in combination with normal loops, but avoid making your list comprehension become too large.

    它也可以与普通循环结合使用,但要避免列表理解太大。

I hope you write clean and maintainable python code with this technique at your disposal 😁.Thanks for the audience 🤗 and connect with me on Github, linkedIn and Twitter.

我希望您可以使用此技术编写干净且可维护的python代码,谢谢您。audience感谢观众🤗在GithublinkedInTwitter上与我联系。

Originally published at https://nextwebb.hashnode.dev.

最初发布在https://nextwebb.hashnode.dev

翻译自: https://medium.com/@nextwebb/python-techniques-introduction-to-python-list-comprehension-8fc2bcce2ef9

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值