1. quiz
- 문자열 my_string이 매개변수로 주어집니다. my_string에서 중복된 문자를 제거하고 하나의 문자만 남긴 문자열을 return하도록 solution 함수를 완성해주세요.
2. answer
def solution(my_string):
return ''.join(dict.fromkeys(my_string))
3. 다른 사람의 풀이
def solution(my_string):
answer = ''
for i in my_string:
if i not in answer:
answer+=i
return answer
4. dict.fromkeys(key,value)
- key : Required. An iterable specifying the keys of the new dictionary
- value : Optional. The value for all keys. Default value is None.
x = ('key1', 'key2', 'key3')
thisdict = dict.fromkeys(x,10)
print(thisdict)
{'key1': 10, 'key2': 10, 'key3': 10}
- 위와 같이 key, value 값을 부여한 딕셔너리 값을 만들 수 있다.
- 또는, 리스트의 중복을 제거할 때 사용 가능하다.