- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我是 python 的新手,所以请原谅外行的描述。
问题:给定一个句子,返回一个单词颠倒的句子
我的解决方案:
def sentence_reverse(text):
a = text.split()
a.reverse()
return ' '.join(a)
输出:
sentence_reverse('I am home') --> 'home am I'
sentence_reverse('We are ready') --> 'ready are We'
代码按预期方式工作,但我的问题是,为什么我不能直接从前一个函数的返回结果进行函数调用。我,我
为什么我不能做类似的事情
返回 ' '.join(text.split().reverse())
这在 java 中工作得很好,因为我从 text.split() 得到 List 对象,我将使用 reverse() 函数对其进行操作,该函数将返回倒排列表,然后由 join( ) 将这个列表变成一个字符串。
这个,也是我试过的:
text.split() 返回一个列表,并在其上应用 .reverse() 函数应该以相反的顺序返回同一列表的元素,但是当我执行这一行时, print(text.split().reverse())
,我得到 None
最佳答案
因为 reverse
返回 None,并更新列表,所以 None 对 join
无效。
是的,所以 reverse
是 inplace
函数,因此无法修复它。
所以使用反转
:
return ' '.join(reversed(text.split()))
或者[::-1]
:
return ' '.join(text.split()[::-1])
来自文档:
You might have noticed that methods like insert, remove or sort that only modify the list have no return value printed – they return the default None. [1] This is a design principle for all mutable data structures in Python.
After code example.
关于 python : Calling functions from inside another function parameter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52999854/
我在stackoverflow上查过很多类似的问题,比如call.call 1 , call.call 2 ,但我是新人,无法发表任何评论。我希望我能找到关于 JavaScript 解释器如何执行这些
“strace 是一个系统调用跟踪器,即一个调试工具,它打印出另一个进程/程序进行的所有系统调用的跟踪。”如果系统调用递归工作或一个系统调用调用另一个系统调用怎么办。我怎样才能得到这些信息? 可能的解
我的问题很简单:我正在将一个函数传递给其他一些稍后调用的函数(示例回调函数),问题是何时、为何以及最佳做法是什么。 样本:我有 xxx() 函数,我必须传递它,如下面的 window.onload 事
我是 Java 新手,我正在尝试学习 ScheduledExecutorService 接口(interface)。我在网上看到了下面的代码。 我没有看到任何对 Callable.call() 方法的
这是我的调用过程: System.out.println
在 typescript 中,我有一个 DataAccess 类,以便所有 Ajax 调用都通过单个对象进行路由,以节省应用程序中许多地方的代码重复。 在使用这种方法时,我需要使用回调将响应返回到调用
如何使用模拟来计算通过 call 或 apply 进行的函数调用 // mylib.js module.exports = { requestInfo: function(model, id) {
每次我尝试roxygenize 一个包我都会得到这个错误: Error: is.call(call) is not TRUE traceback() 的结果: 11: stop(sprintf(nge
这里如果我有一个记录“调用我的函数”的函数 function myFunction() { console.log('called my function') } Function.prototy
在 Javascript 中,Function.call() 可以在给定 this 值和零个或多个参数的情况下调用 Function。 Function.call 本身就是一个函数。所以理论上,Fun
这个问题已经有答案了: "object is not a function" when saving function.call to a variable (3 个回答) a is a functi
在调用 UITableView 上的 reloadData 方法后,我曾多次遇到此问题,但我不明白为什么? 这是一个问题,因为如果更新 TableView 的数据,tableview将不必要地查询不存
我继承了大约 400 行写得非常奇怪的 Fortran 77 代码,我正在尝试逐步分析它以使其在我的脑海中清晰。 无论如何,我有一个类似 header 的文件(实际上是一个 .h,但其中的代码是 fo
这是我的代码 class AuthAction(callbackUri:String) extends ActionBuilder[UserRequest] with ActionRefiner[
我继承了大约 400 行写得非常奇怪的 Fortran 77 代码,我正在尝试逐步分析它以使其在我的脑海中清晰。 无论如何,我有一个类似 header 的文件(实际上是一个 .h,但其中的代码是 fo
我知道这个问题之前在这里被问过 iOS 6 shouldAutorotate: is NOT being called .但我的情况有点不同。 最初,在应用程序启动时,我加载了一个 viewContr
我是 headfirst 设计模式的读者,我注意到了这一点。 “好莱坞原则,别叫我们,我们叫你” 这意味着高级组件告诉低级组件“不要调用我们,我们调用你” High-Level Component 是
这个问题在这里已经有了答案: Why does passing variables to subprocess.Popen not work despite passing a list of ar
我刚找到一个覆盖 OnPaintBackground 的表单。奇怪的是它从来没有被调用过!就像,完全一样。为什么是这样?表单被刷新、移动、调整大小等等,所以它应该一些重新绘制,对吧? 最佳答案 是否设
调用函数的方式 考虑这个简单的函数: function my(p) { console.log(p) } 我可以这样调用它: my("Hello"); 也像这样: my.call(this, "Hel
我是一名优秀的程序员,十分优秀!