加速列表附加过程,通过if else语句

huangapple go评论55阅读模式
英文:

Speeding up the process of list-appending via if else statements

问题

Here's a more efficient way to achieve the same result in Python:

abc_list = ["AB", "AC", "AB", "BC", "BB", ...]

def translate_element(element):
    if element in ["AA", "AB", "BA"]:
        return "A"
    elif element in ["BB", "AC", "CA"]:
        return "B"
    elif element in ["CC", "CB", "BC"]:
        return "C"
    else:
        return "B"

new_abc_list = [translate_element(element) for element in abc_list]

This code uses a function translate_element to simplify the translation logic and then applies it to each element in abc_list using a list comprehension.

英文:

I have a list that contains strings that are a combination of "A", "B" and "C".

For example:

abc_list = ["AB", "AC", "AB", "BC", "BB", ...]

I now want to create a new list that translates every element again to "A", "B" and "C" with a simple rule.

The rule is as follows:

If the element is "AA", "AB" or "BA", then the new string element has to become "A".
If the element is "BB", "AC” or "CA”, then the new string element has to become "B".
If the element is "CC", "CB" or "BC", then the new string element has to become "C".

So the new list becomes:

new_abc_list = ["A", "B", "A", "C", "B", ...]

I'm doing it as follow for the moment, but I got a feeling it can be done much more efficient.

new_abc_list = []
for element in abc_list :
    if element == "AA":
        new_abc_list .append("A")
    elif element == "AB":
        new_abc_list .append("A")
    elif element == "BA":
        new_abc_list .append("A")
    elif element == "CB":
        new_abc_list .append("C")
    elif element == "CC":
        new_abc_list .append("C")
    elif element == "BC":
        new_abc_list .append("C")
    else:
        new_abc_list .append("B")
return new_abc_list 

What is a more efficient way of doing this?

答案1

得分: 1

这似乎是一个适合使用字典和列表推导的好任务:

mapper = {"AA": "A", "AB": "A", "BA": "A", "CB": "C", "CC": "C", "BC": "C"}

abc_list = ["AB", "AC", "AB", "BC", "BB"]

# 查找键对应的值,如果找不到则默认为 'B'
new_abc_list = [mapper.get(x, 'B') for x in abc_list]

输出:

['A', 'B', 'A', 'C', 'B']

复杂度将为 O(n),因为字典查找是 O(1)。与你的原始代码相比,在循环中,需要测试所有值,直到找到每个项目的匹配项。

计时结果:
对于 500k 个项目(abc_list = ["AB", "AC", "AB", "BC", "BB"] * 100_000)进行了 6 次测试:

# 使用字典和列表推导
27.8 毫秒 ± 1.94 毫秒 每次循环均值 ± 标准差7 次运行10 次循环每次

# 使用字典和 for 循环和追加
56.4 毫秒 ± 12.5 毫秒 每次循环均值 ± 标准差7 次运行10 次循环每次

# 使用测试的循环
84.8 毫秒 ± 11.7 毫秒 每次循环均值 ± 标准差7 次运行10 次循环每次

最坏情况下:500k 个项目均不匹配任何测试(abc_list = ['ZZ']*500_000):

# 使用字典和列表推导
35.1 毫秒 ± 4.08 毫秒 每次循环均值 ± 标准差7 次运行10 次循环每次

# 使用字典和 for 循环和追加
48.2 毫秒 ± 2.62 毫秒 每次循环均值 ± 标准差7 次运行10 次循环每次

# 使用测试的循环
124 毫秒 ± 20.7 毫秒 每次循环均值 ± 标准差7 次运行10 次循环每次
英文:

This looks like a good job for a dictionary and a list comprehension:

mapper = {"AA": "A", "AB": "A", "BA": "A", "CB": "C", "CC": "C", "BC": "C"}

abc_list = ["AB", "AC", "AB", "BC", "BB"]

# find the value from key, default to 'B' if not found
new_abc_list = [mapper.get(x, 'B') for x in abc_list]

Output:

['A', 'B', 'A', 'C', 'B']

Complexity will be O(n) as dictionary search is O(1). In contrast your original code has to test all values until it find a match for each item in the loop.

timings

On 500k items (abc_list = ["AB", "AC", "AB", "BC", "BB"] * 100_000) with 6 tests:

# dictionary with list comprehension
27.8 ms ± 1.94 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

# dictionary with for loop and append
56.4 ms ± 12.5 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

# loop with tests
84.8 ms ± 11.7 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

worst case: 500k items that are not matching any test (abc_list = ['ZZ']*500_000):

# dictionary with list comprehension
35.1 ms ± 4.08 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

# dictionary with for loop and append
48.2 ms ± 2.62 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

# loop with tests
124 ms ± 20.7 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

huangapple
  • 本文由 发表于 2023年4月13日 23:18:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76007165.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定