[Python] int값을 각 자리수로 이루어진 list로 바꾸는 방법 ☑️

2022. 10. 20. 20:21·Python

<첫번째 방법>

 

def convert(n):
    origin = n
    values = []
    while True:
        values.append(n%10)
        n = (n // 10)
        if n in range(10):
            values.append(n)
            break
    return values




<두번째 방법>

tmp = b
nums = [0,0,0]

for i in range(3):
    num = tmp
    cnt = 0
    while num//10 != 0:
        num = num//10
        cnt = cnt+1
    nums[cnt] = num
    tmp = tmp - pow(10,cnt)*num




 

 

<세번째 방법>
string과 list를 활용하는 방법 : int -> string -> list

n = int(input())
def intToList(n):
    a = str(n); b = [] 
    for x in a: b.append(x)
    return b
print(intToList(n))
'Python' 카테고리의 다른 글
  • [Python] Collections 모듈 ☑️
  • [Python] 분수를 표현하는 방법 (Fraction) ☑️
  • [Python] 문자열 관련 메서드 ☑️
  • [Python] 문자열 자르기, 붙이기 ☑️
vysryoo
vysryoo
  • vysryoo
    vysryoo
    vysryoo
  • 전체
    오늘
    어제
    • 분류 전체보기 (129)
      • Python (20)
      • Data structure (12)
      • Algorithm (14)
      • Operating system (18)
      • Programming language theory (12)
      • Computer architecture (6)
      • Softeware engineering (8)
      • Multicore (2)
      • Data Base (3)
      • Problem solving (24)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
vysryoo
[Python] int값을 각 자리수로 이루어진 list로 바꾸는 방법 ☑️
상단으로

티스토리툴바