Hướng dẫn how to convert seconds to minutes in python - cách chuyển đổi giây thành phút trong python

Cải thiện bài viết

Lưu bài viết

Cho một số nguyên n [tính bằng giây], chuyển đổi thành giờ, phút và giây. Ví dụ:Examples:

Input : 12345
Output : 3:25:45

Input : 3600
Output : 1:00:00

Cách tiếp cận số 1: Naive Cách tiếp cận này chỉ đơn giản là một cách tiếp cận ngây thơ để có được giờ, phút và giây bằng các tính toán toán học đơn giản. & NBSP;Naive This approach is simply a naive approach to get the hours, minutes and seconds by simple mathematical calculations. 

Python3

def convert[seconds]:

    seconds = seconds %

    

def sec2time[sec, n_msec=3]:
    ''' Convert seconds to 'D days, HH:MM:SS.FFF' '''
    if hasattr[sec,'__len__']:
        return [sec2time[s] for s in sec]
    m, s = divmod[sec, 60]
    h, m = divmod[m, 60]
    d, h = divmod[h, 24]
    if n_msec > 0:
        pattern = '%%02d:%%02d:%%0%d.%df' % [n_msec+3, n_msec]
    else:
        pattern = r'%02d:%02d:%02d'
    if d == 0:
        return pattern % [h, m, s]
    return ['%d days, ' + pattern] % [d, h, m, s]
6= seconds
def sec2time[sec, n_msec=3]:
    ''' Convert seconds to 'D days, HH:MM:SS.FFF' '''
    if hasattr[sec,'__len__']:
        return [sec2time[s] for s in sec]
    m, s = divmod[sec, 60]
    h, m = divmod[m, 60]
    d, h = divmod[h, 24]
    if n_msec > 0:
        pattern = '%%02d:%%02d:%%0%d.%df' % [n_msec+3, n_msec]
    else:
        pattern = r'%02d:%02d:%02d'
    if d == 0:
        return pattern % [h, m, s]
    return ['%d days, ' + pattern] % [d, h, m, s]
9
def sec2time[sec, n_msec=3]:
    ''' Convert seconds to 'D days, HH:MM:SS.FFF' '''
    if hasattr[sec,'__len__']:
        return [sec2time[s] for s in sec]
    m, s = divmod[sec, 60]
    h, m = divmod[m, 60]
    d, h = divmod[h, 24]
    if n_msec > 0:
        pattern = '%%02d:%%02d:%%0%d.%df' % [n_msec+3, n_msec]
    else:
        pattern = r'%02d:%02d:%02d'
    if d == 0:
        return pattern % [h, m, s]
    return ['%d days, ' + pattern] % [d, h, m, s]
9
def sec2time[sec, n_msec=3]:
    ''' Convert seconds to 'D days, HH:MM:SS.FFF' '''
    if hasattr[sec,'__len__']:
        return [sec2time[s] for s in sec]
    m, s = divmod[sec, 60]
    h, m = divmod[m, 60]
    d, h = divmod[h, 24]
    if n_msec > 0:
        pattern = '%%02d:%%02d:%%0%d.%df' % [n_msec+3, n_msec]
    else:
        pattern = r'%02d:%02d:%02d'
    if d == 0:
        return pattern % [h, m, s]
    return ['%d days, ' + pattern] % [d, h, m, s]
3

    seconds %__

def sec2time[sec, n_msec=3]:
    ''' Convert seconds to 'D days, HH:MM:SS.FFF' '''
    if hasattr[sec,'__len__']:
        return [sec2time[s] for s in sec]
    m, s = divmod[sec, 60]
    h, m = divmod[m, 60]
    d, h = divmod[h, 24]
    if n_msec > 0:
        pattern = '%%02d:%%02d:%%0%d.%df' % [n_msec+3, n_msec]
    else:
        pattern = r'%02d:%02d:%02d'
    if d == 0:
        return pattern % [h, m, s]
    return ['%d days, ' + pattern] % [d, h, m, s]
3

    

$ sec2time[10, 3]
Out: '00:00:10.000'

$ sec2time[1234567.8910, 0]
Out: '14 days, 06:56:07'

$ sec2time[1234567.8910, 4]
Out: '14 days, 06:56:07.8910'

$ sec2time[[12, 345678.9], 3]
Out: ['00:00:12.000', '4 days, 00:01:18.900']
8= seconds
def sec2time[sec, n_msec=3]:
    ''' Convert seconds to 'D days, HH:MM:SS.FFF' '''
    if hasattr[sec,'__len__']:
        return [sec2time[s] for s in sec]
    m, s = divmod[sec, 60]
    h, m = divmod[m, 60]
    d, h = divmod[h, 24]
    if n_msec > 0:
        pattern = '%%02d:%%02d:%%0%d.%df' % [n_msec+3, n_msec]
    else:
        pattern = r'%02d:%02d:%02d'
    if d == 0:
        return pattern % [h, m, s]
    return ['%d days, ' + pattern] % [d, h, m, s]
9
def sec2time[sec, n_msec=3]:
    ''' Convert seconds to 'D days, HH:MM:SS.FFF' '''
    if hasattr[sec,'__len__']:
        return [sec2time[s] for s in sec]
    m, s = divmod[sec, 60]
    h, m = divmod[m, 60]
    d, h = divmod[h, 24]
    if n_msec > 0:
        pattern = '%%02d:%%02d:%%0%d.%df' % [n_msec+3, n_msec]
    else:
        pattern = r'%02d:%02d:%02d'
    if d == 0:
        return pattern % [h, m, s]
    return ['%d days, ' + pattern] % [d, h, m, s]
9 def3

    seconds %= def3

    convert[seconds]:0 convert[seconds]:1 % convert[seconds]:3

convert[seconds]:4= convert[seconds]:6

convert[seconds]:7convert[seconds]:8

Độ phức tạp về thời gian: O [1] Không gian phụ trợ: O [1]O[1]
Auxiliary Space: O[1]

& nbsp; Cách tiếp cận số 2: Thay thế cho cách tiếp cận ngây thơ bằng cách sử dụng hàm divmod [], chỉ có một bộ phận duy nhất để tạo ra cả thương số và phần còn lại, bạn có thể có kết quả rất nhanh chỉ với hai thao tác toán học. & NBSP;Approach #2 : Alternate to the Naive approach By using the divmod[] function, which does only a single division to produce both the quotient and the remainder, you can have the result very quickly with only two mathematical operations. 

Python3

def convert[seconds]:

    seconds = seconds %

    

def sec2time[sec, n_msec=3]:
    ''' Convert seconds to 'D days, HH:MM:SS.FFF' '''
    if hasattr[sec,'__len__']:
        return [sec2time[s] for s in sec]
    m, s = divmod[sec, 60]
    h, m = divmod[m, 60]
    d, h = divmod[h, 24]
    if n_msec > 0:
        pattern = '%%02d:%%02d:%%0%d.%df' % [n_msec+3, n_msec]
    else:
        pattern = r'%02d:%02d:%02d'
    if d == 0:
        return pattern % [h, m, s]
    return ['%d days, ' + pattern] % [d, h, m, s]
6= seconds
def sec2time[sec, n_msec=3]:
    ''' Convert seconds to 'D days, HH:MM:SS.FFF' '''
    if hasattr[sec,'__len__']:
        return [sec2time[s] for s in sec]
    m, s = divmod[sec, 60]
    h, m = divmod[m, 60]
    d, h = divmod[h, 24]
    if n_msec > 0:
        pattern = '%%02d:%%02d:%%0%d.%df' % [n_msec+3, n_msec]
    else:
        pattern = r'%02d:%02d:%02d'
    if d == 0:
        return pattern % [h, m, s]
    return ['%d days, ' + pattern] % [d, h, m, s]
9
def sec2time[sec, n_msec=3]:
    ''' Convert seconds to 'D days, HH:MM:SS.FFF' '''
    if hasattr[sec,'__len__']:
        return [sec2time[s] for s in sec]
    m, s = divmod[sec, 60]
    h, m = divmod[m, 60]
    d, h = divmod[h, 24]
    if n_msec > 0:
        pattern = '%%02d:%%02d:%%0%d.%df' % [n_msec+3, n_msec]
    else:
        pattern = r'%02d:%02d:%02d'
    if d == 0:
        return pattern % [h, m, s]
    return ['%d days, ' + pattern] % [d, h, m, s]
9
def sec2time[sec, n_msec=3]:
    ''' Convert seconds to 'D days, HH:MM:SS.FFF' '''
    if hasattr[sec,'__len__']:
        return [sec2time[s] for s in sec]
    m, s = divmod[sec, 60]
    h, m = divmod[m, 60]
    d, h = divmod[h, 24]
    if n_msec > 0:
        pattern = '%%02d:%%02d:%%0%d.%df' % [n_msec+3, n_msec]
    else:
        pattern = r'%02d:%02d:%02d'
    if d == 0:
        return pattern % [h, m, s]
    return ['%d days, ' + pattern] % [d, h, m, s]
3

    seconds %__

def sec2time[sec, n_msec=3]:
    ''' Convert seconds to 'D days, HH:MM:SS.FFF' '''
    if hasattr[sec,'__len__']:
        return [sec2time[s] for s in sec]
    m, s = divmod[sec, 60]
    h, m = divmod[m, 60]
    d, h = divmod[h, 24]
    if n_msec > 0:
        pattern = '%%02d:%%02d:%%0%d.%df' % [n_msec+3, n_msec]
    else:
        pattern = r'%02d:%02d:%02d'
    if d == 0:
        return pattern % [h, m, s]
    return ['%d days, ' + pattern] % [d, h, m, s]
3

convert[seconds]:4= convert[seconds]:6

convert[seconds]:7convert[seconds]:8

Độ phức tạp về thời gian: O [1] Không gian phụ trợ: O [1]O[1]
Auxiliary Space: O[1]

& nbsp; Cách tiếp cận số 2: Thay thế cho cách tiếp cận ngây thơ bằng cách sử dụng hàm divmod [], chỉ có một bộ phận duy nhất để tạo ra cả thương số và phần còn lại, bạn có thể có kết quả rất nhanh chỉ với hai thao tác toán học. & NBSP;Approach #3 : Using timedelta [Object of datetime module] Datetime module provides timedelta object which represents a duration, the difference between two dates or times. datetime.timedelta can be used to represent seconds into hours, minutes and seconds format. 

Python3

Các

    seconds 0    2 =     5

def sec2time[sec, n_msec=3]:
    ''' Convert seconds to 'D days, HH:MM:SS.FFF' '''
    if hasattr[sec,'__len__']:
        return [sec2time[s] for s in sec]
    m, s = divmod[sec, 60]
    h, m = divmod[m, 60]
    d, h = divmod[h, 24]
    if n_msec > 0:
        pattern = '%%02d:%%02d:%%0%d.%df' % [n_msec+3, n_msec]
    else:
        pattern = r'%02d:%02d:%02d'
    if d == 0:
        return pattern % [h, m, s]
    return ['%d days, ' + pattern] % [d, h, m, s]
0    22.

    convert[seconds]:0 =1 % =3    22

convert[seconds]:4= convert[seconds]:6

convert[seconds]:7convert[seconds]:8

Độ phức tạp về thời gian: O [1] Không gian phụ trợ: O [1]O[1]
Auxiliary Space: O[1]

& nbsp; Cách tiếp cận số 2: Thay thế cho cách tiếp cận ngây thơ bằng cách sử dụng hàm divmod [], chỉ có một bộ phận duy nhất để tạo ra cả thương số và phần còn lại, bạn có thể có kết quả rất nhanh chỉ với hai thao tác toán học. & NBSP;Approach #4 : Using time.strftime[] time.strftime[] gives more control over formatting. The format and time.gmtime[] is passed as argument. gmtime is used to convert seconds to special tuple format that strftime[] requires. 

Python3

Các

def convert[seconds]:

    seconds = seconds %

convert[seconds]:4= convert[seconds]:6

convert[seconds]:7convert[seconds]:8

Độ phức tạp về thời gian: O [1] Không gian phụ trợ: O [1]O[1]
Auxiliary Space: O[1]


& nbsp; Cách tiếp cận số 2: Thay thế cho cách tiếp cận ngây thơ bằng cách sử dụng hàm divmod [], chỉ có một bộ phận duy nhất để tạo ra cả thương số và phần còn lại, bạn có thể có kết quả rất nhanh chỉ với hai thao tác toán học. & NBSP;

def sec2time[sec, n_msec=3]:
    ''' Convert seconds to 'D days, HH:MM:SS.FFF' '''
    if hasattr[sec,'__len__']:
        return [sec2time[s] for s in sec]
    m, s = divmod[sec, 60]
    h, m = divmod[m, 60]
    d, h = divmod[h, 24]
    if n_msec > 0:
        pattern = '%%02d:%%02d:%%0%d.%df' % [n_msec+3, n_msec]
    else:
        pattern = r'%02d:%02d:%02d'
    if d == 0:
        return pattern % [h, m, s]
    return ['%d days, ' + pattern] % [d, h, m, s]

Các

$ sec2time[10, 3]
Out: '00:00:10.000'

$ sec2time[1234567.8910, 0]
Out: '14 days, 06:56:07'

$ sec2time[1234567.8910, 4]
Out: '14 days, 06:56:07.8910'

$ sec2time[[12, 345678.9], 3]
Out: ['00:00:12.000', '4 days, 00:01:18.900']

Bài Viết Liên Quan

Chủ Đề