引用列表中的下一个项目

发布于 2025-01-15 20:19:58 字数 606 浏览 3 评论 0原文

我正在编写一个程序,要求我确定字符串列表是否是单词链。 词链是一个单词列表,其中单词的最后一个字母是最后一个单词的下一个字母的第一个字母。如果列表是单词链,程序将返回 True,否则返回 false。我的代码如下:

def is_word_chain(word_list):
    for i in word_list:
        if i[-1] == (i+1[0]):
            result = True
        else:
            result = False 
    return result

它返回以下错误:

SyntaxWarning: 'int' object is not subscriptable; perhaps you missed a comma?
  if i[-1] == (i+1[0]):
Traceback (most recent call last):
  if i[-1] == (i+1[0]):
TypeError: 'int' object is not subscriptable

我想知道如何正确引用列表中的下一项才能执行此函数。

I am working through a program that requires me to determine if a list of strings is a word chain. A word chain is a list of words in which the last letter of a word is the first letter of the next word in the last. The program is to return True if the list is a word chain, and false otherwise. My code is as follows:

def is_word_chain(word_list):
    for i in word_list:
        if i[-1] == (i+1[0]):
            result = True
        else:
            result = False 
    return result

and it returns the following error:

SyntaxWarning: 'int' object is not subscriptable; perhaps you missed a comma?
  if i[-1] == (i+1[0]):
Traceback (most recent call last):
  if i[-1] == (i+1[0]):
TypeError: 'int' object is not subscriptable

I am wondering how I would properly reference the next item in the list in order to execute this function.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

刘备忘录 2025-01-22 20:19:58

给定:

>>> li1=['word','chain','test','test']
>>> li2=['word','chain','test']

您可以使用 zip 获取一对单词进行测试:

>>> list(zip(li1,li1[1:]))
[('word', 'chain'), ('chain', 'test'), ('test', 'test')]

一旦获得,您可以返回 TrueFalse,如下所示:

>>> any(t[0][-1]==t[1][0] for t in zip(li1,li1[1:]))
True

>>> any(t[0][-1]==t[1][0] for t in zip(li2,li2[1:]))
False

Given:

>>> li1=['word','chain','test','test']
>>> li2=['word','chain','test']

You can use zip to get a pair of words to test:

>>> list(zip(li1,li1[1:]))
[('word', 'chain'), ('chain', 'test'), ('test', 'test')]

Once you have that, you can return True or False like so:

>>> any(t[0][-1]==t[1][0] for t in zip(li1,li1[1:]))
True

>>> any(t[0][-1]==t[1][0] for t in zip(li2,li2[1:]))
False
绮筵 2025-01-22 20:19:58

修好了:)

def is_word_chain(word_list):
    for i in range(len(word_list)-1):
        if word_list[i][-1] == word_list[i+1][0]:
            result = True
        else:
            result = False 
            break
    return result

print(is_word_chain(['apple', 'egg', 'grapes', 'sand']))
print(is_word_chain(['apple', 'egg', 'sand', 'grapes']))

Fixed it :)

def is_word_chain(word_list):
    for i in range(len(word_list)-1):
        if word_list[i][-1] == word_list[i+1][0]:
            result = True
        else:
            result = False 
            break
    return result

print(is_word_chain(['apple', 'egg', 'grapes', 'sand']))
print(is_word_chain(['apple', 'egg', 'sand', 'grapes']))
深空失忆 2025-01-22 20:19:58
def is_word_chain(word_list):
for i in range(0,len(word_list)-1):
    if word_list[i][-1] == word_list[i+1][0]:
        result = True
    else:
        result = False 
return result

在您的代码中,您迭代字符串,并且在 if 条件下,您尝试使用该字符串作为列表,这在技术上是错误的。

使用 range 并将 word_chain 作为列表进行迭代。

def is_word_chain(word_list):
for i in range(0,len(word_list)-1):
    if word_list[i][-1] == word_list[i+1][0]:
        result = True
    else:
        result = False 
return result

in your code you iterate through strings and in if condition you are trying use that string as sort of list which is technically blunder here.

use range and iterate over word_chain as list.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文