- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个字符串列表,我尝试使用以下方法迭代列表中每个字符串的字符if NEXT_individual_CHAR != individual_CHAR:
并输出给定索引处不相同的字符,但到目前为止我不断收到 Index Out of Bounds
错误消息,我可以似乎无法正确输出较大或较小字符串的长度,或正确报告哪个实际字符串比另一个字符串大。注释掉的行是应该显示哪一行更大、哪一行更小的行,还有几行只是为了测试输出。最后,我不知道如果一个字符串比另一个字符串长,如何显示剩余的字符,即 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 ```
最佳答案
这是完成任务的非常详细的方法(如下)。
完成您自己的解决方案时需要阅读的主要内容是 Try/Except block 和列表索引。其中前者两个将允许您测试 IndexError (或其他错误),如果遇到一个错误,只需继续。后者将允许您检查列表中的所有元素,除了单个元素(在本例中是一个单词)。例如,如果我的列表是[“I”,“love”,“chocolate”],并且您想查看“love”之前和之后的所有内容,但不查看“love”本身,那么您可以,因为“love”的索引"为 1,检查 list[:list.index("love")] 并检查 list[list.index("love") + 1:]。这每个代码块中的第一个列表是列表名称的占位符。括号表示列表。 my_list = [](空列表)。 my_list[:] 表示列表中的所有内容。 my_list[1:] 表示列表中从元素 1 开始的所有内容(列表的第一个索引为 0)。 my_list[:7] 表示直到但不包括第 7 个元素的所有内容。对于“love”示例,第一个 my_list[filler] 获取直到但不包括“love”元素的所有内容,下一个 my_list[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
array = "red green blue".split()
for string_index in range(len(array)):
one_string = array[string_index]
shortest = ''
print(f"We are at iteration {string_index}, and the word is {array[string_index]}")
for other_str in array[:string_index]:
if len(one_string) > len(other_str):
shortest = len(other_str)
else:
shortest = len(one_string)
try:
for char_index in range(shortest):
if one_string[char_index] != other_str[char_index]:
print(f"{one_string[char_index]} at pos {char_index} in {one_string} != \
{other_str[char_index]} at pos {char_index} in {other_str}")
else:
print(f"{one_string[char_index]} at pos {char_index} in {one_string} == \
{other_str[char_index]} at pos {char_index} in {other_str}")
except IndexError as error:
continue
if shortest == len(other_str):
print(f"{one_string} is longer than {other_str} by {len(one_string) - len(other_str)}")
print(f"The leftovers of {one_string} are {[i for i in one_string[len(other_str):]]}")
if len(one_string) == shortest:
print(f"{one_string} is shorter than {other_str} by {len(other_str) - len(one_string)}")
print(f"The leftovers of {other_str} are {[i for i in other_str[len(one_string):]]}")
for other_str in array[string_index+1:]:
if len(one_string) > len(other_str):
shortest = len(other_str)
else:
shortest = len(one_string)
try:
for char_index in range(len(one_string)):
if one_string[char_index] != other_str[char_index]:
print(f"{one_string[char_index]} at pos {char_index} in {one_string} != \
{other_str[char_index]} at pos {char_index} in {other_str}")
else:
print(f"{one_string[char_index]} at pos {char_index} in {one_string} == \
{other_str[char_index]} at pos {char_index} in {other_str}")
except IndexError as error:
continue
if shortest == len(other_str):
print(f"{one_string} is longer than {other_str} by {len(one_string) - len(other_str)}")
print(f"The leftovers of {one_string} are {[i for i in one_string[len(other_str):]]}")
if len(one_string) == shortest:
print(f"{one_string} is shorter than {other_str} by {len(other_str) - len(one_string)}")
print(f"The leftovers of {other_str} are {[i for i in other_str[len(one_string):]]}")
示例输出:
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']
关于python - 使用列表来比较不等长度的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58583423/
谁能解释为什么这些 JavaScript 数组不等式比较的计算结果为真? [""] !== [""] [1] !== [1] [] !== [] [""] != [""] [1] != [1] []
好的,所以我一直在努力学习掌握子进程并正确地等待它们完成。我已经阅读了很多 Stack Overflow Q/A,但我似乎仍然无法按照我的意愿让它工作。我一直在阅读/搜索这本书(C++ Primer
根据this , !==! 是不等于字符串运算符。尝试一下,我得到: C:\> if "asdf" !==! "fdas" echo asdf !==! was unexpected at this
这是一道面试题: Suppose: I have 100 trillion elements, each of them has size from 1 byte to 1 trillion byte
如何集成功能 f(y) w.r.t 时间;即 'y'是一个包含 3000 个值和值 time(t) 的数组从 1 到 3000 不等。所以,在整合 f(y) 后我需要 3000 个值. 积分将是不确定
我是一名优秀的程序员,十分优秀!