Python 使用列表比较长度不等的字符串

Python 使用列表比较长度不等的字符串,python,Python,我有一个字符串列表,我试图使用 如果下一个字符是单个字符!=单个字符:并输出给定索引中不相同的字符,但到目前为止,我一直收到索引超出范围的错误消息,我似乎无法正确输出较大或较小字符串的长度,也无法正确报告哪个实际字符串比另一个字符串大。注释掉的行是用来显示哪个更大,哪个更小的行,还有一些行是用来测试输出的。最后,如果一个字符串比另一个字符串长,我想不出如何显示剩余的字符,比如s1=red和s2=redford我将如何显示print(f{s1}比{s2}短,{n}和{s2}仍然有剩余的字符o、r、

我有一个字符串列表,我试图使用
如果下一个字符是单个字符!=单个字符:
并输出给定索引中不相同的字符,但到目前为止,我一直收到
索引超出范围的错误消息,我似乎无法正确输出较大或较小字符串的长度,也无法正确报告哪个实际字符串比另一个字符串大。注释掉的行是用来显示哪个更大,哪个更小的行,还有一些行是用来测试输出的。最后,如果一个字符串比另一个字符串长,我想不出如何显示剩余的字符,比如s1=
red
和s2=
redford
我将如何显示
print(f{s1}比{s2}短,{n}和{s2}仍然有剩余的字符o、r、d

一般来说,我对发布和编码还不熟悉,所以非常感谢反馈,特别是如果有一些我以前没有见过的新的有用的方法/关键字的话


array = "red redford grimbolt".split()  

print(array)

for string in range( len(array) ) :
    print()

    print(f"we are at iteration number : {string+1}") #tests output for string
    print()

    individual_string = array[string]
    print()

    print(f"indivudal_string is '{individual_string} '")
    print()

    print(f"the length of the string '{individual_string}' is '{len(individual_string) }'")
    print()

    NEXT_individual_string = array[string +1]
  # smaller_string = min[ NEXT_individual_string , individual_string]

  # 
  # bigger_string = max[ individual_string , NEXT_individual_string]
  #

  # print(f"indivudal_string is '{individual_string}' and NEXT_individual_string is '{NEXT_individual_string}'  " ) # test output
  # print()
  # print(f"bigger_string = '{bigger_string}' and smaller string =  '{smaller_string}'  ") # test output of bigger_string and smaller_string

  # small_len = len(smaller_string)
  #
  # big_len  = len(bigger_string)
  #
  # print()
  #
  # print(f"small len is {small_len} and big len is {big_len}")
  # print()

    for char in range( len(individual_string)  ):

        individual_CHAR = individual_string[char]
        NEXT_individual_CHAR = NEXT_individual_string[char]

        #if small_len > big_len:
        #    print(f"small len is not smaller than big len...small_len right now = {small_len}  and big_len right now is {big_len}")



        if NEXT_individual_CHAR != individual_CHAR:

            print(f" '{individual_CHAR}' at position {char+1} in the string '{individual_string}' is the not same '{NEXT_individual_CHAR}' at position {char+1} in the string '{NEXT_individual_string}'   ")





注释掉这些行后,我得到以下输出:


we are at iteration number : 1


indivudal_string is 'red '

the length of the string 'red' is '3'


we are at iteration number : 2


indivudal_string is 'redford '

the length of the string 'redford' is '7'

 'r' at position 1 in the string 'redford' is the not same 'g' at position 1 in the string 'grimbolt'   
 'e' at position 2 in the string 'redford' is the not same 'r' at position 2 in the string 'grimbolt'   
 'd' at position 3 in the string 'redford' is the not same 'i' at position 3 in the string 'grimbolt'   
 'f' at position 4 in the string 'redford' is the not same 'm' at position 4 in the string 'grimbolt'   
 'o' at position 5 in the string 'redford' is the not same 'b' at position 5 in the string 'grimbolt'   
 'r' at position 6 in the string 'redford' is the not same 'o' at position 6 in the string 'grimbolt'   
 'd' at position 7 in the string 'redford' is the not same 'l' at position 7 in the string 'grimbolt'   

we are at iteration number : 3


indivudal_string is 'grimbolt '

the length of the string 'grimbolt' is '8'



```Traceback (most recent call last):
  File "
/Library/Preferences/PyCharmEdu2019.2/scratches/Comparing an Array of Strings.py", line 25, in <module>
    NEXT_individual_string = array[string +1]
IndexError: list index out of range

Process finished with exit code 1```


but without those lines commented out I get :


Traceback (most recent call last):
  File "/Users//Library/Preferences/PyCharmEdu2019.2/scratches/Comparing an Array of Strings.py", line 26, in <module>
    smaller_string = min[ NEXT_individual_string , individual_string]
TypeError: 'builtin_function_or_method' object is not subscriptable
['red', 'redford', 'grimbolt']

we are at iteration number : 1


indivudal_string is 'red '

the length of the string 'red' is '3'


Process finished with exit code 1 ``` 

我们的迭代次数是:1
indivudal_字符串为“红色”
字符串“red”的长度为“3”
我们的迭代次数是:2
indivudal_字符串为“redford”
字符串“redford”的长度为“7”
字符串“redford”中位置1处的“r”与字符串“grimbolt”中位置1处的“g”不同
字符串“redford”中位置2处的“e”与字符串“grimbolt”中位置2处的“r”不同
字符串“redford”中位置3处的“d”与字符串“grimbolt”中位置3处的“i”不同
字符串“redford”中位置4处的“f”与字符串“grimbolt”中位置4处的“m”不同
字符串“redford”中位置5处的“o”与字符串“grimbolt”中位置5处的“b”不同
字符串“redford”中位置6处的“r”与字符串“grimbolt”中位置6处的“o”不同
字符串“redford”中位置7处的“d”与字符串“grimbolt”中位置7处的“l”不同
我们的迭代次数是:3
独立字符串为“grimbolt”
字符串“grimbolt”的长度为“8”
```回溯(最近一次呼叫最后一次):
文件“
/Library/Preferences/PyCharmEdu2019.2/scratches/Comparing a Array of Strings.py”,第25行,在
下一个\u单个\u字符串=数组[字符串+1]
索引器:列表索引超出范围
进程已完成,退出代码为1```
但如果没有这些话,我会得到:
回溯(最近一次呼叫最后一次):
文件“/Users//Library/Preferences/PyCharmEdu2019.2/scratch/Comparing a Array of Strings.py”,第26行,在
较小的字符串=最小值[下一个单个字符串,单个字符串]
TypeError:“内置函数”或“方法”对象不可下标
['red'、'redford'、'grimbolt']
我们的迭代次数是:1
indivudal_字符串为“红色”
字符串“red”的长度为“3”
进程已完成,退出代码为1``

下面是一个非常详细的方法来完成您的任务(如下)

要完成您自己的解决方案,需要阅读的主要内容 是Try/Except块和列表索引 第二个选项允许您测试索引器错误(或其他错误),如果遇到错误,只需继续。第二个选项允许您检查列表中的所有元素,除了单个元素(在本例中为单词)。例如,如果我的列表是[“我”、“爱”、“巧克力”],并且您希望查看“爱”之前和之后的所有内容,而不是“爱”就其本身而言,如果“爱”的索引为1,则可以选择checklist[:list.index(“爱”)]和checklist[list.index(“爱”)+1:] 每个代码块中的第一个列表是列表名称的占位符。括号表示列表。my_list=[](空列表)。my_list[:]表示列表中的所有内容。my_list[1:]表示从元素1开始的列表中的所有内容(列表的第一个索引为0)。my_list[:7]表示直到但不包括第7个元素的所有内容。对于“love”示例,第一个my_列表[filler]包含所有内容,但不包括“love”元素,下一个my_列表[filler2]表示列表中以“love”开头的所有内容。希望其中一些内容有所帮助。如果有问题,请向我提问

一般做法如下:

for each word in word list
     check every word in the word list before current word
     use shorter of the two words to know when to stop checking
     print differences and information

     check every word in the word list after current word
     use shorter of the two words to know when to stop checking
     print differences and information
样本输出:

We are at iteration 0, and the word is red
r at pos 0 in red != g at pos 0 in green
e at pos 1 in red != r at pos 1 in green
d at pos 2 in red != e at pos 2 in green
red is shorter than green by 2
The leftovers of green are ['e', 'n']
r at pos 0 in red != b at pos 0 in blue
e at pos 1 in red != l at pos 1 in blue
d at pos 2 in red != u at pos 2 in blue
red is shorter than blue by 1
The leftovers of blue are ['e']
We are at iteration 1, and the word is green
g at pos 0 in green != r at pos 0 in red
r at pos 1 in green != e at pos 1 in red
e at pos 2 in green != d at pos 2 in red
green is longer than red by 2
The leftovers of green are ['e', 'n']
g at pos 0 in green != b at pos 0 in blue
r at pos 1 in green != l at pos 1 in blue
e at pos 2 in green != u at pos 2 in blue
e at pos 3 in green == e at pos 3 in blue
We are at iteration 2, and the word is blue
b at pos 0 in blue != r at pos 0 in red
l at pos 1 in blue != e at pos 1 in red
u at pos 2 in blue != d at pos 2 in red
blue is longer than red by 1
The leftovers of blue are ['e']
b at pos 0 in blue != g at pos 0 in green
l at pos 1 in blue != r at pos 1 in green
u at pos 2 in blue != e at pos 2 in green
e at pos 3 in blue == e at pos 3 in green
blue is shorter than green by 1
The leftovers of green are ['n']

这绝对是有帮助的,因为它完成了工作,但也为我打开了学习的新天地,你当然不必教我所有的东西,但我很好奇如何
[I for I in other_str[len(one_string):]]
有效,即输出中循环的格式化,以及使用``最短=''''``的点,您可以使用``最短=len(其他字符串)或最短=len(一个字符串)``酷,`[i代表范围(10)]内的i]等将其转换为整数(对于循环和列表括号内的其他内容),称为列表理解。将其想象为在列表中展开自身的for循环,最终结果是包含所有值的列表。例如,my_list=[i for i in range(10)]是[0,1,2,3,4,5,6,7,8,9],是对范围(10)中的i执行my_list=[](换行)的较短方法:(换行)my_list.append(i)但是我的清单=清单(范围(10))的一个更长的方法其他例子:我的清单=[i^2代表范围(5)]=[0,1,4,9,16],我的清单=[i^2代表范围(5)中的我,如果我%2==0]=[0,4,16]。对于行:shortest='',我只是用它作为变量shortest的占位符。我可以写,shortest=0,shortest=[],等等。这并不重要,因为当我将其分配给len(一个字符串)或len(另一个字符串)时,我会立即更新值,并随后更新最短的类型。
We are at iteration 0, and the word is red
r at pos 0 in red != g at pos 0 in green
e at pos 1 in red != r at pos 1 in green
d at pos 2 in red != e at pos 2 in green
red is shorter than green by 2
The leftovers of green are ['e', 'n']
r at pos 0 in red != b at pos 0 in blue
e at pos 1 in red != l at pos 1 in blue
d at pos 2 in red != u at pos 2 in blue
red is shorter than blue by 1
The leftovers of blue are ['e']
We are at iteration 1, and the word is green
g at pos 0 in green != r at pos 0 in red
r at pos 1 in green != e at pos 1 in red
e at pos 2 in green != d at pos 2 in red
green is longer than red by 2
The leftovers of green are ['e', 'n']
g at pos 0 in green != b at pos 0 in blue
r at pos 1 in green != l at pos 1 in blue
e at pos 2 in green != u at pos 2 in blue
e at pos 3 in green == e at pos 3 in blue
We are at iteration 2, and the word is blue
b at pos 0 in blue != r at pos 0 in red
l at pos 1 in blue != e at pos 1 in red
u at pos 2 in blue != d at pos 2 in red
blue is longer than red by 1
The leftovers of blue are ['e']
b at pos 0 in blue != g at pos 0 in green
l at pos 1 in blue != r at pos 1 in green
u at pos 2 in blue != e at pos 2 in green
e at pos 3 in blue == e at pos 3 in green
blue is shorter than green by 1
The leftovers of green are ['n']