让我们假设我想找到n**2所有小于的数字20000000.
import time, psutil, gc
gc.collect()
mem_before = psutil.virtual_memory()[3]
time1 = time.time()
# (comprehension, generator, function)-code comes here
time2 = time.time()
mem_after = psutil.virtual_memory()[3]
print "Used Mem = ", (mem_after - mem_before)/(1024**2) # convert Byte to Megabyte
print "Calculation time = ", time2 - time1
Run Code Online (Sandbox Code Playgroud)
1.创建通过理解列表:
x = [i**2 for i in range(20000000)]
Run Code Online (Sandbox Code Playgroud)
这真的很慢而且耗时:
Used Mem = 1270 # Megabytes
Calculation time = 33.9309999943 # Seconds
Run Code Online (Sandbox Code Playgroud)
2.使用'()'以下方法创建生成器
x = (i**2 for …Run Code Online (Sandbox Code Playgroud) 以下两种方法有什么区别吗?
哪一个更好,为什么?
PRG1:
public static boolean test() throws Exception {
try {
doSomething();
return true;
} catch (Exception e) {
throw new Exception("No!");
}
}
Run Code Online (Sandbox Code Playgroud)
PRG 2:
public static boolean test() throws Exception {
try {
doSomething();
} catch (Exception e) {
throw new Exception("No!");
}
return true;
}
Run Code Online (Sandbox Code Playgroud) 我有一个名为的文本文件foo.txt,其内容如下:
这个
是
文本
如何在Java 7中将此确切文件打印到屏幕上?
这是我编写的一个测试类,用于熟悉Python脚本中的功能@properties和setter功能:
class Test(object):
def __init__(self, value):
self.x = value
@property
def x(self):
return self.x
@x.setter
def x(self, value):
self.x = value
Run Code Online (Sandbox Code Playgroud)
问题是,当我想从我的类创建一个对象时,我面临以下错误:
>>> t = Test(1)
Traceback (most recent call last):
File "<pyshell#19>", line 1, in <module>
t = Test(1)
File "<pyshell#18>", line 3, in __init__
self.x = value
File "<pyshell#18>", line 9, in x
self.x = value
File "<pyshell#18>", line 9, in x
#A bunch of lines skipped
RuntimeError: maximum recursion depth exceeded
>>>
Run Code Online (Sandbox Code Playgroud) 您可能知道,Gloabal Platform Card Specification定义了一个名为Get Data的命令,用于从智能卡中检索一些信息.
GlobalPlatformPro是与智能卡通信的流行工具之一.当我使用此工具列出卡上已安装的小程序时,我有以下输出:
D:\3-TestTools\SmartCard>GP -list -d -v -i
//Useless Info Censored
ATR: 3B6800000073C84012009000
//Select APDU Command
A>> T=0 (4+0000) 00A40400 00
A<< (0018+2) (48ms) 6F108408A000000003000000A5049F6501FF 9000
***** Card info:
A>> T=0 (4+0000) 80CA9F7F 00
A<< (0045+2) (65ms) 9F7F2A4250010C425102902610116000099A5A0AF9425211694253117E00000000000000000000000000000000 9000
Card CPLC:
ICFabricator: 4250
ICType: 010C
OperatingSystemID: 4251
OperatingSystemReleaseDate: 0290
OperatingSystemReleaseLevel: 2610
ICFabricationDate: 1160
ICSerialNumber: 00099A5A
ICBatchIdentifier: 0AF9
ICModuleFabricator: 4252
ICModulePackagingDate: 1169
ICCManufacturer: 4253
ICEmbeddingDate: 117E
ICPrePersonalizer: 0000
ICPrePersonalizationEquipmentDate: 0000
ICPrePersonalizationEquipmentID: …Run Code Online (Sandbox Code Playgroud) 我正在使用raw_input()在变量中存储消息.因此我不能按回车键返回/换行以开始新的段落.现在,如果我按回车键,它将进入我的程序的下一部分.
我已经尝试过这样的事:
>>> message = raw_input("Message: ")
Message: Hello Sir, \n It's great that..
>>> message
"Hello Sir, \\n It's great that.."
>>>
Run Code Online (Sandbox Code Playgroud)
它没有用,我也尝试用单引号和双引号括起来,这也没用.
据我所知,有这样做,就像使用其他方式的wxPython或Tkinter的,但我想保持它严格控制台.这可能吗?
如您所知,当我们想要使用Java卡进行加密操作时,我们必须使用Cipher对象.我的问题实际上与效率有关.假设我想使用AES密钥执行一些加密和解密操作.
以下哪种策略更好?
Cipher对象并使用单个键初始化它们,但不同的模式(MODE_ENCRYPT和MODE_DECRYPT).然后对于每个操作,我只需要doFinal()在适当的对象上调用方法.Cipher对象,每次调用doFinal()方法之前init(),使用正确的模式对对象执行方法调用.我在 Python 中有这本字典:
[('157.55.39.64', 4), ('188.165.15.192', 2), ('1.165.15.192', 1)]
Run Code Online (Sandbox Code Playgroud)
其中第一个字段是 IP 地址,另一个字段表示文件中出现的次数。
我想有一个像这样的json表示:
[{"ip":"157.55.39.64","times":4},
{"ip":"188.165.15.192","times":2},
{"ip":"1.165.15.192","times":1}]
Run Code Online (Sandbox Code Playgroud)
我尝试使用 jsonify(dictionary),但我得到了这个:
{"157.55.39.64":4, "188.165.15.192": 2, "1.165.15.192":1}
Run Code Online (Sandbox Code Playgroud)
这只是一个 json 项目。我不知道如何将字符串“ip”和“times”放在 json 文件中。
有什么帮助吗?
python ×4
java ×3
javacard ×2
python-2.7 ×2
class ×1
cryptography ×1
dictionary ×1
exception ×1
generator ×1
io ×1
java-7 ×1
json ×1
python-3.x ×1
smartcard ×1
text ×1
try-catch ×1