使用`.join()`和`.pop()`在一行上出现问题。

huangapple go评论68阅读模式
英文:

Issues using .join() and .pop() on one line

问题

我刚刚花了很多时间试图解决 Edabit.com 上的以下非常低级的编码挑战:

这是一个字符列表,末尾有一个不想要的字符:
["H", "e", "l", "l", "o", "!", "\0"]

你也可以在初始化变量时直接输入"Hello!",从而创建字符串"Hello!"

创建一个函数,该函数将通过组合给定的字符列表来返回一个字符串,但不包括不想要的最后一个字符。示例:

cpp_txt(["H", "i", "!", "\0"]) ➞ "Hi!"

我无法解决以下一行的尝试:

return "".join(lst.pop(-1))

return "".join(lst.pop())

这样做会得到非常奇怪的结果:

FAILED: '\x00' 应该等于 'Hi!'
ERROR: Traceback:
in
File "./frameworks/python/cw-2.py", line 28, in assert_equals
expect(actual == expected, message, allow_raise)
File "./frameworks/python/cw-2.py", line 18, in expect
raise AssertException(message)
cw-2.AssertException: '\x00' 应该等于 'Hi!'

我尝试了各种各样的东西,比如 .strip('\x00'),但都没用。我用了一个 for 循环,得到了像 'Hi!x00' 这样的结果。

最终我放弃了,解决方案要么是这样的:

def cpp_txt(lst):
    return ''.join(lst[:-1])

这可能是我一开始应该想到的,或者是这样的:

def cpp_txt(lst):
    a = lst.pop(-1)
    return "".join(lst)

现在,分析第二种解决方案,我想我可能在使用 .pop() 时搞错了,似乎你需要创建一个单独的变量来能够 '清理' 列表,但在我在另一个网站的浏览历史中找到的以下示例中,这似乎并不是必要的:

data_science_topics = ["Machine Learning", "SQL", "Pandas", "Algorithms", "Statistics", "Python 3"]
print(data_science_topics)

# 以下是你的代码:
data_science_topics.pop()
print(data_science_topics)
data_science_topics.pop(3)
print(data_science_topics)

所以,我希望有人能够友好地解释为什么我的结果中会出现奇怪的 "\x00",以及为什么我不应该像这样在一行上使用 .join() 和 .pop()。感谢你的时间和耐心。

英文:

I just spent a lot of time trying to solve the following very low level coding challenge on Edabit.com:

> This is a list of single characters with an unwanted character at the end:
>
> ["H", "e", "l", "l", "o", "!", "\0"]
>
> You could also just type "Hello!" when initializing a variable,
> creating the string "Hello!"
>
> Create a function that will return a string by combining the given
> character list, not including the unwanted final character. Examples
>
> cpp_txt(["H", "i", "!", "\0"]) ➞ "Hi!"

I couldn't solve this trying the following one one line:

return "".join(lst.pop(-1))

or return "".join(lst.pop())

I get very strange results this way:

FAILED: '\x00' should equal 'Hi!'
ERROR: Traceback:
   in <module>
  File "./frameworks/python/cw-2.py", line 28, in assert_equals
    expect(actual == expected, message, allow_raise)
  File "./frameworks/python/cw-2.py", line 18, in expect
    raise AssertException(message)
cw-2.AssertException: '\x00' should equal 'Hi!'

I tried all kinds of things, like .strip('\x00') but nothing worked. I used a for loop, I got results like 'Hi!x00'.

Finally I gave up, and the solutions are either this:

def cpp_txt(lst):
return ''.join(lst[:-1])

that was probably what I should've thought of in the first place,
or this:

def cpp_txt(lst):
a = lst.pop(-1)
return "".join(lst)

Now, analyzing the second solution I guess I am using .pop() wrong, it seems like you need to create a separate variable to be able to 'clean up' the list, but this does not seem to be necessary in the following example I found in my browsing history on another website:

data_science_topics = ["Machine Learning", "SQL", "Pandas", "Algorithms", "Statistics", "Python 3"]
print(data_science_topics)

# Your code below: 
data_science_topics.pop()
print(data_science_topics)
data_science_topics.pop(3)
print(data_science_topics)

So, I hope someone would be so kind to explain me why the weird "\x00" appears in my results and how come I shouldn't be using .join() and .pop() like this on a single line

Thank you for your time and patience.

答案1

得分: 3

当你执行以下代码:

return "".join(lst.pop(-1))

等同于:

def cpp_txt(lst):
    a = lst.pop(-1)
    return "".join(a)  # 注意:这不是 lst

在你的工作解决方案中,你想要连接lst - 即return "".join(lst)。但这不是pop返回的内容。

.pop()修改了提供给它的列表 - 并且只返回最后一个元素。

英文:

When you do:

return "".join(lst.pop(-1))

It is equivalent of:

def cpp_txt(lst):
    a = lst.pop(-1)
    return "".join(a)  # NOTE: This is a ... not lst

In your working solution, you wanted to join the lst - i.e. return "".join(lst). But that is not what pop returns.

.pop() modifies the list provided to it - and returns just the last element.

答案2

得分: 1

.pop() 修改了列表,这就是为什么最后的示例有效,但它返回的是最后弹出的元素,而不是整个列表,所以你只是连接了包含不需要字符的字符串,而不是其他字符的列表。

英文:

.pop() modifies the list, which is why the last example works, but it returns the last element that was popped off, not the whole list, so you're joining just the string containing the unwanted character instead of the list of other characters.

huangapple
  • 本文由 发表于 2023年6月11日 20:28:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76450476.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定