打印generator数组(列表)中的内容(python3)
循环中不适用数组定义封装而直接用函数调用,(…)会使用元组,则会出现generator对象
def sentence_to_id(self, sentence):
word_ids = (self.word_to_id(cur_word) for cur_word in sentence.split())
return word_ids
如果直接打印generator对象的话,会出现类似
<generator object Vocab.sentence_to_id.. at 0x00000155F4948CA8>
试试使用print(word_ids[0]);
则会出现TypeError: ‘generator’ object is not subscriptable
最后,将此对象转换成list列表
def sentence_to_id(self, sentence):
word_ids = (self.word_to_id(cur_word) for cur_word in sentence.split())
word_ids = list(word_ids)
return word_ids
成功~~~
博客主要围绕Python3展开,讲述打印generator数组(列表)内容时遇到的问题。若在循环中不使用数组定义封装,直接用函数调用会出现generator对象,直接打印该对象或使用特定操作会报错,最后将对象转换成list列表可成功打印。
4583

被折叠的 条评论
为什么被折叠?



