Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 未拾取字符串的最后一个字符_Python_Arrays_String_List - Fatal编程技术网

Python 未拾取字符串的最后一个字符

Python 未拾取字符串的最后一个字符,python,arrays,string,list,Python,Arrays,String,List,试图解决一个问题,即我可以反转字符串中的每个单词,因为python中没有“\0”,不像C,我的逻辑无法提取字符串的最后一个字符。 你知道如何在不太多修改代码的情况下解决这个问题吗 Input = This is an example Output = sihT si na elpmaxe import os import string a = "This is an example" temp=[] store=[] print(a) x=0 while (x <= len(a)-

试图解决一个问题,即我可以反转字符串中的每个单词,因为python中没有“\0”,不像C,我的逻辑无法提取字符串的最后一个字符。 你知道如何在不太多修改代码的情况下解决这个问题吗

Input  = This is an example
Output = sihT si na elpmaxe 

import os
import string

a = "This is an example"
temp=[]
store=[]
print(a)
x=0
while (x <= len(a)-1):

    if ((a[x] != " ") and (x != len(a)-1)):
       temp.append(a[x])
       x += 1

    else:
            temp.reverse()
            store.extend(temp)
            store.append(' ')
            del temp[:]
            x += 1

str1 = ''.join(store)
print (str1)

您在
len(a)-1中删除了
-1
,在
中更改了顺序(因此当
x==len(a)
时,它将不会尝试获取
a[x]
,这可能会导致
索引超出范围“


while(x您在
len(a)-1
中删除
-1
,并在
中更改顺序(因此当
x==len(a)
时,它将不会尝试获取
a[x]
,这可能会给出
“索引超出范围”


while(xAs
pvg
建议,您自己排除最后一个字符。您不需要检查
x!=len(a)-1
,以便您可以在
temp
字符串中添加最后一个字符。退出循环后可以添加的最后一个字将包含在
temp
变量中。此提示只是为了让代码正常工作,否则您可以按照人们的建议在python中以更短的方式执行。

as
pvg
suggested,您自己正在排除最后一个字符。您不需要检查
x!=len(a)-1
,以便您可以在
temp
字符串中添加最后一个字符。退出循环后可以添加的最后一个字将包含在
temp
变量中。此提示只是为了让您的代码正常工作,否则您可以按照人们的建议在python中以更短的方式完成此操作。

它非常简单且简单不需要额外的循环:

a = "This is an example"
print(a)
str1 = " ".join([word[::-1] for word in a.split(" ")])
print(str1)
输入和输出:

This is an example
sihT si na elpmaxe

它非常简单,不需要额外的循环:

a = "This is an example"
print(a)
str1 = " ".join([word[::-1] for word in a.split(" ")])
print(str1)
输入和输出:

This is an example
sihT si na elpmaxe


您有一个条件专门排除了最后一个字符。您写c的时间太长了。
print''。如果我这样做,请在a.split()中为word加入(word[:-1)]
@pvg:if((a[x]!=“”)和(x!=len(a)))我的输出会完全截断最后一个单词。输出为:sihT sina@pvg对于while循环,如果我这样做:while(x不清楚您为什么不想更改代码,但正如@Holloway所演示的,您试图做的事情可以在短时间内以pythonical方式完成。是否有理由维护您的代码?您有一个特别排除最后一个字符的条件。您编写c的时间太长了。
print'。join(word[:-1]对于a.split()中的单词
@pvg如果我这样做:如果((a[x]!=“”)和(x!=len(a)),我的输出将完全截断最后一个单词。输出为:sihT sina@pvg对于while循环,如果我这样做:while(x不清楚您为什么不想更改代码,但正如@Holloway所演示的,您试图做的事情可以在短时间内以pythonical方式完成。是否有理由维护您的代码?仍然存在内存冲突“Indexer:字符串索引超出范围”@Fenomatik您是否更改了
中的顺序?我刚刚更改了,所以我假设有一个从左到右的条件顺序?它必须首先检查
x!=len(a)
,当它为false时,它将不会执行
a[x]
,这会给出
“索引超出范围”
它工作得非常好,知道有一个从左到右的条件检查。你的回答修复了这个错误,不像其他人给了我一个使用方法的方法。仍然存在内存冲突“Indexer:字符串索引超出范围”@Fenomatik您是否更改了
中的顺序?我刚刚更改了,所以我假设有一个从左到右的条件顺序?它必须首先检查
x!=len(a)
,当它为false时,它将不会执行
a[x]
,这会给出
“索引超出范围”
它工作得非常好,学会了从左到右的条件检查。你的回答修复了这个错误,不像其他人给了我一个使用方法的方法。人太快了,就让它作为装饰契约存在吧。有疑问吗?@pylang人太快了,就让它作为装饰契约存在吧。有疑问吗?@pylang
This is an example
sihT si na elpmaxe