yield

Python: return values from a loop without breaking out

笑着哭i 提交于 2019-12-01 04:44:23
问题 G'day, I have a list of individuals that are grouped by place. I want to produce a new variable that gives a number to each individual dependant on their place. What I would like my data to look like is: place individual here 1 here 2 here 3 there 1 there 2 somewhere 1 somewhere 2 I have written this: nest="ddd", "ddd", "fff", "fff", "fff", "fff", "qqq", "qqq" def individual(x): i = 0 j = 1 while i < len(x): if x[i] == x[i-1]: print(j+1) i = i + 1 j = j + 1 else: print(1) i = i + 1 j = 1

Tensorflow Dataset.from_generator fails with pyfunc exception

半腔热情 提交于 2019-12-01 04:44:20
问题 I am trying tensorflow's nightly 1.4 as I need Dataset.from_generator to stich together some variable length datasets. This simple code (idea from here): import tensorflow as tf Dataset = tf.contrib.data.Dataset it2 = Dataset.range(5).make_one_shot_iterator() def _dataset_generator(): while True: try: try: get_next = it2.get_next() yield get_next except tf.errors.OutOfRangeError: continue except tf.errors.OutOfRangeError: return # Dataset.from_generator need tensorflow > 1.3 ! das_dataset =

Python (yield): all paths from leaves to root in a tree

我的梦境 提交于 2019-12-01 03:20:33
问题 I want to generate all paths from every leaf to root in a tree. I'd like to do that with generators, to save memory (tree can be big). Here's my code: def paths(self, acc=[]): if self.is_leaf(): yield [self.node]+acc for child in self.children: child.paths([self.node]+acc) But it doesn't work. Why? Invoked at root, it traverses the tree from top to bottom, collecting nodes in "acc". "acc" should be returned in every leaf... is_leaf() is true if self.children is empty. 回答1: This code only

How to yield empty generator?

半城伤御伤魂 提交于 2019-12-01 02:58:03
I have a method which takes a generator plus some additional parameters and yields a new generator: function merge(\Generator $carry, array $additional) { foreach ( $carry as $item ) { yield $item; } foreach ( $additional as $item ) { yield $item; } } The usual use case for this function is similar to this: function source() { for ( $i = 0; $i < 3; $i++ ) { yield $i; } } foreach ( merge(source(), [4, 5]) as $item ) { var_dump($item); } But the problem is that sometimes I need to pass empty source to the merge method. Ideally I would like to be able to do something like this: merge(\Generator:

ES6——generator

╄→гoц情女王★ 提交于 2019-12-01 02:42:36
generator 生成器函数 普通函数,一路到底 generator函数,中间可以停,到哪停呢,用 yield 配合,交出执行权 yield 有 放弃、退让、退位的意思 需要调用next()方法启动执行,需要遇到 yield 停, 踹一脚走一步 generator函数前面加一个 * 两边可以有空格,或靠近函数或 function 背后实际生成多个小函数,实现走走停停 function show() { console.log('a') console.log('b') } show() // 普通函数 function *show2() { console.log('1') yield console.log('2') } let genObj = show2() genObj.next() // 1 genObj.next() // 2 genObj.next() // 最后了,没有结果 来源: https://www.cnblogs.com/sylys/p/11649004.html

Python generator to yield everything from another generator call

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-01 02:24:39
I have a Python generator that can call itself to get more elements to yield. It looks like this: def gen(list): # ... if list: for x in gen(list[1:]): yield x My question is about the last two lines: is there a more concise way to express this? I am hoping for something like this (understanding this isn't valid Python as-is): def gen(list): # ... if list: yield each in gen(list[1:]) Python 3.3 added the yield from keyword . Here's a comparison between what you currently have and code using the new keyword: yield_from_test.py : def gen_for(a_list): if a_list: yield a_list[0] for x in gen_for(a

Generator not working to split string by particular identifier . Python 2

亡梦爱人 提交于 2019-12-01 01:41:53
So far I have found a way to yield the name, string, and extra string. It works for the second one but does not work for the first one? it's so weird because the formats are really similar. is it because it's multiple lines? i thought the if line == '+': pass would bypass the issue. i added in the print '\n' to show the difference Input: @first_name AlongStringOfText ThatHasNoSpaces ButIsSeparatedByLineBreaks + {+iuhsfIUHSDFUi8849308989829 0990+-]@@@#*$()(@*$*)))***)@@**@#*u sdfiuhnknwuiewi + @second_name MoreTextThatCouldBeOnOneLine + +{~~~(@#UhuisdfiuhIUHDSFIUFHIl)}9823)88hafff #empty line

Python generator to yield everything from another generator call

喜欢而已 提交于 2019-11-30 21:57:15
问题 I have a Python generator that can call itself to get more elements to yield. It looks like this: def gen(list): # ... if list: for x in gen(list[1:]): yield x My question is about the last two lines: is there a more concise way to express this? I am hoping for something like this (understanding this isn't valid Python as-is): def gen(list): # ... if list: yield each in gen(list[1:]) 回答1: Python 3.3 added the yield from keyword. Here's a comparison between what you currently have and code

Python's PEP 484 type annotation for Generator Expression

孤街浪徒 提交于 2019-11-30 21:21:35
What is the correct type annotation for a function that returns a generator expression ? e.g.: def foo(): return (x*x for x in range(10)) I can't figure out if this is -> Iterator[int] , -> Iterable[int] , -> Generator[int, None, None] , or something else. If there should be one-- and preferably only one --obvious way to do it , then what is the obvious way here? All three forms mentioned by you in question are listed as valid alternatives in documentation , Generator expression simply creates a generator that only yields. Quote 1: A generator can be annotated by the generic type Generator

Generator not working to split string by particular identifier . Python 2

南笙酒味 提交于 2019-11-30 21:05:32
问题 So far I have found a way to yield the name, string, and extra string. It works for the second one but does not work for the first one? it's so weird because the formats are really similar. is it because it's multiple lines? i thought the if line == '+': pass would bypass the issue. i added in the print '\n' to show the difference Input: @first_name AlongStringOfText ThatHasNoSpaces ButIsSeparatedByLineBreaks + {+iuhsfIUHSDFUi8849308989829 0990+-]@@@#*$()(@*$*)))***)@@**@#*u sdfiuhnknwuiewi +