Số lượng liên tiếp:
Bạn có thể sử dụng itertools.groupby
:
s = "111000222334455555"
from itertools import groupby
groups = groupby[s]
result = [[label, sum[1 for _ in group]] for label, group in groups]
Sau đó,
[["1": 3], ["0", 3], ["2", 3], ["3", 2], ["4", 2], ["5", 5]]
0 có vẻ như:[["1": 3], ["0", 3], ["2", 3], ["3", 2], ["4", 2], ["5", 5]]
Và bạn có thể định dạng với một cái gì đó như:
", ".join["{}x{}".format[label, count] for label, count in result]
# "1x3, 0x3, 2x3, 3x2, 4x2, 5x5"
Tổng số lượng:
Ai đó trong các bình luận lo ngại rằng bạn muốn tổng số số nên
[["1": 3], ["0", 3], ["2", 3], ["3", 2], ["4", 2], ["5", 5]]
1. Trong trường hợp đó, bạn muốn sử dụng [["1": 3], ["0", 3], ["2", 3], ["3", 2], ["4", 2], ["5", 5]]
2:from collections import Counter
s = "11100111"
result = Counter[s]
# {"1":6, "0":2}
Phương pháp của bạn:
Như nhiều người đã chỉ ra, phương pháp của bạn thất bại vì bạn đang lặp lại
[["1": 3], ["0", 3], ["2", 3], ["3", 2], ["4", 2], ["5", 5]]
3 nhưng giải quyết [["1": 3], ["0", 3], ["2", 3], ["3", 2], ["4", 2], ["5", 5]]
4. Điều này dẫn đến một lỗi ngoài một khi [["1": 3], ["0", 3], ["2", 3], ["3", 2], ["4", 2], ["5", 5]]
5 đang chỉ vào chỉ số cuối cùng của [["1": 3], ["0", 3], ["2", 3], ["3", 2], ["4", 2], ["5", 5]]
6, do đó, [["1": 3], ["0", 3], ["2", 3], ["3", 2], ["4", 2], ["5", 5]]
7 làm tăng [["1": 3], ["0", 3], ["2", 3], ["3", 2], ["4", 2], ["5", 5]]
8. Một cách để khắc phục điều này sẽ là lặp qua [["1": 3], ["0", 3], ["2", 3], ["3", 2], ["4", 2], ["5", 5]]
9, nhưng sẽ có nhiều pythonic hơn để tạo ra một cái gì đó để lặp lại.Đối với chuỗi không hoàn toàn lớn,
", ".join["{}x{}".format[label, count] for label, count in result]
# "1x3, 0x3, 2x3, 3x2, 4x2, 5x5"
0 không phải là vấn đề về hiệu suất, vì vậy bạn có thể làm:counts = []
count = 1
for a, b in zip[s, s[1:]]:
if a==b:
count += 1
else:
counts.append[[a, count]]
count = 1
Vấn đề duy nhất là bạn sẽ phải trường hợp nhân vật cuối cùng nếu nó là duy nhất. Có thể được sửa bằng
", ".join["{}x{}".format[label, count] for label, count in result]
# "1x3, 0x3, 2x3, 3x2, 4x2, 5x5"
1import itertools
counts = []
count = 1
for a, b in itertools.zip_longest[s, s[1:], fillvalue=None]:
if a==b:
count += 1
else:
counts.append[[a, count]]
count = 1
Nếu bạn có một chuỗi thực sự lớn và không thể chịu đựng được hai trong số chúng trong bộ nhớ tại một thời điểm, bạn có thể sử dụng công thức
", ".join["{}x{}".format[label, count] for label, count in result]
# "1x3, 0x3, 2x3, 3x2, 4x2, 5x5"
2 ", ".join["{}x{}".format[label, count] for label, count in result]
# "1x3, 0x3, 2x3, 3x2, 4x2, 5x5"
3.def pairwise[iterable]:
"""iterates pairwise without holding an extra copy of iterable in memory"""
a, b = itertools.tee[iterable]
next[b, None]
return itertools.zip_longest[a, b, fillvalue=None]
counts = []
count = 1
for a, b in pairwise[s]:
...
Làm thế nào để bạn tìm thấy số lần xuất hiện trong một chuỗi trong Python?
Chức năng Python String Count [] là một hàm sẵn có trong ngôn ngữ lập trình Python trả về số lần xuất hiện của một chuỗi con trong chuỗi đã cho.
Làm thế nào để bạn tìm thấy số lần xuất hiện trong một chuỗi trong Python?
Chức năng Python String Count [] là một hàm sẵn có trong ngôn ngữ lập trình Python trả về số lần xuất hiện của một chuỗi con trong chuỗi đã cho.
Cải thiện bài viết
Lưu bài viết : test_str = ‘geekforgeeeksss is bbbest forrr geeks’, K = 3 Output : [‘eee’, ‘sss’, ‘bbb’, ‘rrr’] Explanation : K length consecutive strings extracted. Input : test_str = ‘geekforgeeeksss is bbbest forrrr geeks’, K = 4 Output : [‘rrrr’] Explanation : K length consecutive strings extracted.
Đọc
Bàn luận
Python3
Cho một chuỗi, trích xuất tất cả các ký tự liên tiếp độ K.
Đầu vào: test_str = ‘geekforgeeekss là bbbest forrr geek, đầu ra k = 3: [‘ eee, ‘sss,‘ bbb, ‘rrr,] Giải thích: K chuỗi chiều dài liên tiếp được trích xuất. Đầu vào: test_str = geekforGeeekSSS là bbbest forrrr geeks, k = 4 đầu ra: [‘rrrr,] Giải thích: K chuỗi chiều dài liên tiếp được trích xuất.
Phương pháp số 1: Sử dụng vòng lặp
Trong đó, chúng tôi duy trì một bộ đếm để kiểm tra sự hợp nhất của các phần tử, nếu chúng chính xác bằng k trước phần tử riêng biệt, thì số lượng, tự thêm k lần k được trả về.
", ".join["{}x{}".format[label, count] for label, count in result]
# "1x3, 0x3, 2x3, 3x2, 4x2, 5x5"
4", ".join["{}x{}".format[label, count] for label, count in result]
# "1x3, 0x3, 2x3, 3x2, 4x2, 5x5"
5 ", ".join["{}x{}".format[label, count] for label, count in result]
# "1x3, 0x3, 2x3, 3x2, 4x2, 5x5"
6", ".join["{}x{}".format[label, count] for label, count in result]
# "1x3, 0x3, 2x3, 3x2, 4x2, 5x5"
7", ".join["{}x{}".format[label, count] for label, count in result]
# "1x3, 0x3, 2x3, 3x2, 4x2, 5x5"
8", ".join["{}x{}".format[label, count] for label, count in result]
# "1x3, 0x3, 2x3, 3x2, 4x2, 5x5"
9 from collections import Counter
s = "11100111"
result = Counter[s]
# {"1":6, "0":2}
0 from collections import Counter
s = "11100111"
result = Counter[s]
# {"1":6, "0":2}
1from collections import Counter
s = "11100111"
result = Counter[s]
# {"1":6, "0":2}
2from collections import Counter
s = "11100111"
result = Counter[s]
# {"1":6, "0":2}
3", ".join["{}x{}".format[label, count] for label, count in result]
# "1x3, 0x3, 2x3, 3x2, 4x2, 5x5"
5 from collections import Counter
s = "11100111"
result = Counter[s]
# {"1":6, "0":2}
5from collections import Counter
s = "11100111"
result = Counter[s]
# {"1":6, "0":2}
6", ".join["{}x{}".format[label, count] for label, count in result]
# "1x3, 0x3, 2x3, 3x2, 4x2, 5x5"
5 from collections import Counter
s = "11100111"
result = Counter[s]
# {"1":6, "0":2}
8counts = []
count = 1
for a, b in zip[s, s[1:]]:
if a==b:
count += 1
else:
counts.append[[a, count]]
count = 1
8def pairwise[iterable]:
"""iterates pairwise without holding an extra copy of iterable in memory"""
a, b = itertools.tee[iterable]
next[b, None]
return itertools.zip_longest[a, b, fillvalue=None]
counts = []
count = 1
for a, b in pairwise[s]:
...
0def pairwise[iterable]:
"""iterates pairwise without holding an extra copy of iterable in memory"""
a, b = itertools.tee[iterable]
next[b, None]
return itertools.zip_longest[a, b, fillvalue=None]
counts = []
count = 1
for a, b in pairwise[s]:
...
1from collections import Counter
s = "11100111"
result = Counter[s]
# {"1":6, "0":2}
9", ".join["{}x{}".format[label, count] for label, count in result]
# "1x3, 0x3, 2x3, 3x2, 4x2, 5x5"
5 counts = []
count = 1
for a, b in zip[s, s[1:]]:
if a==b:
count += 1
else:
counts.append[[a, count]]
count = 1
1counts = []
count = 1
for a, b in zip[s, s[1:]]:
if a==b:
count += 1
else:
counts.append[[a, count]]
count = 1
2counts = []
count = 1
for a, b in zip[s, s[1:]]:
if a==b:
count += 1
else:
counts.append[[a, count]]
count = 1
3counts = []
count = 1
for a, b in zip[s, s[1:]]:
if a==b:
count += 1
else:
counts.append[[a, count]]
count = 1
4 counts = []
count = 1
for a, b in zip[s, s[1:]]:
if a==b:
count += 1
else:
counts.append[[a, count]]
count = 1
5 counts = []
count = 1
for a, b in zip[s, s[1:]]:
if a==b:
count += 1
else:
counts.append[[a, count]]
count = 1
6 counts = []
count = 1
for a, b in zip[s, s[1:]]:
if a==b:
count += 1
else:
counts.append[[a, count]]
count = 1
7counts = []
count = 1
for a, b in zip[s, s[1:]]:
if a==b:
count += 1
else:
counts.append[[a, count]]
count = 1
8counts = []
count = 1
for a, b in zip[s, s[1:]]:
if a==b:
count += 1
else:
counts.append[[a, count]]
count = 1
9 counts = []
count = 1
for a, b in zip[s, s[1:]]:
if a==b:
count += 1
else:
counts.append[[a, count]]
count = 1
5 ", ".join["{}x{}".format[label, count] for label, count in result]
# "1x3, 0x3, 2x3, 3x2, 4x2, 5x5"
5", ".join["{}x{}".format[label, count] for label, count in result]
# "1x3, 0x3, 2x3, 3x2, 4x2, 5x5"
5 import itertools
counts = []
count = 1
for a, b in itertools.zip_longest[s, s[1:], fillvalue=None]:
if a==b:
count += 1
else:
counts.append[[a, count]]
count = 1
3import itertools
counts = []
count = 1
for a, b in itertools.zip_longest[s, s[1:], fillvalue=None]:
if a==b:
count += 1
else:
counts.append[[a, count]]
count = 1
4import itertools
counts = []
count = 1
for a, b in itertools.zip_longest[s, s[1:], fillvalue=None]:
if a==b:
count += 1
else:
counts.append[[a, count]]
count = 1
5from collections import Counter
s = "11100111"
result = Counter[s]
# {"1":6, "0":2}
03025 import itertools
counts = []
count = 1
for a, b in itertools.zip_longest[s, s[1:], fillvalue=None]:
if a==b:
count += 1
else:
counts.append[[a, count]]
count = 1
8Đầu ra
The original string is : geekforgeeekssss is bbbbest forrrrrrr geeks The K length similar characters : ['ssss', 'bbbb', 'rrrr']
Độ phức tạp về thời gian: O [N] Không gian phụ trợ: O [N]O[n]
Auxiliary Space: O[n]
4import itertools
counts = []
count = 1
for a, b in itertools.zip_longest[s, s[1:], fillvalue=None]:
if a==b:
count += 1
else:
counts.append[[a, count]]
count = 1
9from collections import Counter
s = "11100111"
result = Counter[s]
# {"1":6, "0":2}
5 ", ".join["{}x{}".format[label, count] for label, count in result]
# "1x3, 0x3, 2x3, 3x2, 4x2, 5x5"
5counts = []
count = 1
for a, b in zip[s, s[1:]]:
if a==b:
count += 1
else:
counts.append[[a, count]]
count = 1
2counts = []
count = 1
for a, b in zip[s, s[1:]]:
if a==b:
count += 1
else:
counts.append[[a, count]]
count = 1
8import itertools
counts = []
count = 1
for a, b in itertools.zip_longest[s, s[1:], fillvalue=None]:
if a==b:
count += 1
else:
counts.append[[a, count]]
count = 1
counts = []
count = 1
for a, b in zip[s, s[1:]]:
if a==b:
count += 1
else:
counts.append[[a, count]]
count = 1
8counts = []
count = 1
for a, b in zip[s, s[1:]]:
if a==b:
count += 1
else:
counts.append[[a, count]]
count = 1
9 import itertools
counts = []
count = 1
for a, b in itertools.zip_longest[s, s[1:], fillvalue=None]:
if a==b:
count += 1
else:
counts.append[[a, count]]
count = 1
5", ".join["{}x{}".format[label, count] for label, count in result]
# "1x3, 0x3, 2x3, 3x2, 4x2, 5x5"
5", ".join["{}x{}".format[label, count] for label, count in result]
# "1x3, 0x3, 2x3, 3x2, 4x2, 5x5"
5 The original string is : geekforgeeekssss is bbbbest forrrrrrr geeks The K length similar characters : ['ssss', 'bbbb', 'rrrr']3
Python3
Cho một chuỗi, trích xuất tất cả các ký tự liên tiếp độ K.
Đầu vào: test_str = ‘geekforgeeekss là bbbest forrr geek, đầu ra k = 3: [‘ eee, ‘sss,‘ bbb, ‘rrr,] Giải thích: K chuỗi chiều dài liên tiếp được trích xuất. Đầu vào: test_str = geekforGeeekSSS là bbbest forrrr geeks, k = 4 đầu ra: [‘rrrr,] Giải thích: K chuỗi chiều dài liên tiếp được trích xuất.
Phương pháp số 1: Sử dụng vòng lặp
Trong đó, chúng tôi duy trì một bộ đếm để kiểm tra sự hợp nhất của các phần tử, nếu chúng chính xác bằng k trước phần tử riêng biệt, thì số lượng, tự thêm k lần k được trả về.
", ".join["{}x{}".format[label, count] for label, count in result]
# "1x3, 0x3, 2x3, 3x2, 4x2, 5x5"
4", ".join["{}x{}".format[label, count] for label, count in result]
# "1x3, 0x3, 2x3, 3x2, 4x2, 5x5"
5 ", ".join["{}x{}".format[label, count] for label, count in result]
# "1x3, 0x3, 2x3, 3x2, 4x2, 5x5"
6counts = []
count = 1
for a, b in zip[s, s[1:]]:
if a==b:
count += 1
else:
counts.append[[a, count]]
count = 1
8[["1": 3], ["0", 3], ["2", 3], ["3", 2], ["4", 2], ["5", 5]]
06", ".join["{}x{}".format[label, count] for label, count in result]
# "1x3, 0x3, 2x3, 3x2, 4x2, 5x5"
5 [["1": 3], ["0", 3], ["2", 3], ["3", 2], ["4", 2], ["5", 5]]
08The original string is : geekforgeeekssss is bbbbest forrrrrrr geeks The K length similar characters : ['ssss', 'bbbb', 'rrrr']6
[["1": 3], ["0", 3], ["2", 3], ["3", 2], ["4", 2], ["5", 5]]
10counts = []
count = 1
for a, b in zip[s, s[1:]]:
if a==b:
count += 1
else:
counts.append[[a, count]]
count = 1
8counts = []
count = 1
for a, b in zip[s, s[1:]]:
if a==b:
count += 1
else:
counts.append[[a, count]]
count = 1
9 [["1": 3], ["0", 3], ["2", 3], ["3", 2], ["4", 2], ["5", 5]]
13from collections import Counter
s = "11100111"
result = Counter[s]
# {"1":6, "0":2}
0 [["1": 3], ["0", 3], ["2", 3], ["3", 2], ["4", 2], ["5", 5]]
15", ".join["{}x{}".format[label, count] for label, count in result]
# "1x3, 0x3, 2x3, 3x2, 4x2, 5x5"
5", ".join["{}x{}".format[label, count] for label, count in result]
# "1x3, 0x3, 2x3, 3x2, 4x2, 5x5"
5 [["1": 3], ["0", 3], ["2", 3], ["3", 2], ["4", 2], ["5", 5]]
18import itertools
counts = []
count = 1
for a, b in itertools.zip_longest[s, s[1:], fillvalue=None]:
if a==b:
count += 1
else:
counts.append[[a, count]]
count = 1
4[["1": 3], ["0", 3], ["2", 3], ["3", 2], ["4", 2], ["5", 5]]
20", ".join["{}x{}".format[label, count] for label, count in result]
# "1x3, 0x3, 2x3, 3x2, 4x2, 5x5"
7", ".join["{}x{}".format[label, count] for label, count in result]
# "1x3, 0x3, 2x3, 3x2, 4x2, 5x5"
8The original string is : geekforgeeekssss is bbbbest forrrrrrr geeks The K length similar characters : ['bbbb', 'ssss', 'rrrr']1
from collections import Counter
s = "11100111"
result = Counter[s]
# {"1":6, "0":2}
0 from collections import Counter
s = "11100111"
result = Counter[s]
# {"1":6, "0":2}
1", ".join["{}x{}".format[label, count] for label, count in result]
# "1x3, 0x3, 2x3, 3x2, 4x2, 5x5"
8[["1": 3], ["0", 3], ["2", 3], ["3", 2], ["4", 2], ["5", 5]]
27", ".join["{}x{}".format[label, count] for label, count in result]
# "1x3, 0x3, 2x3, 3x2, 4x2, 5x5"
8[["1": 3], ["0", 3], ["2", 3], ["3", 2], ["4", 2], ["5", 5]]
29[["1": 3], ["0", 3], ["2", 3], ["3", 2], ["4", 2], ["5", 5]]
30Đầu ra
The original string is : geekforgeeekssss is bbbbest forrrrrrr geeks The K length similar characters : ['bbbb', 'ssss', 'rrrr']
Độ phức tạp về thời gian: O [N] Không gian phụ trợ: O [N]O[n]
Auxiliary Space: O[n]