yield

Recursive function with yield doesn't return anything

与世无争的帅哥 提交于 2019-12-08 07:35:19
问题 I am trying to create a generator for permutation purpose. I know there are other ways to do that in Python but this is for something else. Unfortunately, I am not able to yield the values. Can you help? def perm(s,p=0,ii=0): l=len(s) s=list(s) if(l==1): print ''.join(s) elif((l-p)==2): yield ''.join(s) yield ''.join([''.join(s[:-2]),s[-1],s[-2]]) else: for i in range(p,l): tmp=s[p] s[p]=s[i] s[i]=tmp perm(s,p+1,ii) 回答1: Your line perm(s,p+1,ii) doesn't do anything, really: it's just like

Python自动化运维之高级函数

∥☆過路亽.° 提交于 2019-12-07 23:34:35
本帖最后由 陈泽 于 2018-6-20 17:31 编辑 一、协程 1.1协程的概念 协程,又称微线程,纤程。英文名Coroutine。一句话说明什么是线程:协程是一种用户态的轻量级线程。(其实并没有说明白~) 那么这么来理解协程比较容易:   线程是系统级别的,它们是由操作系统调度;协程是程序级别的,由程序员根据需要自己调度。我们把一个线程中的一个个函数叫做子程序,那么子程序在执行过程中可以中断去执行别的子程序;别的子程序也可以中断回来继续执行之前的子程序,这就是协程。也就是说同一线程下的一段代码执行着执行着就可以中断,然后跳去执行另一段代码,当再次回来执行代码块的时候,接着从之前中断的地方开始执行。 比较专业的理解是:   协程拥有自己的寄存器上下文和栈。协程调度切换时,将寄存器上下文和栈保存到其他地方,在切回来的时候,恢复先前保存的寄存器上下文和栈。因此:协程能保留上一次调用时的状态(即所有局部状态的一个特定组合),每次过程重入时,就相当于进入上一次调用的状态,换种说法:进入上一次离开时所处逻辑流的位置。 1.2 协程的优缺点 协程的优点:   (1)无需线程上下文切换的开销,协程避免了无意义的调度,由此可以提高性能(但也因此,程序员必须自己承担调度的责任,同时,协程也失去了标准线程使用多CPU的能力)   (2)无需原子操作锁定及同步的开销   (3)方便切换控制流

Java 复习 —— 线程相关方法介绍

大城市里の小女人 提交于 2019-12-07 15:23:52
1、sleep() 使当前线程(即调用该方法的线程)暂停执行一段时间,让其他线程有机会继续执行,但它并不释放对象锁(如果是在Synchronized块中的话, 他是不会释放锁的 )。也就是说如果有synchronized同步块,其他线程仍然不能访问共享数据。注意该方法是个静态方法,但是系统会自动识别具体哪个线程调用了休眠时间。 例如有两个线程同时执行(没有synchronized)一个线程优先级为MAX_PRIORITY,另一个为MIN_PRIORITY,如果没有Sleep()方法,只有高优先级的线程执行完毕后,低优先级的线程才能够执行;但是高优先级的线程sleep(500)后,低优先级就有机会执行了。 总之,sleep()可以使低优先级的线程得到执行的机会,当然也可以让同优先级、高优先级的线程有执行的机会。 注意:我们都知道,如果所有子线程都还没有执行完毕,那么主线程(main)是不会退出的;当然如果这些子线程都是守护线程的话,也就是在线程start之前设置为setDaemon(true);这个时候如果主线程运行完了,那么子线程也就会跟着消亡。 在做单元测试的时候,我发现就算我的所有子线程都是非守护线程,但是在子线程进入之后我就让他睡眠1秒钟,结果发现,主线程运行完之后,子线程也都退出了,除非我在主线程中睡得更久,程序才会继续执行。 2、join() join()方法

【探秘ES6】系列专栏(十):更深入了解生成器

风流意气都作罢 提交于 2019-12-07 13:42:21
ES6作为新一代JavaScript标准,已正式与广大前端开发者见面。为了让大家对ES6的诸多新特性有更深入的了解,Mozilla Web开发者博客推出了《 ES6 In Depth 》系列文章。CSDN已获授权,将持续对该系列进行翻译,组织成 【探秘ES6】系列专栏 ,供大家学习借鉴。本文为该系列的第十篇。 本文接下来继续讲述有关生成器的更多特性。 回顾 在 上一篇文章 中,主要介绍了有关生成器(Generators)的基本用法。生成器函数与常规函数类似,其主要区别是生成器函数体不会一次全部运行。它会按部分来执行,当碰到yield表达式的时候会暂停执行。 例如: function* someWords() { yield "hello"; yield "world"; } for (var word of someWords()) { alert(word); } 接入来就生成器用法作进一步介绍。 如何关闭生成器 生成器还有几个特性需要介绍下: generator.return() the optional argument to generator.next() generator.throw(error) yield* 先看一个常规的cleanup程序: function doThings() { setup(); try { // ... do some things ..

Scala 特殊的对象和关键字

我的未来我决定 提交于 2019-12-07 11:13:21
Option : 标准类库中的Option类型用样例类来表示那种可能存在、也可能不存在的值。 Option 有两个子类别,一个是 Some,一个是 None,当他回传 Some 的时候,代表这个函式成功地给了你一个 String,而你可以透过 get() 这个函式拿到那个 String,如果他返回的是 None,则代表没有字符串可以给你。 当然,在返回 None,也就是没有 String 给你的时候,如果你还硬要调用 get() 来取得 String 的话,Scala 一样是会报告一个 Exception 给你的 因为 Option[T] 除了 get() 之外,也提供了另一个叫 getOrElse() 的函式,这个函式正如其名--如果 Option 里有东西就拿出来,不然就给个默认值。 参考url implicit (隐式转换): *以implicit关键字声明的带有单个参数的函数。*implicit conversion function ,这样的函数将被自动应用,将值从一种类型转换为另一种类型。 【 example】 我们想把整数n转换成分数n/1. <!-- lang: scala --> implicit def int2Fraction (n: Int) = Fraction(n, 1) 这样就可以用如下表达式求值: <!-- lang: scala --> val

C#: How to translate the Yield Keyword

女生的网名这么多〃 提交于 2019-12-07 08:39:32
问题 What would the MSDN sample look like without the yield keyword? You may use any example if you perfer. I would just like to understand what is going on under the hood. Is the yield operator eagerly or lazily evaluated? Sample: using System; using System.Collections; public class List { public static IEnumerable Power(int number, int exponent) { int counter = 0; int result = 1; while (counter++ < exponent) { result = result * number; yield return result; } } static void Main() { // Display

Workaround for python 2.4's yield not allowed in try block with finally clause

陌路散爱 提交于 2019-12-07 07:58:44
问题 I'm stuck on python2.4, so I can't use a finally clause with generators or yield . Is there any way to work around this? I can't find any mentions of how to work around this limitation in python 2.4, and I'm not a big fan of the workarounds I've thought of (mainly involving __del__ and trying to make sure it runs within a reasonable time) aren't very appealing. 回答1: You can duplicate code to avoid the finally block: try: yield 42 finally: do_something() Becomes: try: yield 42 except: # bare

How does 'yield' work in tornado when making an asynchronous call?

穿精又带淫゛_ 提交于 2019-12-07 06:53:50
问题 Recently, I was learning Introduction to Tornado , and I came across the following code: class IndexHandler(tornado.web.RequestHandler): @tornado.web.asynchronous @tornado.gen.engine def get(self): query = self.get_argument('q') client = tornado.httpclient.AsyncHTTPClient() response = yield tornado.gen.Task(client.fetch, "http://search.twitter.com/search.json?" + \ urllib.urlencode({"q": query, "result_type": "recent", "rpp": 100})) body = json.loads(response.body) [...omitted the following

Laravel 4 syntax error out-of-the-box

一世执手 提交于 2019-12-07 03:34:04
问题 I just installed Laravel 4 (Illuminate) and as I opened the index.php file in a browser, I was met with this error: Parse error: syntax error, unexpected 'yield' (T_YIELD), expecting identifier (T_STRING) in /www/Laravel4/vendor/illuminate/view/src/Illuminate/View/Environment.php on line 339 I have fixed the permissions for the meta folder, and installed all the dependencies through Composer. I am running PHP version 5.5.0alpha2 on OSX 10.8.2. 回答1: That's because yield became a language

TypeError: 'generator' object has no attribute '__getitem__'

谁说我不能喝 提交于 2019-12-06 19:12:33
问题 I have written a generating function that should return a dictionary. however when I try to print a field I get the following error print row2['SearchDate'] TypeError: 'generator' object has no attribute '__getitem__' This is my code from csv import DictReader import pandas as pd import numpy as np def genSearch(SearchInfo): for row2 in DictReader(open(SearchInfo)): yield row2 train = 'minitrain.csv' SearchInfo = 'SearchInfo.csv' row2 = {'SearchID': -1} for row1 in DictReader(open(train)): if