티스토리 뷰

반응형

Stack Overflow에 자주 검색, 등록되는 문제들과 제가 개발 중 찾아 본 문제들 중에서 나중에도 찾아 볼 것 같은 문제들을 정리하고 있습니다.

Stack Overflow에서 가장 먼저 확인하게 되는 가장 높은 점수를 받은 Solution과 현 시점에 도움이 될 수 있는 가장 최근에 업데이트(최소 점수 확보)된 Solution을 각각 정리하였습니다.

 

아래 word cloud를 통해 이번 포스팅의 주요 키워드를 미리 확인하세요.

Alphabet range in Python

Python에서 알파벳 리스트 만들기

 문제 내용 

How do I create a list of alphabet characters, without doing it manually like this?

다음과 같이 수동으로 하지 않고 알파벳 문자열의 리스트를 만드는 방법은 무엇인가요?
['a', 'b', 'c', 'd', ..., 'z']

 

 

 높은 점수를 받은 Solution 

>>> import string
>>> string.ascii_lowercase
'abcdefghijklmnopqrstuvwxyz'
>>> list(string.ascii_lowercase)
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

 


 

Alternatively, using range:

또는 range를 사용하여:
>>> list(map(chr, range(97, 123)))
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

 

Or equivalently:

또는 다음과 같이 작성할 수 있습니다:
>>> list(map(chr, range(ord('a'), ord('z')+1)))
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

 


 

Other helpful string module features:

그 밖에 유용한 문자열 모듈 기능:
>>> help(string)
....
DATA
    ascii_letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
    ascii_lowercase = 'abcdefghijklmnopqrstuvwxyz'
    ascii_uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
    digits = '0123456789'
    hexdigits = '0123456789abcdefABCDEF'
    octdigits = '01234567'
    printable = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~ \t\n\r\x0b\x0c'
    punctuation = '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'
    whitespace = ' \t\n\r\x0b\x0c'

 

 

 가장 최근 달린 Solution 

This is the easiest way I can figure out:

제가 생각한 가장 쉬운 방법은 다음과 같습니다:
#!/usr/bin/python3
for i in range(97, 123):
    print("{:c}".format(i), end='')

 

So, 97 to 122 are the ASCII number equivalent to 'a' to and 'z'. Notice the lowercase and the need to put 123, since it will not be included).

즉, 97에서 122까지의 숫자는 'a'에서 'z'까지의 ASCII 숫자와 대응됩니다. 소문자와 포함하지 않으므로 123을 넣어야 합니다).

 

In print function make sure to set the {:c} (character) format, and, in this case, we want it to print it all together not even letting a new line at the end, so end=''would do the job.

출력 함수에서 문자열의 형식으로 {:c} (문자)를 설정하고, 이 경우에는 끝에 새 줄을 허용하지 않고 모두 함께 출력하려면 end=''를 사용하면 됩니다..

 

The result is this: abcdefghijklmnopqrstuvwxyz

결과는 다음과 같습니다: abcdefghijklmnopqrstuvwxyz

 

 

 

출처 : https://stackoverflow.com/questions/16060899/alphabet-range-in-python

반응형
댓글
공지사항
최근에 올라온 글