Hướng dẫn python todigit - con trăn

Giả sử các chuỗi không bị cấm nghiêm ngặt, đây là một số giải pháp thay thế có thể.

def get_digits(num):
    return [int(i) for i in str(num)]

Một giải pháp khác [1]:

import math

def get_digits(num):
    return [(num//(10**i))%10 for i in range(math.ceil(math.log(num, 10))-1, -1, -1)]

Điều này sẽ là tối ưu nếu bạn chỉ cần in trực tiếp các chữ số.

print(", ".join([i for i in str(num)]))

Nếu bạn đang ở lớp Python bắt đầu, giáo sư của bạn có thể chỉ muốn thấy một cái gì đó như thế này không sử dụng danh sách hoặc chỉ mục chuỗi:

num = 1203

for i, n in enumerate(str(num)):
    if i == 0:
        print(n, end="")
    else:
        print(', ' + n, end='')

[Out]: 1, 2, 0, 3

Tôi muốn viết một hàm đơn giản in các chữ số của một số nguyên ngược. Tôi nghĩ rằng nó có thể được thực hiện với vòng lặp trong khi tôi không chắc chắn. Tôi đã thử điều này, nhưng nó không hoạt động.
I think it could be done with the while loop, but I am not so sure.
I have tried this, but it didn't work.

Mở rộng | Chọn | Wrap | Số dòng|Select|Wrap|Line Numbers

  1. def & nbsp; print_digits (n):
  2. & nbsp; & nbsp; & nbsp; & nbsp; n & nbsp; = & nbsp; abs (n)
  3. & nbsp; & nbsp; & nbsp; & nbsp; while & nbsp; n & nbsp;> & nbsp; 0:
  4. & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;
  5. & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;
  6.  

23 tháng 7 '08 #1

Hướng dẫn python todigit - con trăn

Trong Python % là toán tử modulo ... Tôi không chắc bạn đang thử gì ở đây ...

Đây là cách đảo ngược các chữ số của số nguyên bằng cách sử dụng danh sách hiểu:

Mở rộng | Chọn | Wrap | Số dòng|Select|Wrap|Line Numbers

  1. >>> def print_digits(n):
  2. def & nbsp; print_digits (n):
  3. & nbsp; & nbsp; & nbsp; & nbsp; n & nbsp; = & nbsp; abs (n)
  4. & nbsp; & nbsp; & nbsp; & nbsp; while & nbsp; n & nbsp;> & nbsp; 0:
  5. & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;
  6. & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;
  7. 631
  8. 23 tháng 7 '08 #1

Trong Python % là toán tử modulo ... Tôi không chắc bạn đang thử gì ở đây ...

Đây là cách đảo ngược các chữ số của số nguyên bằng cách sử dụng danh sách hiểu:

Mở rộng | Chọn | Wrap | Số dòng|Select|Wrap|Line Numbers

  1. def & nbsp; print_digits (n):
  2. & nbsp; & nbsp; & nbsp; & nbsp; n & nbsp; = & nbsp; abs (n)
  3. & nbsp; & nbsp; & nbsp; & nbsp; while & nbsp; n & nbsp;> & nbsp; 0:
  4. & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;
  5. & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;
  6. & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;
  7. 23 tháng 7 '08 #1
  8. 23 tháng 7 '08 #1

Trong Python % là toán tử modulo ... Tôi không chắc bạn đang thử gì ở đây ...

Đây là cách đảo ngược các chữ số của số nguyên bằng cách sử dụng danh sách hiểu:

... & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; new_n & nbsp; = & nbsp;
http://openbookproject.net/thinkCSpy/ch06.xhtml#auto16
so the exercise is number 8

Mở rộng | Chọn | Wrap | Số dòng|Select|Wrap|Line Numbers

  1. def & nbsp; print_digits (n):
  2. & nbsp; & nbsp; & nbsp; & nbsp; n & nbsp; = & nbsp; abs (n)
  3. & nbsp; & nbsp; & nbsp; & nbsp; while & nbsp; n & nbsp;> & nbsp; 0:
  4. & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;
  5. & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;
  6. 23 tháng 7 '08 #1
  7. Trong Python % là toán tử modulo ... Tôi không chắc bạn đang thử gì ở đây ...
  8. Đây là cách đảo ngược các chữ số của số nguyên bằng cách sử dụng danh sách hiểu:
  9. & nbsp; & nbsp; & nbsp; & nbsp; n & nbsp; = & nbsp; abs (n)
  10.  

& nbsp; & nbsp; & nbsp; & nbsp; while & nbsp; n & nbsp;> & nbsp; 0:

& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;

& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;

23 tháng 7 '08 #1

Trong Python % là toán tử modulo ... Tôi không chắc bạn đang thử gì ở đây ...

Đây là cách đảo ngược các chữ số của số nguyên bằng cách sử dụng danh sách hiểu:

... & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; new_n & nbsp; = & nbsp;

... & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; new_n.reverse ()

  • ... & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; print & nbsp; ''.
  • ... & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;
  • Đây là cách đảo ngược các chữ số của số nguyên bằng cách sử dụng danh sách hiểu:

    ... & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; new_n & nbsp; = & nbsp;

    ... & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; new_n.reverse ()

    ... & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; print & nbsp; ''.N as the input. The program must print the desired pattern as shown in the example input/ output. Examples:

    ... & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; 41325 Output : |**** |* |*** |** |***** Explanation: for a given integer print the number of *’s that are equivalent to each digit in the integer. Here the first digit is 4 so print four *sin the first line. The second digit is 1 so print one *. So on and the last i.e., the fifth digit is 5 hence print five *s in the fifth line. Input : 60710 Output : |****** | |******* |* |

    >>> & nbsp; print_digits (136) Read the input For each digit in the integer print the corresponding number of *s If the digit is 0 then print no *s and skip to the next line 

    Python3

    >>> & nbsp;

    23 tháng 7 '08 #2

    Oh chờ đã .. bây giờ tôi thấy những gì bạn đang cố gắng làm ...

    >>> & nbsp; def & nbsp; print_n (n):

    ... & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; while & nbsp;

    num = 1203
    
    for i, n in enumerate(str(num)):
        if i == 0:
            print(n, end="")
        else:
            print(', ' + n, end='')
    
    [Out]: 1, 2, 0, 3
    
    0

    Output:

    |****
    |*
    |***
    |**
    |*****

    ... & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;O(n) since one traversal of the array is required to complete all operations hence the overall time required by the algorithm is linear
    Auxiliary Space: O(1) since no extra array is used so the space taken by the algorithm is constant

    ... & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; 

    Python3

    >>> & nbsp; print_n (492)

    2 & nbsp; 9 & nbsp; 4

    Mô đun 10 sẽ cung cấp cho bạn phần còn lại nhưng bạn muốn phân chia số nguyên cho 10 lần mỗi lần để giảm số chữ số khác.

        

    |****
    |*
    |***
    |**
    |*****
    2
    |****
    |*
    |***
    |**
    |*****
    3
    |****
    |*
    |***
    |**
    |*****
    4
    |****
    |*
    |***
    |**
    |*****
    5

    23 tháng 7 '08 #3

    Chà, tôi chưa quen với Python và tôi đang đọc điều này: http://openbookproject.net/thinkcspy/ch06.xhtml#auto16 vì vậy bài tập là số 8

        

    import math
    
    def get_digits(num):
        return [(num//(10**i))%10 for i in range(math.ceil(math.log(num, 10))-1, -1, -1)]
    
    4
    import math
    
    def get_digits(num):
        return [(num//(10**i))%10 for i in range(math.ceil(math.log(num, 10))-1, -1, -1)]
    
    5pattern(n):1pattern(n):2pattern(n):3
    print(", ".join([i for i in str(num)]))
    
    4pattern(n):5
    |****
    |*
    |***
    |**
    |*****
    5

    Output:

    |****
    |*
    |***
    |**
    |*****

    Độ phức tạp về thời gian: O (n) Vì một lần truyền của mảng là cần thiết để hoàn thành tất cả các hoạt động do đó thời gian chung được yêu cầu bởi thuật toán là không gian tuyến tính Thuật toán sẽ là tuyến tínhO(n) since one traversal of the array is required to complete all operations hence the overall time required by the algorithm is linear
    Auxiliary Space: O(n) since an extra list is used so in the worst case the space taken by the algorithm will be linear


    Làm thế nào để bạn in số chữ số trong Python?

    Hàm Len () là một hàm tích hợp trong Python được sử dụng để tính toán số lượng ký tự bên trong một biến chuỗi. Hàm Len () lấy một chuỗi làm tham số đầu vào và trả về số lượng ký tự bên trong chuỗi đó. is a built-in function in Python used to calculate the number of characters inside a string variable. The len() function takes a string as an input parameter and returns the number of characters inside that string.

    Làm cách nào để in một chữ số riêng lẻ của một số trong Python?

    Nhận được các chữ số cá nhân từ câu trả lời của mã int python..

    >>> n = 43365644 ..

    >>> chữ số = [int (x) cho x in str (n)].

    >>> chữ số ..

    >>> lst. Mở rộng (chữ số) # Sử dụng phương thức mở rộng nếu bạn muốn thêm danh sách vào một danh sách khác ..

    Làm thế nào để bạn in các chữ số của số?

    Phương pháp 1: Cách đơn giản nhất để thực hiện là trích xuất từng chữ số một và in nó ...

    Trích xuất chữ số cuối cùng của số N theo n%10 và lưu trữ chữ số đó trong một mảng (nói ARR []) ..

    Cập nhật giá trị của N bằng N/10 và lặp lại bước trên cho đến khi N không bằng 0 ..

    Làm thế nào để bạn in 10 chữ số trong Python?

    Thông tin về str.isdigit (): loại: meather_descriptor Mẫu chuỗi: không gian tên: python xây dựng tài liệu: S. isDigit () -> bool trả về true nếu tất cả các ký tự trong s là chữ số và có ít nhất một ký tự trong s, sai.str. isdigit() : Type: method_descriptor String Form: Namespace: Python builtin Docstring: S. isdigit() -> bool Return True if all characters in S are digits and there is at least one character in S, False otherwise.