Một tùy chọn thay thế ________ 8-vấn đề kiểu chữ với repeat[]
, ____10 và
>>> import re
>>> n = 2
>>> re.sub[".", lambda x: x.group[] * n, s] # or re.sub['[.]', r'\1' * n, s] - thanks Eduardo
'112233aabbcc'
1:>>> from itertools import repeat, izip, chain
>>> "".join[chain[*izip[*repeat[s, 2]]]]
'112233aabbcc'
>>> "".join[chain[*izip[*repeat[s, 3]]]]
'111222333aaabbbccc'
Hoặc, "Tôi biết regexes và tôi sẽ sử dụng nó cho mọi thứ"-tùy chọn kiểu dáng:
>>> import re
>>> n = 2
>>> re.sub[".", lambda x: x.group[] * n, s] # or re.sub['[.]', r'\1' * n, s] - thanks Eduardo
'112233aabbcc'
Tất nhiên, đừng sử dụng các giải pháp này trong thực tế.
0txttxttxtt
gegege
0
2txttxttxtt
gegege
2gegege
3gegege
4- Lặp lại chuỗi ở độ dài với chức năng do người dùng xác định trong Python
Độ phức tạp về thời gian: O [N] Không gian phụ trợ: O [N]
txttxttxtt
0gegege
0txttxttxtt
2 gegege
2gegege
3gegege
4
txttxttxtt
txttxttxtt
Độ phức tạp về thời gian: O [N] Không gian phụ trợ: O [N]
text = "txt"
repeated = text * 4
print[repeated]
Output:
txttxttxttxt
Trong mã ở trên, chúng tôi đã tạo một biến chuỗi
>>> import re
>>> n = 2
>>> re.sub[".", lambda x: x.group[] * n, s] # or re.sub['[.]', r'\1' * n, s] - thanks Eduardo
'112233aabbcc'
6, lặp lại nó >>> import re
>>> n = 2
>>> re.sub[".", lambda x: x.group[] * n, s] # or re.sub['[.]', r'\1' * n, s] - thanks Eduardo
'112233aabbcc'
7 lần và lưu trữ chuỗi lặp lại bên trong biến chuỗi mới >>> import re
>>> n = 2
>>> re.sub[".", lambda x: x.group[] * n, s] # or re.sub['[.]', r'\1' * n, s] - thanks Eduardo
'112233aabbcc'
8. Cuối cùng, chúng tôi đã hiển thị giá trị của biến >>> import re
>>> n = 2
>>> re.sub[".", lambda x: x.group[] * n, s] # or re.sub['[.]', r'\1' * n, s] - thanks Eduardo
'112233aabbcc'
8 cho người dùng.Phương pháp này thuận tiện khi chúng ta muốn lặp lại toàn bộ chuỗi
text = "txt"
repeated = text * 4
print[repeated]
0 lần, như được hiển thị trong đầu ra text = "txt"
repeated = text * 4
print[repeated]
1. Nhưng nếu chúng tôi sẽ lặp lại một chuỗi ở một độ dài nhất định, chúng tôi phải viết triển khai của chúng tôi. Ví dụ: nếu độ dài được chỉ định là text = "txt"
repeated = text * 4
print[repeated]
2 ký tự, chuỗi trên sẽ trông giống như text = "txt"
repeated = text * 4
print[repeated]
3.Lặp lại chuỗi ở độ dài với chức năng do người dùng xác định trong Python
Phương pháp trước đó không thành công nếu chúng ta muốn lặp lại một chuỗi nhưng cũng ở trong giới hạn ký tự. Trong Python, không có phương pháp tích hợp nào cho nó, vì vậy chúng tôi phải thực hiện logic của riêng mình bên trong một hàm. Ví dụ mã bên dưới cho thấy cách lặp lại một chuỗi ở một độ dài nhất định với hàm do người dùng xác định.
def repeat[string_to_repeat, length]:
multiple = int[length/len[string_to_repeat] + 1]
repeated_string = string_to_repeat * multiple
return repeated_string[:length]
r = repeat["txt", 10]
print[r]
Output:
txttxttxtt
Lần này, chúng tôi đã lặp lại chuỗi
text = "txt"
repeated = text * 4
print[repeated]
4 với độ dài text = "txt"
repeated = text * 4
print[repeated]
2. Chúng tôi đã viết hàm repeat[]
lấy chuỗi gốc text = "txt"
repeated = text * 4
print[repeated]
7 và độ dài của chuỗi lặp lại text = "txt"
repeated = text * 4
print[repeated]
8 làm tham số đầu vào. Sau đó, chúng tôi đã khởi tạo biến số nguyên text = "txt"
repeated = text * 4
print[repeated]
9 xác định số lần chuỗi ban đầu cần được lặp lại để phù hợp với giới hạn độ dài. Điều này có thể được xác định bằng cách chia tham số text = "txt"
repeated = text * 4
print[repeated]
8 cho độ dài thực của tham số text = "txt"
repeated = text * 4
print[repeated]
7. Chúng tôi đã thêm txttxttxttxt
2 để bù cho các giá trị bị mất sau điểm thập phân. Sau đó, chúng tôi đã lưu trữ một chuỗi lặp lại bên trong biến txttxttxttxt
3 bằng cách nhân text = "txt"
repeated = text * 4
print[repeated]
7 với biến text = "txt"
repeated = text * 4
print[repeated]
9. Cuối cùng, chúng tôi đã trả lại các giá trị bên trong txttxttxttxt
3 từ txttxttxttxt
7 cho chỉ số text = "txt"
repeated = text * 4
print[repeated]
8.Chúng tôi đã sử dụng hàm repeat[]
để lặp lại chuỗi
text = "txt"
repeated = text * 4
print[repeated]
4 với độ dài text = "txt"
repeated = text * 4
print[repeated]
2 và hiển thị đầu ra. Đầu ra cho thấy text = "txt"
repeated = text * 4
print[repeated]
3, đó là những gì chúng ta đã thảo luận trong phần trước. Cải thiện bài viết
Lưu bài viết
Cải thiện bài viết
Lưu bài viết
Đọc
Bàn luận
- Trong bài viết này, nhiệm vụ là viết một chương trình Python để lặp lại các ký tự m của chuỗi n lần.
- Phương pháp 1:
- Bây giờ lưu trữ các ký tự cần được lặp lại vào một chuỗi có tên lặp lại_string bằng cách cắt lát.
- Nhân lặp lại_string với n.
- Bây giờ in chuỗi.
- Bây giờ in chuỗi.
3 def repeat[string_to_repeat, length]:
multiple = int[length/len[string_to_repeat] + 1]
repeated_string = string_to_repeat * multiple
return repeated_string[:length]
r = repeat["txt", 10]
print[r]
4def repeat[string_to_repeat, length]:
multiple = int[length/len[string_to_repeat] + 1]
repeated_string = string_to_repeat * multiple
return repeated_string[:length]
r = repeat["txt", 10]
print[r]
Python3
def repeat[string_to_repeat, length]:
multiple = int[length/len[string_to_repeat] + 1]
repeated_string = string_to_repeat * multiple
return repeated_string[:length]
r = repeat["txt", 10]
print[r]
3 def repeat[string_to_repeat, length]:
multiple = int[length/len[string_to_repeat] + 1]
repeated_string = string_to_repeat * multiple
return repeated_string[:length]
r = repeat["txt", 10]
print[r]
4txttxttxtt
0txttxttxtt
1txttxttxtt
2 def repeat[string_to_repeat, length]:
multiple = int[length/len[string_to_repeat] + 1]
repeated_string = string_to_repeat * multiple
return repeated_string[:length]
r = repeat["txt", 10]
print[r]
8txttxttxtt
4txttxttxtt
0txttxttxtt
1txttxttxtt
2 def repeat[string_to_repeat, length]:
multiple = int[length/len[string_to_repeat] + 1]
repeated_string = string_to_repeat * multiple
return repeated_string[:length]
r = repeat["txt", 10]
print[r]
8txttxttxtt
4def repeat[string_to_repeat, length]:
multiple = int[length/len[string_to_repeat] + 1]
repeated_string = string_to_repeat * multiple
return repeated_string[:length]
r = repeat["txt", 10]
print[r]
5txttxttxtt
6txttxttxtt
2 txttxttxtt
8Độ phức tạp về thời gian và không gian cho cả hai phương pháp đều giống nhau:
Độ phức tạp về thời gian: O [N] Không gian phụ trợ: O [N]
Cải thiện bài viết
def repeat[string_to_repeat, length]:
multiple = int[length/len[string_to_repeat] + 1]
repeated_string = string_to_repeat * multiple
return repeated_string[:length]
r = repeat["txt", 10]
print[r]
5gegege6
gegege7
gegege8
gegege9
itertools
0itertools
1itertools
0itertools
3itertools
4Output:
gegege
Độ phức tạp về thời gian: O [N] Không gian phụ trợ: O [N]O[n]
Auxiliary Space : O[n]
Phương pháp 2:
- Xác định một hàm sẽ lấy một từ, m, n giá trị làm đối số.
- Nếu m lớn hơn chiều dài của từ. Đặt giá trị m bằng độ dài của từ
- Bây giờ lưu trữ các ký tự cần được lặp lại vào một chuỗi có tên lặp lại_string bằng cách cắt lát.
- Nhân lặp lại_string với n.
- Bây giờ in chuỗi.
Python3
def repeat[string_to_repeat, length]:
multiple = int[length/len[string_to_repeat] + 1]
repeated_string = string_to_repeat * multiple
return repeated_string[:length]
r = repeat["txt", 10]
print[r]
3 def repeat[string_to_repeat, length]:
multiple = int[length/len[string_to_repeat] + 1]
repeated_string = string_to_repeat * multiple
return repeated_string[:length]
r = repeat["txt", 10]
print[r]
4def repeat[string_to_repeat, length]:
multiple = int[length/len[string_to_repeat] + 1]
repeated_string = string_to_repeat * multiple
return repeated_string[:length]
r = repeat["txt", 10]
print[r]
5def repeat[string_to_repeat, length]:
multiple = int[length/len[string_to_repeat] + 1]
repeated_string = string_to_repeat * multiple
return repeated_string[:length]
r = repeat["txt", 10]
print[r]
6def repeat[string_to_repeat, length]:
multiple = int[length/len[string_to_repeat] + 1]
repeated_string = string_to_repeat * multiple
return repeated_string[:length]
r = repeat["txt", 10]
print[r]
7def repeat[string_to_repeat, length]:
multiple = int[length/len[string_to_repeat] + 1]
repeated_string = string_to_repeat * multiple
return repeated_string[:length]
r = repeat["txt", 10]
print[r]
8def repeat[string_to_repeat, length]:
multiple = int[length/len[string_to_repeat] + 1]
repeated_string = string_to_repeat * multiple
return repeated_string[:length]
r = repeat["txt", 10]
print[r]
9txttxttxtt
0txttxttxtt
1txttxttxtt
2 def repeat[string_to_repeat, length]:
multiple = int[length/len[string_to_repeat] + 1]
repeated_string = string_to_repeat * multiple
return repeated_string[:length]
r = repeat["txt", 10]
print[r]
8txttxttxtt
4def repeat[string_to_repeat, length]:
multiple = int[length/len[string_to_repeat] + 1]
repeated_string = string_to_repeat * multiple
return repeated_string[:length]
r = repeat["txt", 10]
print[r]
5txttxttxtt
6txttxttxtt
2 txttxttxtt
8def repeat[string_to_repeat, length]:
multiple = int[length/len[string_to_repeat] + 1]
repeated_string = string_to_repeat * multiple
return repeated_string[:length]
r = repeat["txt", 10]
print[r]
5gegege6
>>> import re
>>> n = 2
>>> re.sub[".", lambda x: x.group[] * n, s] # or re.sub['[.]', r'\1' * n, s] - thanks Eduardo
'112233aabbcc'
03>>> import re
>>> n = 2
>>> re.sub[".", lambda x: x.group[] * n, s] # or re.sub['[.]', r'\1' * n, s] - thanks Eduardo
'112233aabbcc'
2>>> import re
>>> n = 2
>>> re.sub[".", lambda x: x.group[] * n, s] # or re.sub['[.]', r'\1' * n, s] - thanks Eduardo
'112233aabbcc'
05gegege8
gegege9
itertools
0itertools
1itertools
0itertools
3itertools
4Output:
gegege
Độ phức tạp về thời gian và không gian cho cả hai phương pháp đều giống nhau:
Độ phức tạp về thời gian: O [N] Không gian phụ trợ: O [N]O[n]
Auxiliary Space : O[n]