python学习笔记+使用技巧

来源:互联网 发布:布尔战争知乎 编辑:程序博客网 时间:2024/05/18 02:29

>>1.django模版中怎么用for输出一个字典的所有键值?

{% for key, value in data.items %}    {{ key }}: {{ value }}{% endfor %}

 

>>2.标签之间可以嵌套

添加:

<div class="unit"><label>机房状态:</label><select name="status" class="required combox" >{% for key,value in nocinfo_status_dict.items %}<option value="{{ key }}">{{ value }}</option>{% endfor %}</select></div>


 

修改:

<div class="unit"><label>机房状态:</label><select name="status" class="required combox" >{% for key,value in noc_dict.items %}{% ifequal nocinfo.status key %}<option value="{{ key }}" selected="selected">{{ value }}</option>{% else %}<option value="{{ key }}">{{ value }}</option>{% endifequal %}{% endfor %}</select></div>

 

basepage:

<td>           {% for key,value in nocinfo_status_dict.items %}{% ifequal nocinfo.status key %}{{ value }}{% endifequal %}{% endfor %}           </td>

以前basepage是这样写的:

<td>           {% ifequal nocinfo.status 0 %}备用{% endifequal %}           {% ifequal nocinfo.status 1 %}使用{% endifequal %}           {% ifequal nocinfo.status 2 %}报废{% endifequal %}           </td>




 

这里要注意一个问题,注意nocinfo.status 和 key 对应的数据类型是否一致,不一致的话不能这样做比较,得到的结果是不相等。

noc_dict = {'0':'备用', '1':'使用', '2':'报废'}


改成这样:

noc_dict = {0:'备用', 1:'使用', 2:'报废'}


 



python一些小问题:
1 交换两个变量的值: 
 a,b =b,a

2 字符串的格式化与字符串的连接: 
>>> uid = "sa"
>>> pwd = "secret"
>>> print pwd + " is not a good password for " + uid      1
secret is not a good password for sa
>>> print "%s is not a good password for %s" % (pwd, uid) 2
secret is not a good password for sa
>>> userCount = 6
>>> print "Users connected: %d" % (userCount, )           3 4
Users connected: 6
>>> print "Users connected: " + userCount                 5
Traceback (innermost last):
  File "<interactive input>", line 1, in ?
TypeError: cannot concatenate 'str' and 'int' objects

1     + 是字符串连接操作符。
2     在这个简单例子中,字符串格式化实现与连接一样的结果。
3     (userCount, ) 是一个只包含一个元素的 tuple。是的,语法有一点奇怪,但是使用它的理由就是:显示地指出它是一个 tuple,而不是其他。实际上,当定义一个 list、tuple 或 dictionary 时,您可以总是在最后一个元素后面跟上一个逗号,但是当定义一个只包含一个元素的 tuple 时逗号是必须的。如果省略逗号,Python 不会知道 (userCount) 究竟是一个只包含一个元素的 tuple 还是变量 userCount 的值。
4     字符串格式化通过将 %s 替换成 %d 即可处理整数。
5     试图将一个字符串同一个非字符串连接会引发一个异常。与字符串格式化不同,字符串连接只能在被连接的每一个都是字符串时起作用。

 

 

 

3list映射:

>>> li = [1,9,8,4]
>>> [elem*2 for elem in li]
[2, 18, 16, 8]
>>>
>>> li
[1, 9, 8, 4]

 

字典的keys,values,item方法:


>>> params = {"dd":"ddsff","a":"fdf"}
>>> params.keys()
['a', 'dd']
>>> b=params.values()
>>> print b
['fdf', 'ddsff']
>>>
>>>
>>>
>>> params.items()
[('a', 'fdf'), ('dd', 'ddsff')]
>>>
>>>
>>>

 

join 只能用于元素是字符串的 list;它不进行任何的强制类型转换。连接一个存在一个或多个非字符串元素的 list 将引发一个异常。

 

1split 与 join 正好相反,它将一个字符串分割成多元素 list。注意,分隔符 (“; ”) 被完全去掉了,它没有在返回的 list 中的任意元素中出现。2split 接受一个可选的第二个参数,它是要分割的次数。


1. 读取文件的内容

  1. filePath = "文件路径"  
  2. (lambda f: (f.read(), f.close()))(file(filePath))[0]  

2. 将内容写入到文件中

  1. (lambda f, d: (f.write(d), f.close()))(file(r'd:/a.txt''w'), '要写入的数据')   

3. 根据正则表达式过滤文件夹中的文件

  1. import os, re  
  2. f1 = lambda dir = os.getcwd() ,p = '': [file for file in os.listdir(dir) if p == '' or re.search(p, file)]  
  3. #f2 = lambda dir = os.getcwd(), p = '': filter(lambda f: p == '' or re.search(p, f), os.listdir(dir))   
  4. print f1(p = r'/.py$')  #列出当前目录下所有的py文件  
  5. #print f2(p = r'/.py$')   
  

4. 将b列表中的在a列表中不存在的元素添加到a列表中。

  1. def appendDiff(aList, bList):  
  2.     return ([item in aList or aList.append(item) for item in bList] or 1and aList  

5. 序列类型的反转。

  1. s = 'abcd'  
  2. print s[::-1]  
  3. l = [1234]  
  4. print l[::-1]  
  不断更新中... ...
注意:4 有很简单的方法: a+= [x for x in b if not(x in a)]
或者:
a+= set(b) - set(a)