Nếu bạn cần làm điều này rất nhiều, bạn có thể xử lý trước tất cả các chuỗi có thể có trong số giây trong một ngày:
try:
from itertools import product
except ImportError:
def product[*seqs]:
if len[seqs] == 1:
for p in seqs[0]:
yield p,
else:
for s in seqs[0]:
for p in product[*seqs[1:]]:
yield [s,] + p
hhmmss = []
for [h, m, s] in product[range[24], range[60], range[60]]:
hhmmss.append["%02d:%02d:%02d" % [h, m, s]]
Bây giờ chuyển đổi giây sang chuỗi định dạng là một tra cứu được lập chỉ mục nhanh:
print hhmmss[12345]
bản in
'03:25:45'
EDIT:
Cập nhật đến năm 2020, loại bỏ sự xấu xí tương thích PY2 và dây F!
import sys
from itertools import product
hhmmss = [f"{h:02d}:{m:02d}:{s:02d}"
for h, m, s in product[range[24], range[60], range[60]]]
# we can still just index into the list, but define as a function
# for common API with code below
seconds_to_str = hhmmss.__getitem__
print[seconds_to_str[12345]]
Điều này mất bao nhiêu bộ nhớ?
print hhmmss[12345]
5 của một danh sách sẽ không làm được, vì nó sẽ chỉ cung cấp cho chúng tôi kích thước của danh sách và các tài liệu tham khảo STR của nó, nhưng không bao gồm bộ nhớ của chính các STR:# how big is a list of 24*60*60 8-character strs?
list_size = sys.getsizeof[hhmmss] + sum[sys.getsizeof[s] for s in hhmmss]
print["{:,}".format[list_size]]
prints:
5,657,616
Điều gì sẽ xảy ra nếu chúng ta chỉ có một str lớn? Mỗi giá trị dài chính xác là 8 ký tự, vì vậy chúng tôi có thể cắt vào STR này và nhận đúng STR cho lần thứ hai của ngày:
hhmmss_str = ''.join[[f"{h:02d}:{m:02d}:{s:02d}"
for h, m, s in product[range[24],
range[60],
range[60]]]]
def seconds_to_str[n]:
loc = n * 8
return hhmmss_str[loc: loc+8]
print[seconds_to_str[12345]]
Điều đó đã tiết kiệm bất kỳ không gian?
# how big is a str of 24*60*60*8 characters?
str_size = sys.getsizeof[hhmmss_str]
print["{:,}".format[str_size]]
prints:
691,249
Giảm xuống còn nhiều điều này:
print[str_size / list_size]
prints:
print hhmmss[12345]
0Về mặt hiệu suất, điều này trông giống như một bộ nhớ cổ điển so với sự đánh đổi CPU:
print hhmmss[12345]
1Trên máy của tôi, tôi nhận được:
print hhmmss[12345]
2Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Đọc
Bàn luận is used to convert the DateTime string to DateTime in the format of year-month-day hours minutes and seconds
Syntax:
Trong bài viết này, chúng tôi sẽ chuyển đổi chuỗi DateTime thành định dạng%y-%m-%d-%H:%m:%s. Đối với chức năng này & nbsp; Task StrptTime [] và strftime [] được sử dụng.
Strptime [] được sử dụng để chuyển đổi chuỗi DateTime thành DateTime ở định dạng của các ngày trong các ngày tháng năm mớiis used to convert DateTime into required timestamp format
Syntax::
datetime.strptime [my_date, Hồi%d-%b-%y-%h:%m:%s Hồi]
Here,
- Strftime [] được sử dụng để chuyển đổi DateTime thành định dạng dấu thời gian cần thiết
- DateTime.strftime [Hồi%Y-%M-%D-%H:%M:%Siêu]
- & Y có nghĩa là năm
- %m & nbsp; có nghĩa là tháng
- %d có nghĩa là ngày
- %H có nghĩa là giờ
%M có nghĩa là phút
%S có nghĩa là giây.Python program to convert DateTime string into %Y-%m-%d-%H:%M:%S format
Python3
Đầu tiên, thời gian sử dụng DateTime làm chuỗi. Sau đó, chuyển đổi nó thành DateTime bằng cách sử dụng strptime []. Bây giờ, hãy chuyển đổi thành định dạng cần thiết của DateTime bằng cách sử dụng Strftime
Ví dụ 1: Chương trình Python để chuyển đổi chuỗi DateTime thành%Y-%M-%D-%H:%M:%s Định dạng
print hhmmss[12345]
6 print hhmmss[12345]
7print hhmmss[12345]
8 print hhmmss[12345]
9'03:25:45'
8'03:25:45'
9import sys
from itertools import product
hhmmss = [f"{h:02d}:{m:02d}:{s:02d}"
for h, m, s in product[range[24], range[60], range[60]]]
# we can still just index into the list, but define as a function
# for common API with code below
seconds_to_str = hhmmss.__getitem__
print[seconds_to_str[12345]]
0import sys
from itertools import product
hhmmss = [f"{h:02d}:{m:02d}:{s:02d}"
for h, m, s in product[range[24], range[60], range[60]]]
# we can still just index into the list, but define as a function
# for common API with code below
seconds_to_str = hhmmss.__getitem__
print[seconds_to_str[12345]]
1
0'03:25:45'
1 '03:25:45'
2'03:25:45'
print hhmmss[12345]
3
3'03:25:45'
1 '03:25:45'
5'03:25:45'
6'03:25:45'
7 Python program to convert DateTime string into %Y-%m-%d-%H:%M:%S format'03:25:45'
Python3
Đầu ra
Ví dụ 1: Chương trình Python để chuyển đổi chuỗi DateTime thành%Y-%M-%D-%H:%M:%s Định dạng
print hhmmss[12345]
6 print hhmmss[12345]
7print hhmmss[12345]
8 print hhmmss[12345]
9'03:25:45'
8'03:25:45'
9import sys
from itertools import product
hhmmss = [f"{h:02d}:{m:02d}:{s:02d}"
for h, m, s in product[range[24], range[60], range[60]]]
# we can still just index into the list, but define as a function
# for common API with code below
seconds_to_str = hhmmss.__getitem__
print[seconds_to_str[12345]]
0import sys
from itertools import product
hhmmss = [f"{h:02d}:{m:02d}:{s:02d}"
for h, m, s in product[range[24], range[60], range[60]]]
# we can still just index into the list, but define as a function
# for common API with code below
seconds_to_str = hhmmss.__getitem__
print[seconds_to_str[12345]]
1'03:25:45'
0'03:25:45'
1 '03:25:45'
2print hhmmss[12345]
6 print hhmmss[12345]
7print hhmmss[12345]
8 print hhmmss[12345]
9'03:25:45'
8'03:25:45'
9import sys
from itertools import product
hhmmss = [f"{h:02d}:{m:02d}:{s:02d}"
for h, m, s in product[range[24], range[60], range[60]]]
# we can still just index into the list, but define as a function
# for common API with code below
seconds_to_str = hhmmss.__getitem__
print[seconds_to_str[12345]]
0import sys
from itertools import product
hhmmss = [f"{h:02d}:{m:02d}:{s:02d}"
for h, m, s in product[range[24], range[60], range[60]]]
# we can still just index into the list, but define as a function
# for common API with code below
seconds_to_str = hhmmss.__getitem__
print[seconds_to_str[12345]]
1'03:25:45'
0'03:25:45'
1 '03:25:45'
2print hhmmss[12345]
6 print hhmmss[12345]
7print hhmmss[12345]
8 print hhmmss[12345]
9'03:25:45'
8'03:25:45'
9import sys
from itertools import product
hhmmss = [f"{h:02d}:{m:02d}:{s:02d}"
for h, m, s in product[range[24], range[60], range[60]]]
# we can still just index into the list, but define as a function
# for common API with code below
seconds_to_str = hhmmss.__getitem__
print[seconds_to_str[12345]]
0import sys
from itertools import product
hhmmss = [f"{h:02d}:{m:02d}:{s:02d}"
for h, m, s in product[range[24], range[60], range[60]]]
# we can still just index into the list, but define as a function
# for common API with code below
seconds_to_str = hhmmss.__getitem__
print[seconds_to_str[12345]]
1
0'03:25:45'
1 '03:25:45'
2'03:25:45'
print hhmmss[12345]
4