English 中文(简体)
为什么[-1:-1:-1]不努力扭转扼杀?
原标题:Why does [-1:-1:-1] not work for reversing a string?

Maybe someone can explain why a particular slice operation to reverse a string doesn t work. There must be something about slices I don t understand. Basic operation: s[start:end:step]

s= abcd 

扭转这种说法,我可以采用缺省值。

s[::-1]

s[-1::-1]

这些是罚款,但为什么是罚款。

s[-1:-1:-1]

work and give me the whole string reversed? It should start at with the last character in the string at (-1), and the stepsize is -1. It should run down to one before -1 i.e., index 0. Instead I get .

f或i in xrange(3, -2, -1):
    print  s[-1:%d:-1] =>   %i,
    print s[-1:i:-1]

产量:

s[-1:3:-1] =>     # didn t expect any output
s[-1:2:-1] =>  d
s[-1:1:-1] =>  dc
s[-1:0:-1] =>  dcb
s[-1:-1:-1] =>    # shouldn t this be  dcba ?

我在这里失踪了吗?

(我确实看到许多关于倒塌方的布局,但如果对这个特定项目作了解释,我就没有这样做)

最佳回答
s[-1:-1:-1]

出于同样原因没有工作

s[0:0]

页: 1

当你照相时,你指的是between<>/em>两点,而不是这些元素的位置。 因此,你要求把1-1到1号空间,即:

问题回答

In

>>> s[-1:-1:-1]
>>>   

。 页: 1 因此,它们是相同的。 斜体从<条码><>>> >到<条码>>>>>>>> 至<条码>>的阵列中每一元素均在<条码>上加起来。 每一次。 如果已经达到<代码>end 那么,这片lic就成了......。 因此,见<条码>。

Try:

>>> s[1:1:1]
>>>   

这样做可能更有意义,因为它几乎相同,但是积极的。

在这方面,消极指数和非消极指数是两个单独的类别:

  • non-negative indices start from the beginning;
  • negative indices start from the end.

我认为,对我来说,许可的定义似乎非常合理,这样,后者就永远不会流入前者,正如你在说:

指数0

你错过了整个切题。

租船经营人喜欢什么。

if stride is negative, then definitely your start should be greater than end as you are moving from high to low. All your examples which fails violates that.

http://docs.python.org/release/2.3.5/whatsnew/section-slices.html 扩展文件

页: 1 页: 1 这将不一致。

或许,你会重新想象一下,在把扼杀作为一种循环处理时,会同时考虑<代码>-1<<>>>>。 但情况并非如此。 例如,如果是这样的话,那么就认为:

 abcd [-2:-1:-1] ==  cbad 

但它没有。 结果是空洞的。

>>>  abcd [-2:-1:-1] 
  

插图中负面指数的正确解释是,从胎体末起,而不是从<代码>0中逐年指数的反向指数。

为了进一步思考,考虑如下:

a =  abcd 
a[-1:-(len(a) + 0):-1] ==  dcb 
a[-1:-(len(a) + 1):-1] ==  dcba 
a[-1:-(len(a) + 2):-1] ==  dcba 

这种不幸吗? 这里是一场ward浅的工作。

for i in range(3, -2, -1):
    j = i if i != -1 else None
    print  s[-1:%s:-1] =>   % str(j),
    print s[-1:j:-1]

s[-1:3:-1] =>  
s[-1:2:-1] =>  d
s[-1:1:-1] =>  dc
s[-1:0:-1] =>  dcb
s[-1:None:-1] =>  dcba

这里的情况也有所不同。 我不敢肯定它会给它带来多少麻烦。

for i in range(3, -1, -1) + [None]:
    print  s[-1:%s:-1] =>   % str(i),
    print s[-1:i:-1]

s[-1:3:-1] =>  
s[-1:2:-1] =>  d
s[-1:1:-1] =>  dc
s[-1:0:-1] =>  dcb
s[-1:None:-1] =>  dcba




相关问题
Simple JAVA: Password Verifier problem

I have a simple problem that says: A password for xyz corporation is supposed to be 6 characters long and made up of a combination of letters and digits. Write a program fragment to read in a string ...

Case insensitive comparison of strings in shell script

The == operator is used to compare two strings in shell script. However, I want to compare two strings ignoring case, how can it be done? Is there any standard command for this?

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

String initialization with pair of iterators

I m trying to initialize string with iterators and something like this works: ifstream fin("tmp.txt"); istream_iterator<char> in_i(fin), eos; //here eos is 1 over the end string s(in_i, ...

break a string in parts

I have a string "pc1|pc2|pc3|" I want to get each word on different line like: pc1 pc2 pc3 I need to do this in C#... any suggestions??

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...