码迷,mamicode.com
首页 > 编程语言 > 详细

python3的dict

时间:2016-09-29 20:19:06      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:

 1 dict1 = {getlistUrl:getlistData,getskuUrl:getskuData, approveUrl:approveData, approvedlistUrl:approvedlistData, searchpresellUrl:searchpresellData, deletepresellUrl:deletepresellData} 

 

然后

import collections
info = dict(name=cold, blog=linuxzen.com) for key, value in info.items(): print (key, :, value)

还学会了,妈蛋,调用函数,蠢哭了 嘤嘤嘤:

def kolApprove(url1, value):
    kolRequest = requests.post(url=url1,json=value,cookies=userlogin.cookies)
    print(kolRequest.url)
    pprint(kolRequest.json())
    if kolRequest.status_code == 200:
        print (kolRequest.status_code,kolRequest.reason,\n)
    else:
        print (kolRequest.read())

def main():
    print (let\‘s try it+\n )
    for ur, va in dict1.items():
        kolApprove(ur,va)
# 用main来开始调用 main()

后来发现,用dict ,不是顺序请求接口的,于是改成list:

# 搞定了,原来要单独一对的里面才可以用.items()
def kolApprove(url1, value):
    kolRequest = requests.post(url=url1,json=value,cookies=userlogin.cookies)
    print(kolRequest.url)
    pprint(kolRequest.json())
    if kolRequest.status_code == 200:
        print (kolRequest.status_code,kolRequest.reason,\n)
    else:
        print (kolRequest.read())

def main():
    print (let\‘s try it+\n )
    for number in list1:
        for ur, va in number.items():
            kolApprove(ur,va)

 

 接着,听说了一个OrderedDict,有序字典,先装utils库,然后:

from collections import OrderedDict
‘‘‘
这样的形式也行,但是一定要ordereddict来格式化一下numbers列表,不可以先写成dict,再用ordereddict 

‘‘‘
numbers = ((getlistUrl,getlistData),(getskuUrl,getskuData),(approveUrl,approveData),(approvedlistUrl,approvedlistData),(searchpresellUrl,searchpresellData),(deletepresellUrl,deletepresellData))
ordered_dict = OrderedDict(numbers)
for k, v in ordered_dict.items():
    print (k,v)

不可以直接用numbers={a:b,c:d}这样的字典形式写,

应先写成numbers =((a,b),(c,d))

然后用ordered_numbers = OrderedDict(numbers) 来生成有序化字典,

然后就可以按顺序输出啦,就可以按顺序请求接口啦,啦啦啦啦??

 

python3的dict

标签:

原文地址:http://www.cnblogs.com/vivivi/p/5920654.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!