[英]What is the most pythonic way to map two different strings, to act as one?
Imagine two services running:想象一下两个服务正在运行:
Service 1 has two statuses: ACTIVE/INACTIVE
服务 1 有两种状态:
ACTIVE/INACTIVE
Service 2 has two statuses: RUNNING/TERMINATED
服务 2 有两种状态:
RUNNING/TERMINATED
We want to do a simple status comparison as:我们想做一个简单的状态比较:
service1_status = get_service2_status()
service2_status = get_service2_status()
if service1_status == service2_status:
// They match!
else:
// They don't match!
This is a relatively simple task of course.这当然是一个相对简单的任务。 A working solution would be something like this:
一个可行的解决方案是这样的:
service1_status = get_service2_status()
service2_status = get_service2_status()
if service1_status == 'ACTIVE' and service2_status == 'RUNNING':
return "They match!"
elif service1_status == 'INACTIVE' and service2_status == 'TERMINATED':
return "They match!"
else:
return "They don't match!"
Now imagine this example a hundred different statuses for each service instead of two.现在想象一下这个例子,每个服务有一百个不同的状态,而不是两个。
The if statement would be overwhelming. if 语句将是压倒性的。 I am looking for a more elegant way to program this.
我正在寻找一种更优雅的编程方式。 eg:
例如:
matching_tuples = [('x1', 'y1'), ('x2', 'y2'), ..., ('x100', 'y100')]
service1_status = get_service2_status()
service2_status = get_service2_status()
if (service1_status, service2_status) in matching_tuples:
return "They match!"
elif:
return "They don't match!"
This works and is fine.这有效并且很好。 I wonder though if there is a better, more pythonic and elegant way to marry two strings together and compare them.
我想知道是否有更好、更 Pythonic 和优雅的方式将两个字符串结合在一起并进行比较。
Decide on a base-line service and map its statuses to the other:确定一个基线服务和 map 其对另一个的状态:
service1_status = get_service2_status()
service2_status = get_service2_status()
mapping = {"ACTIVE": "RUNNING", "INACTIVE": "TERMINATED"}
if mapping[service1_status] == service2_status:
return "They match!"
else:
return "They don't match!"
In case a status of service1 might not be mapped, you can choose either to:如果可能未映射 service1 的状态,您可以选择:
Raise the KeyError
(as above, using mapping[service1_status]
).提高
KeyError
(如上,使用mapping[service1_status]
)。
Silence the error by using mapping.get(service1_status)
instead and treat it as "no match".通过使用
mapping.get(service1_status)
来消除错误并将其视为“不匹配”。
Catch the error and re-raise it as an informative debugging message:捕获错误并将其作为信息调试消息重新引发:
try: match = mapping[service1_status] == service2_status except KeyError: raise KeyError(f"The status {service1_status} of service1 is not mapped to any status in service2")
Depending on your actual input, you might not need to construct the dict manually:根据您的实际输入,您可能不需要手动构建字典:
If you already have something like your provided matching_tuples
, you can pass that directly to the dict constructor - mapping = dict(matching_tuples)
.如果您已经有类似您提供的
matching_tuples
的东西,您可以将其直接传递给 dict 构造函数 - mapping = dict(matching_tuples)
。
If you have two matching lists of the statuses you can do mapping = dict(zip(statuses1, statuses2))
(Note that basically matching_tuples = list(zip(statuses1, statuses2))
)如果你有两个匹配的状态列表,你可以做
mapping = dict(zip(statuses1, statuses2))
(注意基本上matching_tuples = list(zip(statuses1, statuses2))
)
I also have to map keys from the backend and frontend.我还必须从后端和前端获取 map 密钥。
For mapping, I usually use a dictionary
you can add as many keys as you want like this.对于映射,我通常使用
dictionary
,您可以像这样添加任意数量的键。
MapService1to2 = {
"ACTIVE": "RUNNING",
"INACTIVE": "TERMINATED"
}
service1_status = get_service2_status()
service2_status = get_service2_status()
if MapService1to2[service1_status] == service2_status:
print("They match")
else:
print("They don't match")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.