#-*- coding:utf-8 -*-
import re
dicts = {'name':'test'}
if dicts.has_key('name'):
def methods(data):
htmlRegex = re.compile('<[^>]*>')
words = htmlRegex.findall(data)
return words
results = methods(dicts.get('name'))
print results
|
1. 在if语句中再定义一个函数。python非常灵活!
selected_as_list = [{'name':'a'},{'name':'b'},{'name':'c'}]
users = [x.get('name') for x in selected_as_list]
print ''.join(users)
|
2. 学一下如何在python中做遍历操作的。
x.get('name') for x in selected_as_list
python语法真是美!
3. 实现批量插入的代码。解决这次的问题
import MySQLdb
conn = MySQLdb.connect("localhost","root","123","images",port=3306,connect_timeout=10,compress=True,charset='utf8',use_unicode=True)
cursor=conn.cursor()
word_set = [['a','b'],['c','d']]
cursor.executemany("INSERT INTO images (uid,picurl) VALUES (%s,%s)", [(word,uriid) for word,uriid in word_set ])
conn.commit()
cursor.close()
conn.close()
|
非常不错的~
4。判断一个字符串是否以指定字符串序列中的字符串结尾
import itertools
def anyTrue(predicate, sequence):
return True in itertools.imap(predicate, sequence)
def endsWith(s, *endings):
return anyTrue(s.endswith, endings)
import os
for filename in os.listdir('.'):
if endsWith(filename, '.jpg', '.py', '.gif'):
print filename
|
5. python遍历list。添加一个新的方法非常帅
lists = ['a',123,'e',2.3]
for ele in lists:
print ele
for i in xrange(len(lists)):
print lists[i]
for index,num in enumerate(lists):
print index,num 这个关键字enumerate非常之帅!
|
6.python实现的三元表达式
load = True if(float(value) > float(s_perfalarm.rv)) else False
|
7. python实现二维数组
multilist = [[0 for col in range(5)] for row in range(3)]
|
8.python实现类似于php里面的reload
9.python实现乘法口诀
print '\n'.join([ ' '.join([ "%d*%d=%2s" %(y,x,x*y) for y in range(1,x+1)]) for x in range(1,10)])
|
阅读(676) | 评论(2) | 转发(0) |