简体   繁体   English

map 两个不同的字符串作为一个字符串的最pythonic方法是什么?

[英]What is the most pythonic way to map two different strings, to act as one?

The problem问题

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!

A working solution一个有效的解决方案

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!"

The problem - but more difficult问题 - 但更困难

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 的状态,您可以选择:

  1. Raise the KeyError (as above, using mapping[service1_status] ).提高KeyError (如上,使用mapping[service1_status] )。

  2. Silence the error by using mapping.get(service1_status) instead and treat it as "no match".通过使用mapping.get(service1_status)来消除错误并将其视为“不匹配”。

  3. 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.

相关问题 大多数pythonic方式交错两个字符串 - Most pythonic way to interleave two strings 使绑定方法像函数一样运行的最pythonic方法是什么? - What is the most pythonic way to make a bound method act like a function? 连接字符串的大多数Python方法 - Most Pythonic way to concatenate strings 形成两个字符串列表的笛卡尔积的最 Pythonic 方法 - Most Pythonic way to form cartesian product of two lists of strings 格式化字符串的大多数pythonic方法 - Most pythonic way for formatting strings 映射字典列表的最Pythonic方法是什么? - What is the most Pythonic way to map-process lists of dictionaries? 循环遍历列表的最 Pythonic 方法是什么,其中某些条目充当后续条目的标头? - What is the most Pythonic way to loop over a list where certain entries act as headers for subsequent entries? 在列表中找到与其他元素不同的元素的最pythonic方法是什么? - what is most pythonic way to find a element in a list that is different with other elements? 什么是确保列表中所有元素不同的最pythonic方法? - What's the most pythonic way to ensure that all elements of a list are different? 将“兄弟”模块相互导入的最 Pythonic 的方法是什么? - What is the most pythonic way to import 'sibling' modules into one another?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM