python
共 5 篇文章
python两个dictionary合并
问题描述: 两个dictionary: dict1={1:[1,11,111],2:[2,22,222]} dict2={3:[3,33,333],4:[4,44,444]} 合并两个字典得到类似 {1:[1,11,111],2:[2,22,222],3:[3,33,333],4:[4,44,444]} 方法1: dictMerged1=dict(dict1.items()+dict2.items()) 方法2: dictMerged2=dict(dict1, **dict2) 方法2等同于 dictMerged=dict1.copy() dictMerged.update(dict2) 或…
2010-11-29 04:43 · python
浮点数转换科学计数字符串
m = 1.23344 m_str = '{0:1.2E}'.format(m) n = 0.00025 mn_str = '{0:1.2E} {1:1.2e}'.format(m,n) #outputs: #m_str: '1.23E+00' #mn_str: '1.23E+00 2.50e-04'
2010-03-09 03:39 · python
数值转换字符串 固定位数
问题:一个浮点型的数要转化为字符串,比如 n=1.50000,要转换成‘1.50000’。如果用 str(n),出来的字符串是'1.5'。 目前的解决方案:用fpformat包 import fpformat n = 1.50000 fpformat.fix(n,5) 这时在shell中的输出就是'1.50000' 后续问题:fpformat包在3.0之后的版本中就没有了。不知道还有什么解决方法。请高手赐教。
2010-03-03 13:11 · python
IDLE处理main()函数参数
命令行里可以直接加参数传递给主函数。IDLE没有输入参数的地方(貌似pythonwin有,没用过,不确定)。假设下面的scripts名叫helloworld.py,此时如果要用IDLE执行,需要在main函数一开始的地方加入以下语句: import sys blablabla..... #other codes def main(): try: __FILE__ except: sys.argv=['helloworld.py', '-o','a'] blablablabla.... #other codes 第一个参数是要解释的脚本名,第二个参数是option,第三个参数是argument…
2009-09-08 12:58 · python
python indent sensitive
第一次写python,感觉很诡异。居然是indent sensitive。写while循环的时候,程序运行死机。syntax检查没有问题,但是怎么没有想到是因为没有缩进所以找不到循环结束。太诡异了。写java和c++缩进只是一个很好的coding习惯,python…… 看来比我想象的要复杂一点。
2009-09-06 16:21 · python