简体   繁体   English

str.index() 和 list.index() 有什么区别?

[英]What is the difference between str.index() and list.index()?

Is it possible to use str.index() method instead of list.index() to solve this problem and what is the difference between them both after 2 times of the execution of while-loop in both cases (as shown below)?是否可以使用str.index()方法而不是list.index()来解决这个问题,在两种情况下执行 2 次 while-loop 后它们有什么区别(如下所示)?

During the code, I was working on a task that looks like this:在编写代码期间,我正在处理如下所示的任务:

string = 'ABCDEFG'

  1. Assign 0 to the variable var .0分配给变量var
  2. Add 1 to var .1添加到var
  3. Swap the letter at position var with the letter at position var + 2 .将 position var处的字母与 position var + 2处的字母交换。
  4. if var < 4 go to Step 2. if var < 4 go 到步骤 2。
  5. End.结尾。

The main question of the task: What will be the string at Step 5?任务的主要问题:第 5 步的字符串是什么?

The right output should be CDEFABG右边的output应该是CDEFABG

After some time of thinking about it I came with the right solution using slist.index() :经过一段时间的思考,我使用slist.index()提出了正确的解决方案:

string = 'ABCDEFG'
slist = list(string)
var = 0

while var < 4:
    #assigned a letter to a variable
    letter_1, letter_2 = slist[var], slist[var+2]
    #assigned a letter index to a variable
    l_1, l_2 = slist.index(letter_1), slist.index(letter_2)
    #swaped two letters with each other
    slist[l_1], slist[l_2] = letter_2, letter_1
    var = var+1

string = ''.join(slist)

print(slist)
print(string)

Output: Output:

['C', 'D', 'E', 'F', 'A', 'B', 'G']
CDEFABG

But before I came out to this I tried first to implement string.index() instead of slist.index() :但在我出来之前,我首先尝试实现string.index()而不是slist.index()

#assigned a letter to a variable
letter_1, letter_2 = string[var], string[var+2]
#assigned a letter index to a variable
l_1, l_2 = string.index(letter_1), string.index(letter_2)

And after 2 times of looping while var < 4 it started to execute different outputs in contrast to the right solution with list.index() and gave the outputs like this:while var < 4循环 2 次后,它开始执行不同的输出,与使用list.index()的正确解决方案形成对比,并给出如下输出:

['C', 'B', 'A', 'D', 'E', 'F', 'G']
CBADEFG

['C', 'D', 'A', 'B', 'E', 'F', 'G']
CDABEFG

['C', 'D', 'E', 'B', 'C', 'F', 'G']
CDEBCFG #the right one is CDEBAFG

['C', 'D', 'E', 'F', 'C', 'D', 'G']
CDEFCDG #the right one is CDEFABG

Is it possible to use str.index() method instead of list.index() to solve this problem and what is the difference between them both after 2 times of the execution of while-loop in both cases (as shown below)?是否可以使用str.index()方法而不是list.index()来解决这个问题,在两种情况下执行 2 次 while-loop 后它们有什么区别(如下所示)?

During the code, I was working on a task that looks like this:在编写代码期间,我正在处理如下所示的任务:

string = 'ABCDEFG'

  1. Assign 0 to the variable var .0分配给变量var
  2. Add 1 to var .1添加到var
  3. Swap the letter at position var with the letter at position var + 2 .将 position var处的字母与 position var + 2处的字母交换。
  4. if var < 4 go to Step 2. if var < 4 go 到步骤 2。
  5. End.结尾。

The main question of the task: What will be the string at Step 5?任务的主要问题:第 5 步的字符串是什么?

The right output should be CDEFABG右边的output应该是CDEFABG

After some time of thinking about it I came with the right solution using slist.index() :经过一段时间的思考,我使用slist.index()提出了正确的解决方案:

string = 'ABCDEFG'
slist = list(string)
var = 0

while var < 4:
    #assigned a letter to a variable
    letter_1, letter_2 = slist[var], slist[var+2]
    #assigned a letter index to a variable
    l_1, l_2 = slist.index(letter_1), slist.index(letter_2)
    #swaped two letters with each other
    slist[l_1], slist[l_2] = letter_2, letter_1
    var = var+1

string = ''.join(slist)

print(slist)
print(string)

Output: Output:

['C', 'D', 'E', 'F', 'A', 'B', 'G']
CDEFABG

But before I came out to this I tried first to implement string.index() instead of slist.index() :但在我出来之前,我首先尝试实现string.index()而不是slist.index()

#assigned a letter to a variable
letter_1, letter_2 = string[var], string[var+2]
#assigned a letter index to a variable
l_1, l_2 = string.index(letter_1), string.index(letter_2)

And after 2 times of looping while var < 4 it started to execute different outputs in contrast to the right solution with list.index() and gave the outputs like this:while var < 4循环 2 次后,它开始执行不同的输出,与使用list.index()的正确解决方案形成对比,并给出如下输出:

['C', 'B', 'A', 'D', 'E', 'F', 'G']
CBADEFG

['C', 'D', 'A', 'B', 'E', 'F', 'G']
CDABEFG

['C', 'D', 'E', 'B', 'C', 'F', 'G']
CDEBCFG #the right one is CDEBAFG

['C', 'D', 'E', 'F', 'C', 'D', 'G']
CDEFCDG #the right one is CDEFABG

Is it possible to use str.index() method instead of list.index() to solve this problem and what is the difference between them both after 2 times of the execution of while-loop in both cases (as shown below)?是否可以使用str.index()方法而不是list.index()来解决这个问题,在两种情况下执行 2 次 while-loop 后它们有什么区别(如下所示)?

During the code, I was working on a task that looks like this:在编写代码期间,我正在处理如下所示的任务:

string = 'ABCDEFG'

  1. Assign 0 to the variable var .0分配给变量var
  2. Add 1 to var .1添加到var
  3. Swap the letter at position var with the letter at position var + 2 .将 position var处的字母与 position var + 2处的字母交换。
  4. if var < 4 go to Step 2. if var < 4 go 到步骤 2。
  5. End.结尾。

The main question of the task: What will be the string at Step 5?任务的主要问题:第 5 步的字符串是什么?

The right output should be CDEFABG右边的output应该是CDEFABG

After some time of thinking about it I came with the right solution using slist.index() :经过一段时间的思考,我使用slist.index()提出了正确的解决方案:

string = 'ABCDEFG'
slist = list(string)
var = 0

while var < 4:
    #assigned a letter to a variable
    letter_1, letter_2 = slist[var], slist[var+2]
    #assigned a letter index to a variable
    l_1, l_2 = slist.index(letter_1), slist.index(letter_2)
    #swaped two letters with each other
    slist[l_1], slist[l_2] = letter_2, letter_1
    var = var+1

string = ''.join(slist)

print(slist)
print(string)

Output: Output:

['C', 'D', 'E', 'F', 'A', 'B', 'G']
CDEFABG

But before I came out to this I tried first to implement string.index() instead of slist.index() :但在我出来之前,我首先尝试实现string.index()而不是slist.index()

#assigned a letter to a variable
letter_1, letter_2 = string[var], string[var+2]
#assigned a letter index to a variable
l_1, l_2 = string.index(letter_1), string.index(letter_2)

And after 2 times of looping while var < 4 it started to execute different outputs in contrast to the right solution with list.index() and gave the outputs like this:while var < 4循环 2 次后,它开始执行不同的输出,与使用list.index()的正确解决方案形成对比,并给出如下输出:

['C', 'B', 'A', 'D', 'E', 'F', 'G']
CBADEFG

['C', 'D', 'A', 'B', 'E', 'F', 'G']
CDABEFG

['C', 'D', 'E', 'B', 'C', 'F', 'G']
CDEBCFG #the right one is CDEBAFG

['C', 'D', 'E', 'F', 'C', 'D', 'G']
CDEFCDG #the right one is CDEFABG

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM