Chinaunix首页 | 论坛 | 博客
  • 博客访问: 26277656
  • 博文数量: 2065
  • 博客积分: 10377
  • 博客等级: 上将
  • 技术积分: 21525
  • 用 户 组: 普通用户
  • 注册时间: 2008-11-04 17:50
文章分类

全部博文(2065)

文章存档

2012年(2)

2011年(19)

2010年(1160)

2009年(969)

2008年(153)

分类: Python/Ruby

2010-04-21 20:11:27

#-*- 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

import sys
reload(sys)


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)])



阅读(642) | 评论(2) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2010-05-10 08:41:54

系统地学习一个东西将其学精了

hkebao2010-05-07 18:36:38

http://jexcelapi.sourceforge.net/resources/javadocs/2_6_10/docs/index.html