Python如何调用字典的结果并将结果传递给几个变量

时间:2016-07-13 05:49:42

标签: python

我有3个变量x,y,z,它们将具有不同的值,具体取决于我要执行的3个测试的结果。因此,测试将有8种可能的结果。这8个结果将匹配存储在名为dic的标题中的相应结果。我需要将相应的结果传递给变量x,y,z。

代码如下:

fetch = requests.get('http://www.example.com')

match_M = re.search(r'something1...' , fetch.text)
if match_M != None: match_M = 1 
else: match_M = 0

match_K = re.search(r'something2...' , fetch.text)
if match_K != None: match_K = 1 
else: match_K = 0

match_T = re.search(r'something3...' , fetch.text)
if match_T != None: match_T = 1 
else: match_T = 0

outcome = [[0,3,1], [0,2,1,], [0,3,2], [0,2,1], [1,3,1], [1,3,2], [2,3,1], [3,3,1]]

dic = {'111':outcome[0], '110':outcome[1], '101':outcome[2], '100':outcome[3], '011':outcome[4], '010':outcome[5], '001':outcome[6], '000':outcome[7]}

X_num = str(match_M) + str(match_K) + str(match_T)

print dic['X_num']

x, y, z = dic['X_num']

有没有更好的方法来编写它我不需要使用str(match_M)+ str(match_K)+ str(match_T),也许我可以使用像dic ['match_M + match_K + match_K']这样的东西

很抱歉,如果这听起来太愚蠢,我刚开始学习。

2 个答案:

答案 0 :(得分:1)

我可能会稍微改变一下这个问题,至少对于以下三行

outcome = [[0,3,1], [0,2,1,], [0,3,2], [0,2,1], [1,3,1], [1,3,2], [2,3,1], [3,3,1]]  
dic = {'111':outcome[0], '110':outcome[1], '101':outcome[2], '100':outcome[3], '011':outcome[4], '010':outcome[5], '001':outcome[6], '000':outcome[7]}
X_num = str(match_M) + str(match_K) + str(match_T)

我更愿意迭代基数集[0,1]的结果和笛卡尔积,并用每个单独的匹配变量对其进行索引

>>> from itertools import product
>>> dic = {k : v for k, v in zip(product([0,1], repeat = len(keys)), outcome)}
>>> dic
{(0, 1, 1): [0, 2, 1], (1, 1, 0): [2, 3, 1], (1, 0, 0): [1, 3, 1], (0, 0, 1): [0, 2, 1], (1, 0, 1): [1, 3, 2], (0, 0, 0): [0, 3, 1], (0, 1, 0): [0, 3, 2], (1, 1, 1): [3, 3, 1]}
>>> dic[(match_M, match_K, match_T)]
[1, 3, 2] 

我甚至会扩展这个想法,并且不鼓励你使用单独的匹配变量,而是匹配元组

keys = [r'something1...', r'something2...', r'something3...']
match = [[0, 1][re.search(key , fetch.text) != None] for key in keys]

总结

def foo(url, keys, outcome):
    from itertools import product, izip
    fetch = fetch = requests.get(url)
    match = [[0, 1][re.search(key , fetch.text) != None] for key in keys]
    dic = {k : v for k, v in izip(product([0,1], repeat = len(keys)), outcome)}
    return dic[match]

这将帮助您缩放设计,而不是基于键的基数来限制

注意我通常更喜欢[0, 1][re.search(key , fetch.text) != None],但同样鼓励使用类似0 if re.search(key , fetch.text) == None else 1的替代语法

答案 1 :(得分:1)

也许这样的事情会有所改善吗?

fetch = requests.get('http://www.example.com')

match_M = 0 if re.search(r'something1...' , fetch.text) else 4
match_K = 0 if re.search(r'something2...' , fetch.text) else 2
match_T = 0 if re.search(r'something3...' , fetch.text) else 1

index = match_M + match_K + match_T

outcomes = [[0,3,1], [0,2,1], [0,3,2], [0,2,1], [1,3,1], [1,3,2], [2,3,1], [3,3,1]]

x, y, z = outcomes[index]

这是基于您建立一个3位二进制数字的观察结果,因此数字位于4s位置,2s位置和1s位置。我从你的计划中反转了1和0,因为那样"结果"与您正在构建的二进制数字的顺序相同。

修改

一点点"代码高尔夫" (对不起):

fetch = requests.get('http://www.example.com')

x, y, z = [[0,3,1], [0,2,1], [0,3,2], [0,2,1], [1,3,1], [1,3,2], [2,3,1], [3,3,1]][
    sum(2**i * (0 if re.search(pattern, fetch.text) else 1)
        for i, pattern in enumerate([r'something3', r'something2', r'something1']))]