下面是混淆了我(下文解釋)的部分代碼:爲什麼這個Python代碼返回一個生成器對象?
class Node:
def __init__(self, start, end):
self.start = start
self.end = end
intervals = [Node(1, 2), Node(3, 4), Node(5, 6)]
starts = sorted(i.start for i in intervals)
ends = sorted(i.end for i in intervals)
爲什麼(i.start for i in intervals)
回報發電機對象,爲什麼去掉括號像這樣i.start for i in intervals
返回無效語法錯誤?
你的問題是什麼? '(i.start for i in intervals)'是[gen-exp的語法](https://www.python.org/dev/peps/pep-0289/),因此它返回一個生成器和'i。我以間隔開始「不是有效的語法。 –
在你的代碼中,什麼是生成器? 'starts'或'ends'的值?或者'(i.start我間隔)'? – Quelklef