1,分析列表,保存字典
-
timesL = (0,1243,2312,2332,2333,2333,0,0,0,0,0,0,0,2332,2222,2222,2221,0,0,0,0,0,2111,590,2223,2224,2333,2444,2443,0,0,0,0,0,0,0,1555,2112)
-
times = list(timesL)
-
dicTime = {}
-
key = 0
-
tsL2 = []
-
x = 0
-
for i, value in enumerate(timesL):
-
print 'i, value',i, value
-
if i == 0:continue
-
while value != 0:
-
print i,
-
key = i + 1
-
tsL2 = []
-
x = 0
-
break
-
while value == 0:
-
tsL2.append(0)
-
x = x + 1
-
dicTime[key] = x
-
print x, tsL2
-
break
-
print dicTime
-
-
timesDic = {17: 5, 29: 7, 6: 7}
-
print timesDic.values()
-
print timesDic.keys()
-
lis1 = timesDic.keys()
-
lis1.sort()
-
print [timesDic[key] for key in lis1]
-
print lis1
-
for i in lis1:
-
print timesDic[i]
-
-
timesDic.keys().sort()
2,dict 中 key 与value 排序
对字典按键排序,用元组列表的形式返回,同时使用lambda函数来进行;
sorted(iterable[, cmp[, key[, reverse]]]
cmp和key一般使用lambda
如:
>>> d={"ok":1,"no":2}
对字典按键排序,用元组列表的形式返回
>>> sorted(d.items, key=lambda d:d[0])
[('no', 2), ('ok', 1)]
对字典按值排序,用元组列表的形式返回
>>> sorted(d.items, key=lambda d:d[1])
[('ok', 1), ('no', 2)]
3,urllib几种请求方式
3.1 示例会话,使用get方法检索包含参数的url:
-
import urllib
-
params = urllib.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})
-
f = urllib.urlopen("%s" % params)
-
print f.read()
3.2 下面的示例使用POST方法代替:
-
import urllib
-
params = urllib.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})
-
f = urllib.urlopen("", params)
-
print f.read()
3.3 下面的示例使用显式指定的HTTP代理,覆盖环境设置
-
import urllib
-
proxies = {'http': ''}
-
opener = urllib.FancyURLopener(proxies)
-
f = opener.open("")
-
f.read()
3.4 下面的示例不使用代理,覆盖环境设置:
-
import urllib
-
opener = urllib.FancyURLopener({})
-
f = opener.open("/")
-
f.read()
阅读(527) | 评论(0) | 转发(0) |