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

2021-09-27 05:05:11 发布

您现在位置: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条回答
网友
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']))]
网友
2楼 ·

我可能会以不同的方式处理这个问题,至少对于下面三行

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的替代语法

相关问题