Map Built-in

두문자어
두문자어 Python에서
def abbreviate(to_abbreviate):
    phrase = to_abbreviate.replace("-", " ").replace("_", " ").upper().split()
    
    return "".join(map(lambda word: word[0], phrase))
  • This approach begins by using str.replace() on to_abbreviate to convert non-letter characters such as - and _ into spaces.
  • The phrase is then upper-cased by calling str.upper().
  • Finally, the phrase is turned into a list of words by calling str.split().

The three methods above are all chained together, with each method operating on the output of the method before it in the "chain". This works because both replace() and upper() operate on strings (as they are str methods) and return strings. If split() was called first, replace() and upper() would fail, since they cannot operate on the list returned by split().

Note

re.findall() or re.finditer() can also be used to clean to_abbreviate. These two methods from the re module will return a list or a lazy iterator of results, respectively. As of this writing, both of these methods benchmark slower than using str.replace() for cleaning.

Once the phrase is cleaned and turned into a word list, the acronym is created via the built-in map() function. map() applies an anonymous function (the lambda in the code example) to all the items of an iterable ('mapping' the function 'onto' each item), returning a lazy iterator of results. The application of the function travels from left to right, and function results are produced as needed.

Using code from the example above, map(lambda word: word[0], ["GNU", "IMAGE", "MANIPULATION", "PROGRAM"]) would calculate "GNU"[0], "IMAGE"[0], "MANIPULATION"[0], "PROGRAM"[0] in order as a stream of data. word[0] is the function, which extracts the letter at index zero for every word in the phrase list. This stream of data can then be 'consumed' — either in a loop, or by being 'unpacked' by another function or process. Here, the iterator from map() is immediately consumed/unpacked by str.join(), which glues the results together with an empty string to produce the acronym.

Since using join() with map() is fairly succinct, the combination is put directly on the return line to produce the acronym, rather than assigning and returning an intermediate variable.

In benchmarks, this solution performed about as well as the loops, reduce and list-comprehension approaches.

Translation missing: ko.number.nth.ordinalized Sep 2026 · 유용했나요?

Python의 두문자어에 대한 다른 접근법

우리 커뮤니티가 이 연습 문제를 해결한 다른 방법
from functools import reduce

def abbreviate(to_abbreviate):
    phrase = to_abbreviate.replace("-", " ").replace("_", " ").upper().split()

    return reduce(lambda start, word: start + word[0], phrase, "")
Functools Reduce

Use functools.reduce() to form an acronym from text cleaned using str.replace().

def abbreviate(to_abbreviate):
    phrase = to_abbreviate.replace("-", " ").replace("_", " ").upper().split()

    # Note the lack of square brackets around the comprehension.
    return "".join(word[0] for word in phrase)
Generator Expression

Use a generator expression with str.join() to form an acronym from text cleaned using str.replace().

def abbreviate(to_abbreviate):
    phrase = to_abbreviate.replace("-", " ").replace("_", " ").upper().split()

    return "".join([word[0] for word in phrase])
List Comprehension

Use a list comprehension with str.join() to form an acronym from text cleaned using str.replace().

def abbreviate(to_abbreviate):
    phrase = to_abbreviate.replace("-", " ").replace("_", " ").upper().split()
    acronym = ""

    for word in phrase:
        acronym += word[0]

    return acronym
Loop

Use str.replace() to clean the input string and a loop with string concatenation to form the acronym.

import re

def abbreviate(phrase):
    removed = re.findall(r"[a-zA-Z']+", phrase)

    return "".join(word[0] for word in removed).upper()
Regex join

Use regex to clean the input string and form the acronym with str.join().

import re

def abbreviate_regex_sub(to_abbreviate):
    pattern = re.compile(r"(?<!_)\B[\w']+|[ ,\-_]")

    return re.sub(pattern, "", to_abbreviate.upper())
Regex Sub

Use re.sub() to clean the input string and create the acronym in one step.

VALID_CHARS = {" ", "-"} | set(ascii_letters)

def abbreviate(to_abbreviate):
    to_abbreviate = "".join(" " if char == "-" else char
                            for char in to_abbreviate
                            if char in VALID_CHARS)

    return "".join(word[0] for word in to_abbreviate.split()).upper()
Double Generator Expression

Use generator expressions for both cleaning and joining the input.