Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python如何调用字典的结果并将结果传递给几个变量_Python - Fatal编程技术网

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

Python如何调用字典的结果并将结果传递给几个变量,python,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 =

我有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']


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

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

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]
但是如果re.search(key,fetch.text)==None,我会选择like
0的替代语法,如果同样鼓励re.search(key,fetch.text)==None,那么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,因为这样“结果”的顺序与你正在构建的二进制数的顺序相同

编辑

一点“密码高尔夫”(对不起):


我认为是
结果[index]
而不是
dic[index]
?非常感谢@斯马尔克斯。非常感谢您的帮助。如果代码已经运行,而您只是想帮助改进它,那么这是一个比StackOverflow更合适的网站。感谢您的推荐,@skrrgwasmeThank非常感谢您对Abhijit的帮助,但就我目前的水平而言,我确实需要更多的时间和实践来理解您的代码。无论如何谢谢你!干得好。
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']))]