프로그래머스 lv1 숫자 문자열과 영단어 파이썬
알파벳 딕셔너리로 생성하기.
from string import ascii_lowercase
alphabet_dict = {}
for idx, i in enumerate(ascii_lowercase):
alphabet_dict[i] = idx + 1
print(alphabet_dict)
-> {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5, 'f': 6, 'g': 7, 'h': 8, 'i': 9, 'j': 10, 'k': 11, 'l': 12, 'm': 13, 'n': 14, 'o': 15, 'p': 16, 'q': 17, 'r': 18, 's': 19, 't': 20, 'u': 21, 'v': 22, 'w': 23, 'x': 24, 'y': 25, 'z': 26}
딕셔너리에 있는 모든 key값 가져오기.keys()
alphabet_dict.keys()
딕셔너리에 있는 모든 value값 가져오기.values()
alphabet_dict.values()
key값으로 딕셔너리에 있는 특정 value 가져오기.get(), []
1)alphabet_dict.get('a')
2)alphabet_dict['a]
def solution(s):
answer = ''
result = ''
dict1 = {'0':'zero','1':'one','2':"two",'3':'three','4':'four',
'5':'five','6':'six','7':'seven','8':'eight','9':'nine'}
dict2 = {v:k for k, v in dict1.items()}
for i in s:
if i.isalpha():
answer += i
if answer in dict2.keys():
result += dict2[answer]
answer = ''
else:
continue
else:
result += i
return int(result)
num_dic = {"zero":"0", "one":"1", "two":"2", "three":"3", "four":"4", "five":"5", "six":"6", "seven":"7", "eight":"8", "nine":"9"}
def solution(s):
answer = s
for key, value in num_dic.items():
answer = answer.replace(key, value)
return int(answer)
'Programmers' 카테고리의 다른 글
10진수를 2진수로 바꾸기 bin() 사용. rjust 문자열 정렬 사용하기. (0) | 2024.01.24 |
---|---|
[프로그래머스]유클리드 알고리즘으로 최대공약수 구하기. feat 재귀 (0) | 2024.01.22 |
[프로그래머스]정수를 나선형으로 배치하기 feat 2차원 배열 선언하여 풀기 (0) | 2024.01.22 |
Dictionary 내부에서 value가 가장 큰 것 찾기! (0) | 2024.01.22 |
[프로그래머스]주사위게임3 value의 최대값으로 key값 구하기 (0) | 2024.01.22 |