Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 下面提到的语法是什么意思?_Python - Fatal编程技术网

Python 下面提到的语法是什么意思?

Python 下面提到的语法是什么意思?,python,Python,我正在开发著名的hamlet机器人程序,以使用Python 3.7。所以我有一部分剧本(以字符串输入的形式)来自著名的莎士比亚哈姆雷特戏剧 我的任务是将脚本中的句子分成列表,然后进一步创建句子中的单词列表 我正在使用从internet复制的以下代码: ''' ''' 在这里,我想了解最后两行代码的含义 if hamsplits[-1][-1][-1] == '.': hamsplits[-1][-1] = hamsplits[-1][-1][:-1] # Remove trail

我正在开发著名的hamlet机器人程序,以使用Python 3.7。所以我有一部分剧本(以字符串输入的形式)来自著名的莎士比亚哈姆雷特戏剧

我的任务是将脚本中的句子分成列表,然后进一步创建句子中的单词列表

我正在使用从internet复制的以下代码:

'''

'''

在这里,我想了解最后两行代码的含义

if hamsplits[-1][-1][-1] == '.':
        hamsplits[-1][-1] = hamsplits[-1][-1][:-1] # Remove trailing punctuation 

如果有人能在这方面帮助我???

让我们假设
hamsplits
是一个3D阵列

第一行检查最后一个平面的最后一行中的最后一个元素是否为点,然后从最后一行中删除最后一个元素

>>> x = [1, 2, 3]
>>> x = x[:-1] # Remove last element
>>> x
[1, 2]
应该有同样的效果

del hamsplits[-1][-1][-1]

让我们举一个例子,假设我们有像

hamsplits=['test',['test1',['test2','.']]]
print(hamsplits[-1][-1][-1])  # it would be equal to '.' 
if hamsplits[-1][-1][-1] == '.':  # here we are comparing it with "."
       hamsplits[-1][-1] = hamsplits[-1][-1][:-1] # in this we are just removing the '.' from third list in hamsplits and taking all remaining elements
print(hamsplits[-1][-1][:-1]) # it would print ['test2'] (removing last element from list) and overwriting in hamsplits[-1][-1]

**Note**:
hamsplits[:-1] is removing the last element, it's a slicing in python
hamsplits[-1] you are accessing the last element

希望这有帮助

什么让你困惑?
-1
?它只是获取列表中的最后一个索引可能是的副本,所以基本上[:-1]是在删除最后一个元素吗?对吗?@AmarSharma没有删除而是“创建”没有最后一个元素的新列表
hamsplits=['test',['test1',['test2','.']]]
print(hamsplits[-1][-1][-1])  # it would be equal to '.' 
if hamsplits[-1][-1][-1] == '.':  # here we are comparing it with "."
       hamsplits[-1][-1] = hamsplits[-1][-1][:-1] # in this we are just removing the '.' from third list in hamsplits and taking all remaining elements
print(hamsplits[-1][-1][:-1]) # it would print ['test2'] (removing last element from list) and overwriting in hamsplits[-1][-1]

**Note**:
hamsplits[:-1] is removing the last element, it's a slicing in python
hamsplits[-1] you are accessing the last element