# coding=utf-8
import itertools
# list.reverse()
# 反向列表中元
def foo(lst,s):
return [str(i)+s for i in lst]
data=[[4,5,4,46,4], [1, 2, 8, 9],
[9, 4, 9, 11],
[3,3],
[5, 8, 11, 1],
[5, 7, 10, 11],
]
t=0
#判断首尾相同提取拆分数组
# shouwei=[]
# for i in range(len(data)):
# if data[i][0] == data[i][-1]:
# print shouwei.append(data.pop(i));
#判断数组首尾相同
for i in range(len(data)):
# print i
temp=[]
# shouwei=[]
# if data[i][0] != data[i][-1]:#判断是否是首尾相同
for j in range(i+1,len(data)):
if data[j][0] == data[i][-1] or data[j][-1] == data[i][-1]:#如果是邻边
if data[i][-1] != data[j][0]:
data[j].reverse()
if data[i+1] in data:
data[j],data[i+1]=data[i+1],data[j]
print data
python爬虫使用urllib2方法
# coding:utf8
import urllib2
import cookielib
url = "http://www.baidu.com"
print '第一种方法'
response1=urllib2.urlopen(url)
print response1.getcode()
print len(response1.read())
print '第二种方法'
request=urllib2.Request(url)
request.add_header("user-agent","Mozilla/5.0")
response2=urllib2.urlopen(request)
print response2.getcode()
print len(response2.read())
print '第三种方法'
cj=cookielib.CookieJar()
opener=urllib2.build_opener(urllib2.HTTPCookieProcessor(cookiejar=cj))
urllib2.install_opener(opener)
request=urllib2.Request(url)
response3=urllib2.urlopen(request)
print response3.getcode()
print cj
print response3.read()
python取余数
x=4000
y=200
print x%y
python随机定时休息程序
# -*- coding: UTF-8 -*-
import random
import time
a=random.randint(1, 10)
print "休息时间为:",a
time.sleep(a)
判断逗号是否在字符串内
site = 'Illinois;Winnebago County;Rockford, Machesney Park, Machesney Pk;61103'
if "," not in site:
print('在里面')
读取文件删除不需要文件
# coding=utf-8
import os
# 定义函数用于,获得文件名
def get_filename(path, filetype):
# import os
name = []
for root, dirs, files in os.walk(path):
for dirs in files:
if "," in dirs:
os.remove(dirs)
return name
if __name__ == '__main__':
# 测试
path = os.getcwd()
print path
filetype = '.txt'
# data=open_allfile(path,filetype)
name = get_filename(path, filetype)
# print name
解一元多次方程
# coding=utf-8
a=[[-1,1], [1,0]]
a=np.array(a)#转成数组
b=[48,-32]
b=np.array(b)
c=np.linalg.solve(a,b)
print "美国第一夫人比法国第一夫人小:",c[1],"岁"
Python把带有中括号的字符串直接转为列表
可以把list,tuple,dict和string相互转化。
字符串转换成列表
>>>a = "[[1,2], [3,4], [5,6], [7,8], [9,0]]"
>>>type(a)
>>> b = eval(a)
>>> print b
[[1, 2], [3, 4], [5, 6], [7, 8], [9, 0]]
>>> type(b)
字符串转换成字典
>>> a = "{1: 'a', 2: 'b'}"
>>> type(a)
>>> b = eval(a)
>>> print b
{1: 'a', 2: 'b'}
>>> type(b)
字符串转换成元组
>>> a = "([1,2], [3,4], [5,6], [7,8], (9,0))"
>>> type(a)
>>> b = eval(a)
>>> print b
([1, 2], [3, 4], [5, 6], [7, 8], (9, 0))
>>> type(b)