pt100 python_使用列表pt 1的所有人的python

pt100 python

The University of Michigan offers a Python specialization on Coursera called Python for Everybody. Charles Severance, who is humorous and witty, teaches these courses using Python 3. The second course — Data Structures — explores the Python language through learning to navigate text files and trying to manipulate or retrieve data to achieve a desired result (such as finding the most common word in the file).

密歇根大学在Coursera上提供了Python专业化课程,称为Python for Everybody。 幽默风趣的Charles Severance使用Python 3教授这些课程。第二门课程-数据结构-通过学习导航文本文件并尝试操纵或检索数据以获得期望的结果(例如查找文件中最常见的词)。

制作清单 (Making Lists)

The professor compares lists to pieces of luggage with multiple items inside, and each piece of luggage has a name. To create a list of markers, for example, programmers can use the following code:

教授将清单与内部有多个物品的行李箱进行比较,每个行李箱都有一个名称。 例如,要创建标记列表,程序员可以使用以下代码:

>>>markers = [‘blue’, ‘red’, ‘yellow’]>>>print(markers[1])red

>>> markers = ['blue','red','yellow'] >>> print(markers [1])red

Image for post
A simple list
一个简单的清单

Lists are mutable. For example, the string ‘red’ can be changed to ‘green’:

列表是可变的。 例如,字符串“ red”可以更改为“ green”:

>>>markers[1] = ‘green’>>>print(markers[1])green

>>> markers [1] ='green'>>>打印(markers [1])green

To build a list of markers from the ground up, programmers can use the following code that creates an empty list and adds elements with the append method:

为了从头开始构建标记列表,程序员可以使用以下代码创建一个空列表并使用append方法添加元素:

>>>markers = list()>>>markers.append(‘brown’)>>>markers.append(‘black’)>>>print(markers)[‘brown’, ‘black’]

>>> markers = list()>>> markers.append('brown')>>> markers.append('black')>>> print(markers)['brown','black']

Image for post
Adding items to an empty list using the append method
使用append方法将项目添加到空列表

使用分割方法 (Using the Split Method)

Once programmers are able to create their own lists, they can begin to do some interesting things when exploring text files. Before turning a text file into a list of strings, beginners might want to start breaking a simple string into a list. Do this using the split method:

程序员一旦能够创建自己的列表,就可以在浏览文本文件时开始做一些有趣的事情。 在将文本文件转换为字符串列表之前,初学者可能要开始将简单的字符串拆分为列表。 使用split方法执行此操作:

>>>words = ‘Here it is’>>>wordparts = words.split()>>>print(wordparts)[‘Here’, ‘it’, ‘is’]

>>> words ='在这里'>>>> wordparts = words.split()>>> print(wordparts)['Here','it','is']

In the above example the text is split according to spaces between words. Programmers can also split a text according to other characteristics. Let’s say we would like to split words according to the @ symbol in an email address:

在上面的示例中,根据单词之间的间距对文本进行了拆分。 程序员还可以根据其他特征来分割文本。 假设我们想根据电子邮件地址中的@符号分割单词:

>>>email = ‘helloworld@outlook.com’>>>emailparts = email.split(‘@’)>>>print(emailparts)[‘helloworld’, ‘outlook.com’]

>>> email ='helloworld@outlook.com'>>> emailparts = email.split('@')>>> print(emailparts)['helloworld','outlook.com']

Image for post
Splitting an email into parts
将电子邮件分为几部分

Let’s say the programmer would like to loop through each word in a list and print it out.

假设程序员想遍历列表中的每个单词并将其打印出来。

>>>for parts in wordparts:>>>>>>print(parts)Hereitis

>>>对于单词中的部分:>>>>>>打印(部分)

Remember, indention in Python is important. Without an indention in the appropriate places you’ll receive an indention error.

请记住,Python中的缩进很重要。 如果在适当的位置没有缩进,则会收到缩进错误。

实际应用 (Practical Application)

Now what can I as a programmer do with the above information? Well, let’s say I have a long text with thousands of emails. Perhaps I would like to print out emails with “gmail.com” while excluding all other emails that belong to other email services, such as Yahoo Mail or Outlook. Let’s assume these particular emails we are looking for begin in lines that all feature “From” as their first word — much like this:

现在,作为程序员,我可以使用上述信息做什么? 好吧,比方说,我的文字很长,包含数千封电子邮件。 也许我想用“ gmail.com”打印电子邮件,同时排除属于其他电子邮件服务(例如Yahoo Mail或Outlook)的所有其他电子邮件。 假设我们正在寻找的这些特殊电子邮件以所有以“发件人”为第一个单词的行开头-就像这样:

“From helloworld@gmail.com Fri Aug 12th”

“来自helloworld@gmail.com 8月12日星期五”

Programmers need to first open the text file.

程序员需要首先打开文本文件。

>>>fhand = open(‘atextfile.txt’)

>>> fhand = open('atextfile.txt')

After this, we need a for loop to look through each line of the text:

之后,我们需要一个for循环来浏览文本的每一行:

>>>for line in fhand:

>>>对于手中的线:

We also need to strip any white space at the end of each line using the strip method:

我们还需要使用strip方法在每行的末尾删除任何空白:

>>>>>>line = line.rstrip()

>>>>>> line = line.rstrip()

Next, we skip over any line that doesn’t start with “From”:

接下来,我们跳过任何以“ From”开头的行:

>>>>>>if not line.startswith(‘From’): >>>>>>>>>continue

>>>>>>如果不是line.startswith('From'):>>>>>>>>>继续

We split each word in the sentence we are looking for (“From helloworld@gmail.com Fri Aug 12th”) into a list:

我们将要查找的句子中的每个单词(“从helloworld@gmail.com 8月12日星期五”拆分为一个列表):

>>>>>>words = line.split()

>>>>>> words = line.split()

The list should look something like this if you print it out:

如果将其打印出来,列表应该看起来像这样:

[‘From’, ‘helloworld@gmail.com’, ‘Fri’, ‘Aug’, ‘12th’]

[“发件人”,“ helloworld@gmail.com”,“周五”,“八月”,“第十二”]

Now we need to grab the email address. In the above list we have five items, but our list starts with 0, so, since this is the case, we need to use sub one:

现在我们需要获取电子邮件地址。 在上面的列表中,我们有五项,但我们的列表以0开头,因此,既然是这种情况,我们需要使用sub one:

>>>>>>email = words[1]

>>>>>>电子邮件=单词[1]

Now we have our email!

现在我们有了我们的电子邮件!

>>>>>>print(email)helloworld@gmail.com

>>>>>>打印(电子邮件)helloworld@gmail.com

What if we want to do another split? This is called a double split pattern.

如果要再次拆分怎么办? 这称为双分割模式。

>>>>>>emailparts = email.split(‘@’)

>>>>>> emailparts = email.split('@')

If you were to print out emailparts at this point, you would have the following output:

如果此时要打印出电子邮件部分 ,将获得以下输出:

[‘helloworld’, ‘gmail.com’]

['helloworld','gmail.com']

Now you can print out the individual parts of the email:

现在,您可以打印出电子邮件的各个部分:

>>>>>>print(emailparts[1])gmail.com

>>>>>>打印(emailparts [1])gmail.com

When all is said and done the double split pattern looks like this:

说完所有步骤后,双重拆分模式如下所示:

Image for post
A code sample of the double split pattern from Python for Everybody
Python for Everybody的双重拆分模式的代码示例

翻译自: https://medium.com/codingskool/python-for-everybody-using-lists-pt-1-2671d7524f90

pt100 python

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值