Tag: python

Python中的非赋值string如何在内存中有一个地址?

谁可以给我解释一下这个? 所以我一直在使用Python中的id()命令,碰到这个: >>> id('cat') 5181152 >>> a = 'cat' >>> b = 'cat' >>> id(a) 5181152 >>> id(b) 5181152 除了一个部分,这对我来说是有意义的:string'cat'在我将其分配给一个variables之前在内存中有一个地址。 我可能只是不明白如何内存寻址的作品,但有人可以解释这个给我,或者至less告诉我,我应该阅读内存寻址? 所以这一切都很好,但这使我更加困惑: >>> a = a[0:2]+'t' >>> a 'cat' >>> id(a) 39964224 >>> id('cat') 5181152 这让我感到奇怪,因为'猫'是一个地址为5181152的string,但是新的地址是不同的。 所以,如果在内存中有两个“猫”string,为什么不为id('cat')打印两个地址? 我最后的想法是,连接与地址的变化有关,所以我尝试了这个: >>> id(b[0:2]+'t') 39921024 >>> b = b[0:2]+'t' >>> b 'cat' >>> id(b) 40000896 我会预测的ID是相同的,但事实并非如此。 思考?

遍历一个JSON对象

我正在尝试遍历一个JSON对象来导入数据,即标题和链接。 我似乎无法得到过去的内容。 JSON: [ { "title": "Baby (Feat. Ludacris) – Justin Bieber", "description": "Baby (Feat. Ludacris) by Justin Bieber on Grooveshark", "link": "http://listen.grooveshark.com/s/Baby+Feat+Ludacris+/2Bqvdq", "pubDate": "Wed, 28 Apr 2010 02:37:53 -0400", "pubTime": 1272436673, "TinyLink": "http://tinysong.com/d3wI", "SongID": "24447862", "SongName": "Baby (Feat. Ludacris)", "ArtistID": "1118876", "ArtistName": "Justin Bieber", "AlbumID": "4104002", "AlbumName": "My World (Part II);\nhttp://tinysong.com/gQsw", "LongLink": "11578982", "GroovesharkLink": […]

如何outlookPython生成器中的一个元素?

我无法弄清楚如何在Python生成器中outlook一个元素。 只要我看,它走了。 这是我的意思: gen = iter([1,2,3]) next_value = gen.next() # okay, I looked forward and see that next_value = 1 # but now: list(gen) # is [2, 3] — the first value is gone! 这是一个更真实的例子: gen = element_generator() if gen.next_value() == 'STOP': quit_application() else: process(gen.next()) 任何人都可以帮我写一个发电机,你可以看一个元素向前?

如何判断tensorflow是否从python shell里面使用gpu加速?

我已经在我的ubuntu 16.04安装了tensorflow使用第二个答案在这里与Ubuntu的内置apt cuda安装。 现在我的问题是如何testingtensorflow是否真的使用GPU? 我有一个gtx 960m gpu。 当我import tensorflow这是最重要的 I tensorflow/stream_executor/dso_loader.cc:105] successfully opened CUDA library libcublas.so locally I tensorflow/stream_executor/dso_loader.cc:105] successfully opened CUDA library libcudnn.so locally I tensorflow/stream_executor/dso_loader.cc:105] successfully opened CUDA library libcufft.so locally I tensorflow/stream_executor/dso_loader.cc:105] successfully opened CUDA library libcuda.so.1 locally I tensorflow/stream_executor/dso_loader.cc:105] successfully opened CUDA library libcurand.so locally 这个输出是否足以检查tensorflow是否使用gpu?

在ipython笔记本中测量单元执行时间的简单方法

除了单元格的原始输出之外,我还想花费在单元格执行上的时间。 为此,我尝试了%%timeit -r1 -n1但是它没有暴露单元格中定义的variables。 %%time适用于只包含1条语句的单元格。 In[1]: %%time 1 CPU times: user 4 µs, sys: 0 ns, total: 4 µs Wall time: 5.96 µs Out[1]: 1 In[2]: %%time # Notice there is no out result in this case. x = 1 x CPU times: user 3 µs, sys: 0 ns, total: 3 µs Wall time: 5.96 […]

月份名称到月份数字,反之亦然

我试图创build一个函数,可以将一个月份号码转换为一个缩写月份名称或一个缩写月份名称为一个月份号码。 我认为这可能是一个常见的问题,但我不能在网上find它。 我正在考虑日历模块。 我看到要从月份号码转换为缩写月份名称,你可以做calendar.month_abbr[num] 。 我看不出去另一个方向。 创build一个字典转换其他方向是处理这个最好的方法? 还是有更好的方法,从月份名称到月份号码,反之亦然?

Python标准库中有哪些“工具”可用?

我目前知道两个工具: base64编码器/解码器: python -m base64 -e <input python -m base64 -d <input jsonvalidation和漂亮的打印机 python -m json.tool <input input可以是标准input或文件。 我很好奇,如果有其他工具暴露在SPL工作在类似的方式?

如何左alignment固定宽度的string?

我只想固定宽度的文本列,但string都是正确填充,而不是左! sys.stdout.write("%6s %50s %25s\n" % (code, name, industry)) 产生 BGA BEGA CHEESE LIMITED Food Beverage & Tobacco BHP BHP BILLITON LIMITED Materials BGL BIGAIR GROUP LIMITED Telecommunication Services BGG BLACKGOLD INTERNATIONAL HOLDINGS LIMITED Energy 但我们想要 BGA BEGA CHEESE LIMITED Food Beverage & Tobacco BHP BHP BILLITON LIMITED Materials BGL BIGAIR GROUP LIMITED Telecommunication Services BGG […]

replace文件内容中的string

如何打开一个文件Stud.txt,然后用“Orange”replace“A”的出现?

如何loggingpythonexception?

我如何在Python中loggingexception? 我看了一些选项,发现我可以使用这段代码访问实际的exception详细信息: import sys import traceback try: 1/0 except: exc_type, exc_value, exc_traceback = sys.exc_info() traceback.print_exception(exc_type, exc_value, exc_traceback) 我想以某种方式得到stringprint_exception()抛出到标准输出,以便我可以logging它。