Python—编写一个函数,将字符串作为参数,并向后显示字母,每行一个
这是一个来自“如何像计算机科学家一样思考”的练习。我正在学习Python/编程,不知道如何完成这项任务 这是书中的一个例子,显示了字母向前,我不知道如何得到相反的效果。必须使用while循环Python—编写一个函数,将字符串作为参数,并向后显示字母,每行一个,python,traversal,while-loop,Python,Traversal,While Loop,这是一个来自“如何像计算机科学家一样思考”的练习。我正在学习Python/编程,不知道如何完成这项任务 这是书中的一个例子,显示了字母向前,我不知道如何得到相反的效果。必须使用while循环 fruit = 'banana' index = 0 while index > len(fruit): letter = fruit[index] print letter index = index + 1 嗯,基本上是一样的,但是: 你必须从最后
fruit = 'banana'
index = 0
while index > len(fruit):
letter = fruit[index]
print letter
index = index + 1
嗯,基本上是一样的,但是:
index=0
,而是index=len(水果)-1
index=index+1
变为index=index-1
索引
指向有效的字符索引,您就希望留在循环中。由于index
从len(fruit)-1开始,每次迭代后它会变小一个,最终它会变小到小于零。零仍然是一个有效的字符索引(它引用字符串的第一个字符),因此只要索引>=0
——这将是while
条件,您就希望留在循环中
fruit = 'banana'
index = len(fruit) - 1
while index >= 0:
letter = fruit[index]
print letter
index = index - 1
嗯,基本上是一样的,但是:
index=0
,而是index=len(水果)-1
index=index+1
变为index=index-1
索引
指向有效的字符索引,您就希望留在循环中。由于index
从len(fruit)-1开始,每次迭代后它会变小一个,最终它会变小到小于零。零仍然是一个有效的字符索引(它引用字符串的第一个字符),因此只要索引>=0
——这将是while
条件,您就希望留在循环中
fruit = 'banana'
index = len(fruit) - 1
while index >= 0:
letter = fruit[index]
print letter
index = index - 1
最简单的是:
>>> def print_reversed(s):
... for letter in reversed(s):
... print letter,
...
>>> print_reversed('banana')
a n a n a b
>>>
其他可能的解决方案是将索引作为字符串的最后一个索引。然后,将逐个字母向后读取字符串,每次将索引值降低1。然后,您展示的代码snipplet可能会变成:
>>> def print_reversed2(s):
... index = len(s) - 1
... while index >= 0:
... letter = fruit[index]
... print letter
... index = index - 1
...
>>> print_reversed2('banana')
a
n
a
n
a
b
>>>
使用交互式解释器(只需在命令提示符中键入“python”)可以帮助您试验此类代码片段。例如:
>>> fruit = 'banana'
>>> len(fruit)
6
>>> len(fruit) - 1
5
>>> while index >= 0:
... print "index at: " + str(index)
... print "fruit[index] at: " + fruit[index]
... index = index - 1
...
index at: 5
fruit[index] at: a
index at: 4
fruit[index] at: n
index at: 3
fruit[index] at: a
index at: 2
fruit[index] at: n
index at: 1
fruit[index] at: a
index at: 0
fruit[index] at: b
>>>
最简单的是:
>>> def print_reversed(s):
... for letter in reversed(s):
... print letter,
...
>>> print_reversed('banana')
a n a n a b
>>>
其他可能的解决方案是将索引作为字符串的最后一个索引。然后,将逐个字母向后读取字符串,每次将索引值降低1。然后,您展示的代码snipplet可能会变成:
>>> def print_reversed2(s):
... index = len(s) - 1
... while index >= 0:
... letter = fruit[index]
... print letter
... index = index - 1
...
>>> print_reversed2('banana')
a
n
a
n
a
b
>>>
使用交互式解释器(只需在命令提示符中键入“python”)可以帮助您试验此类代码片段。例如:
>>> fruit = 'banana'
>>> len(fruit)
6
>>> len(fruit) - 1
5
>>> while index >= 0:
... print "index at: " + str(index)
... print "fruit[index] at: " + fruit[index]
... index = index - 1
...
index at: 5
fruit[index] at: a
index at: 4
fruit[index] at: n
index at: 3
fruit[index] at: a
index at: 2
fruit[index] at: n
index at: 1
fruit[index] at: a
index at: 0
fruit[index] at: b
>>>
我认为最简单的方法是
print ''.join(reversed('banana'))
或者,如果你想每行写一个字母
print '\n'.join(reversed('banana'))
我认为这样更好,因为join是对字符串进行操作的标准方法,所以…我认为最简单的方法是
print ''.join(reversed('banana'))
或者,如果你想每行写一个字母
print '\n'.join(reversed('banana'))
我认为它更好,因为join是对字符串进行操作的标准方法,所以…当index
。另一个琐碎的练习,要求你以错误的方式做事。@Ignacio-认真;迭代需要更加普遍:|@伊格纳西奥:没有一本CS101的书会教人们用特定于Python的方式做事,他们也不应该这样做。在看迭代器之前,您需要学习如何处理循环、变量和条件——显然,简单地调用reversed
根本不会教任何东西。当index
时,它不应该是吗。另一个琐碎的练习,要求你以错误的方式做事。@Ignacio-认真;迭代需要更加普遍:|@伊格纳西奥:没有一本CS101的书会教人们用特定于Python的方式做事,他们也不应该这样做。在查看迭代器之前,您需要学习如何处理循环、变量和条件——显然,简单地调用reversed
根本不会教任何东西。