在Python中使用.split()和.join() [英] Using .split() and .join() in Python

查看:41
本文介绍了在Python中使用.split()和.join()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在Treehouse中学习一些Python,但是遇到了这一挑战,不知道自己在做什么错.挑战分为三个部分,下面显示提示和我编写的代码.我似乎在第三部分犯了错误.

I am currently learning some Python in Treehouse, but I came upon this challenge and do not know what I am doing wrong. The challenge is split into three parts, shown below with the prompt and the code I have written. I seem to be making the mistake in the third part.

第1部分:

我想是时候吃点零食了.幸运的是,我有一连串的冰淇淋圣代.不幸的是,它们全都在一个字符串中,并且字符串中也有分号.使用.split()可以在分号(;)上分开可用的字符串.将此分配给新的变量圣代冰淇淋.

I think it's time for a snack. Luckily I have a string full of types of ice cream sundaes. Unluckily, they're all in one string and the string has semi-colons in it too. Use .split() to break the available string apart on the semi-colons (;). Assign this to a new variable sundaes.

available = "banana split;hot fudge;cherry;malted;black and white"
sundaes = available.split(";")

第2部分:

让我们添加一个新的字符串变量来显示我们的菜单项.创建一个名为 menu 的新变量,该变量设置为我们可用的口味是:{}.".

Let's add a new string variable for displaying our menu items. Make a new variable named menu that's set to "Our available flavors are: {}.".

available = "banana split;hot fudge;cherry;malted;black and white"
sundaes = available.split(";")
menu = "Our available flavors are: {}."

第3部分:

好的,让我们完成菜单的制作.将圣代冰淇淋列表合并到一个名为display_menu的新变量中,该列表中的每个项目都用逗号和空格(,")重新连接在一起.然后重新分配菜单变量以使用现有变量,并使用.format()将占位符替换为display_menu中的新字符串.如果您真的很勇敢,甚至可以在当前设置菜单的同一行上完成所有操作.

Alright, let's finish making our menu. Combine the sundaes list into a new variable named display_menu, where each item in the list is rejoined together by a comma and a space (", "). Then reassign the menu variable to use the existing variable and .format() to replace the placeholder with the new string in display_menu. If you're really brave, you can even accomplish this all on the same line where menu is currently being set.

available = "banana split;hot fudge;cherry;malted;black and white"
sundaes = available.split(";")
display_menu = sundaes.join(", ")
menu = "Our available flavors are: {}.".format(display_menu)

每当我运行第三部分时,Treehouse都会显示任务1似乎不再通过",但是当我独自运行第一部分时,Treehouse接受了它.

Whenever I run the third part, Treehouse reads, "It looks like Task 1 is no longer passing," but when I ran the first part by itself, Treehouse accepted it.

谢谢.

推荐答案

您应该使用

display_menu = ", ".join(sundaes)

sundaes 是一个列表,它没有 .join ,您可以通过打开python解释器并运行:

sundaes is a list and it doesn't have .join, you can check by opening a python interpreter and running :

>>> dir(list)

但是字符串对象没有 .join

>>> dir(str)

并运行

>>> help(str.join)

我们可以看到说明关于method_descriptor的帮助:

we can see the description Help on method_descriptor:

join(...)
S.join(iterable) -> string

Return a string which is the concatenation of the strings in the
iterable.  The separator between elements is S.

这篇关于在Python中使用.split()和.join()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆