SyntaxError:Python inline for循环的语法无效

2024-06-16 14:46:59 发布

您现在位置:Python中文网/ 问答频道 /正文

wordnum = [14, 1, 7, 0, 0, 11]
sentNum = [4, 2, 8, 6, 5, 8]
findtext = [u'I', u'our', u'it', u'The villa', u'It', u'the large main pool']
sentss2 = [
    ['When', 'planning', 'our', 'return', 'trip', 'to', 'Kauai', ',', 'husband', 'and', 'I', 'were', 'happy', 'to', 'read', 'that', 'all', 'the', 'renovations', 'to', 'the', 'Koloa', 'Landing', 'Resort', 'were', 'completed', '.'],
    ['We', 'then', 'went', 'ahead', 'and', 'booked', 'a', 'deluxe', 'studio', 'for', 'mid-October', '.'],
    ['Upon', 'our', 'check', 'in', 'after', 'a', 'long', 'flight', 'and', 'dark', 'rainy', 'drive', 'from', 'the', 'airport', ',', 'we', 'were', 'greeted', 'warmly', 'at', 'the', 'front', 'desk', '.'],
    ['While', 'we', 'are', 'platinum', 'status', ',', 'we', 'were', "n't", 'expecting', 'a', 'room', 'upgrade', ',', 'but', 'much', 'to', 'our', 'delight', ',', 'was', 'advised', 'by', 'the', 'clerk', 'that', 'we', 'received', 'one', '.'],
    ['However', ',', 'it', 'was', "n't", 'until', 'we', 'got', 'to', 'our', 'room', ',', 'or', 'should', 'I', 'say', 'villa', ',', 'how', 'nice', 'the', 'upgrade', 'was', '.'],
    ['It', 'was', 'a', 'one', 'bedroom', ',', '1', '1/2', 'bath', ',', 'almost', '1000', 'square', 'feet', 'corner', 'villa', 'with', '2', 'full', 'walls', 'of', 'windows', 'in', 'the', 'lovely', 'living', 'room', 'alone', '.'],
    ['The', 'villa', 'was', 'beautiful', '.'],
    ['Our', 'view', 'of', 'the', 'smaller', 'family', 'pool', 'and', 'soccer', 'field', 'was', 'so', 'nice', '.'],
    ['Really', 'enjoyed', 'being', 'in', 'this', 'area', 'as', 'it', "'s", 'quieter', 'than', 'the', 'large', 'main', 'pool', '.']
]

我有一个for循环来分配值,并将sentss2中的现有值替换为大小写为findtext的第一个值。我写了一个for循环来完成这项工作,它非常完美:

^{pr2}$

现在我想在单个内联语句中编写它,以便更快地执行这个耗时的for循环。如果我写内联循环如下:

[(sentss2[aa][cc]=findtext[0],findtext[0].lower())[0] for aa,cc in zip(sentNum,wordnum)]

我得到一个无效的语法错误:

 File "<ipython-input-142-82f373e08c0a>", line 1
    [(sentss2[aa][cc]=findtext[0],findtext[0].lower())[0] for aa,cc in zip(sentNum,wordnum)]
                     ^
SyntaxError: invalid syntax

有什么帮助我如何写这个内联for循环?在


Tags: andthetoinforouraacc