Hướng dẫn how do you split a large string in python? - làm thế nào để bạn chia một chuỗi lớn trong python?

Đối với chuỗi lớn, Iterator được khuyến nghị cho tốc độ và dấu chân bộ nhớ thấp.

import re, itertools

# Original text
text = "When in the course of human Events, it becomes necessary for one People to dissolve the Political Bands which have connected them with another, and to assume among the Powers of the Earth, the separate and equal Station to which the Laws of Nature and of Nature?s God entitle them, a decent Respect to the Opinions of Mankind requires that they should declare the causes which impel them to the Separation."
n = 10

# An iterator which will extract words one by one from text when needed
words = itertools.imap[lambda m:m.group[], re.finditer[r'\w+', text]]
# The final iterator that combines words into n-length groups
word_groups = itertools.izip_longest[*[words,]*n]

for g in word_groups: print g

sẽ nhận được kết quả sau:

['When', 'in', 'the', 'course', 'of', 'human', 'Events', 'it', 'becomes', 'necessary']
['for', 'one', 'People', 'to', 'dissolve', 'the', 'Political', 'Bands', 'which', 'have']
['connected', 'them', 'with', 'another', 'and', 'to', 'assume', 'among', 'the', 'Powers']
['of', 'the', 'Earth', 'the', 'separate', 'and', 'equal', 'Station', 'to', 'which']
['the', 'Laws', 'of', 'Nature', 'and', 'of', 'Nature', 's', 'God', 'entitle']
['them', 'a', 'decent', 'Respect', 'to', 'the', 'Opinions', 'of', 'Mankind', 'requires']
['that', 'they', 'should', 'declare', 'the', 'causes', 'which', 'impel', 'them', 'to']
['the', 'Separation', None, None, None, None, None, None, None, None]

Nội dung

  • Giới thiệu
  • Đoạn mã mẫu
  • Ví dụ 1: Chia chuỗi thành các khối
  • Ví dụ 2: Chuỗi phân chia theo chiều dài
  • Ví dụ 3: Chia chuỗi phân chia với độ dài 0 chunk
  • Ví dụ 4: Chia chuỗi thành các khối bằng cách sử dụng trong khi vòng lặp
  • Bản tóm tắt

Để chia một chuỗi thành các khối có độ dài cụ thể, hãy sử dụng danh sách hiểu với chuỗi. Tất cả các khối sẽ được trả lại dưới dạng một mảng.

Chúng ta cũng có thể sử dụng một vòng lặp thời gian để chia danh sách thành các khối có độ dài cụ thể.

Trong hướng dẫn này, chúng ta sẽ học cách chia một chuỗi thành các khối độ dài cụ thể, với sự trợ giúp của các chương trình Python ví dụ chi tiết.

Đoạn mã mẫu

Ví dụ 1: Chia chuỗi thành các khối

n = 3 # chunk length
chunks = [str[i:i+n] for i in range[0, len[str], n]]

Ví dụ 2: Chuỗi phân chia theo chiều dài

Ví dụ 3: Chia chuỗi phân chia với độ dài 0 chunk

Ví dụ 4: Chia chuỗi thành các khối bằng cách sử dụng trong khi vòng lặp

str = 'CarBadBoxNumKeyValRayCppSan'

n = 3
chunks = [str[i:i+n] for i in range[0, len[str], n]]
print[chunks]

Bản tóm tắt

Để chia một chuỗi thành các khối có độ dài cụ thể, hãy sử dụng danh sách hiểu với chuỗi. Tất cả các khối sẽ được trả lại dưới dạng một mảng.

['Car', 'Bad', 'Box', 'Num', 'Key', 'Val', 'Ray', 'Cpp', 'San']

Chúng ta cũng có thể sử dụng một vòng lặp thời gian để chia danh sách thành các khối có độ dài cụ thể.3. You can try with different length and different string values.

Ví dụ 2: Chuỗi phân chia theo chiều dài

Ví dụ 3: Chia chuỗi phân chia với độ dài 0 chunk4. Also, we have taken a string such that its length is not exactly divisible by chunk length. In that case, the last chunk contains characters whose count is less than the chunk size we provided.

Ví dụ 4: Chia chuỗi thành các khối bằng cách sử dụng trong khi vòng lặp

str = 'Welcome to Python Examples'

n = 4
chunks = [str[i:i+n] for i in range[0, len[str], n]]
print[chunks]

Bản tóm tắt

Để chia một chuỗi thành các khối có độ dài cụ thể, hãy sử dụng danh sách hiểu với chuỗi. Tất cả các khối sẽ được trả lại dưới dạng một mảng.

['Welc', 'ome ', 'to P', 'ytho', 'n Ex', 'ampl', 'es']

Ví dụ 3: Chia chuỗi phân chia với độ dài 0 chunk

Ví dụ 4: Chia chuỗi thành các khối bằng cách sử dụng trong khi vòng lặp

Ví dụ 4: Chia chuỗi thành các khối bằng cách sử dụng trong khi vòng lặp

str = 'Welcome to Python Examples'

#chunk size
n = 0

chunks = [str[i:i+n] for i in range[0, len[str], n]]
print[chunks]

Bản tóm tắt

Để chia một chuỗi thành các khối có độ dài cụ thể, hãy sử dụng danh sách hiểu với chuỗi. Tất cả các khối sẽ được trả lại dưới dạng một mảng.

Traceback [most recent call last]:
  File "example1.py", line 4, in 
    chunks = [str[i:i+n] for i in range[0, len[str], n]]
ValueError: range[] arg 3 must not be zero

Chúng ta cũng có thể sử dụng một vòng lặp thời gian để chia danh sách thành các khối có độ dài cụ thể.

Ví dụ 4: Chia chuỗi thành các khối bằng cách sử dụng trong khi vòng lặp

Bản tóm tắt

Ví dụ 4: Chia chuỗi thành các khối bằng cách sử dụng trong khi vòng lặp

str = 'Welcome to Python Examples'
n = 5

chunks = []

i = 0
while i < len[str]:
    if i+n < len[str]:
        chunks.append[str[i:i+n]]
    else:
        chunks.append[str[i:len[str]]]
    i += n
print[chunks]

Bản tóm tắt

Để chia một chuỗi thành các khối có độ dài cụ thể, hãy sử dụng danh sách hiểu với chuỗi. Tất cả các khối sẽ được trả lại dưới dạng một mảng.

['When', 'in', 'the', 'course', 'of', 'human', 'Events', 'it', 'becomes', 'necessary']
['for', 'one', 'People', 'to', 'dissolve', 'the', 'Political', 'Bands', 'which', 'have']
['connected', 'them', 'with', 'another', 'and', 'to', 'assume', 'among', 'the', 'Powers']
['of', 'the', 'Earth', 'the', 'separate', 'and', 'equal', 'Station', 'to', 'which']
['the', 'Laws', 'of', 'Nature', 'and', 'of', 'Nature', 's', 'God', 'entitle']
['them', 'a', 'decent', 'Respect', 'to', 'the', 'Opinions', 'of', 'Mankind', 'requires']
['that', 'they', 'should', 'declare', 'the', 'causes', 'which', 'impel', 'them', 'to']
['the', 'Separation', None, None, None, None, None, None, None, None]
0

Bản tóm tắt

Để chia một chuỗi thành các khối có độ dài cụ thể, hãy sử dụng danh sách hiểu với chuỗi. Tất cả các khối sẽ được trả lại dưới dạng một mảng.

Chúng ta cũng có thể sử dụng một vòng lặp thời gian để chia danh sách thành các khối có độ dài cụ thể.

  • Trong hướng dẫn này, chúng ta sẽ học cách chia một chuỗi thành các khối độ dài cụ thể, với sự trợ giúp của các chương trình Python ví dụ chi tiết.
  • Sau đây là một đoạn mã nhanh để chia một chuỗi đã cho
    ['When', 'in', 'the', 'course', 'of', 'human', 'Events', 'it', 'becomes', 'necessary']
    ['for', 'one', 'People', 'to', 'dissolve', 'the', 'Political', 'Bands', 'which', 'have']
    ['connected', 'them', 'with', 'another', 'and', 'to', 'assume', 'among', 'the', 'Powers']
    ['of', 'the', 'Earth', 'the', 'separate', 'and', 'equal', 'Station', 'to', 'which']
    ['the', 'Laws', 'of', 'Nature', 'and', 'of', 'Nature', 's', 'God', 'entitle']
    ['them', 'a', 'decent', 'Respect', 'to', 'the', 'Opinions', 'of', 'Mankind', 'requires']
    ['that', 'they', 'should', 'declare', 'the', 'causes', 'which', 'impel', 'them', 'to']
    ['the', 'Separation', None, None, None, None, None, None, None, None]
    
    1 thành các khối có độ dài cụ thể
    ['When', 'in', 'the', 'course', 'of', 'human', 'Events', 'it', 'becomes', 'necessary']
    ['for', 'one', 'People', 'to', 'dissolve', 'the', 'Political', 'Bands', 'which', 'have']
    ['connected', 'them', 'with', 'another', 'and', 'to', 'assume', 'among', 'the', 'Powers']
    ['of', 'the', 'Earth', 'the', 'separate', 'and', 'equal', 'Station', 'to', 'which']
    ['the', 'Laws', 'of', 'Nature', 'and', 'of', 'Nature', 's', 'God', 'entitle']
    ['them', 'a', 'decent', 'Respect', 'to', 'the', 'Opinions', 'of', 'Mankind', 'requires']
    ['that', 'they', 'should', 'declare', 'the', 'causes', 'which', 'impel', 'them', 'to']
    ['the', 'Separation', None, None, None, None, None, None, None, None]
    
    2 bằng cách sử dụng danh sách hiểu.
  • Ví dụ 1: Chia chuỗi thành các khối
  • Trong đó, chúng tôi sẽ lấy một chuỗi
    ['When', 'in', 'the', 'course', 'of', 'human', 'Events', 'it', 'becomes', 'necessary']
    ['for', 'one', 'People', 'to', 'dissolve', 'the', 'Political', 'Bands', 'which', 'have']
    ['connected', 'them', 'with', 'another', 'and', 'to', 'assume', 'among', 'the', 'Powers']
    ['of', 'the', 'Earth', 'the', 'separate', 'and', 'equal', 'Station', 'to', 'which']
    ['the', 'Laws', 'of', 'Nature', 'and', 'of', 'Nature', 's', 'God', 'entitle']
    ['them', 'a', 'decent', 'Respect', 'to', 'the', 'Opinions', 'of', 'Mankind', 'requires']
    ['that', 'they', 'should', 'declare', 'the', 'causes', 'which', 'impel', 'them', 'to']
    ['the', 'Separation', None, None, None, None, None, None, None, None]
    
    1 và chia chuỗi này thành các khối độ dài
    ['When', 'in', 'the', 'course', 'of', 'human', 'Events', 'it', 'becomes', 'necessary']
    ['for', 'one', 'People', 'to', 'dissolve', 'the', 'Political', 'Bands', 'which', 'have']
    ['connected', 'them', 'with', 'another', 'and', 'to', 'assume', 'among', 'the', 'Powers']
    ['of', 'the', 'Earth', 'the', 'separate', 'and', 'equal', 'Station', 'to', 'which']
    ['the', 'Laws', 'of', 'Nature', 'and', 'of', 'Nature', 's', 'God', 'entitle']
    ['them', 'a', 'decent', 'Respect', 'to', 'the', 'Opinions', 'of', 'Mankind', 'requires']
    ['that', 'they', 'should', 'declare', 'the', 'causes', 'which', 'impel', 'them', 'to']
    ['the', 'Separation', None, None, None, None, None, None, None, None]
    
    4 bằng cách sử dụng danh sách hiểu.
  • Chương trình Python

Làm thế nào để bạn chia một chuỗi dài trong Python?

Sử dụng trích dẫn ba để tạo một chuỗi đa dòng, phương pháp đơn giản nhất để cho một chuỗi dài chia thành các dòng khác nhau. Bạn sẽ cần phải đặt nó với một cặp trích dẫn ba, một lần bắt đầu và thứ hai cuối cùng. Bất cứ điều gì bên trong các trích dẫn ba kèm theo sẽ trở thành một phần của một chuỗi đa dòng. It is the simplest method to let a long string split into different lines. You will need to enclose it with a pair of Triple quotes, one at the start and second in the end. Anything inside the enclosing Triple quotes will become part of one multiline string.

Làm cách nào để chia một chuỗi thành nhiều chuỗi trong Python?

Trong Python, bạn có thể chia một chuỗi với phương thức chia [].Nó phá vỡ một chuỗi [dựa trên bộ phân cách đã cho] và trả về một danh sách các chuỗi.Để phân chia một chuỗi, chúng tôi sử dụng phương thức.tách ra[] .with the split[] method. It breaks up a string [based on the given separator] and returns a list of strings. To split a string, we use the method . split[] .

Làm thế nào để bạn chia một chuỗi thành 3 trong Python?

Python 3 - Chuỗi chia [] Phương thức chia [] Phương thức trả về một danh sách tất cả các từ trong chuỗi, sử dụng str làm dấu tách [chia trên tất cả khoảng trắng nếu không xác định], tùy chọn giới hạn số lượng phân tách thành num.The split[] method returns a list of all the words in the string, using str as the separator [splits on all whitespace if left unspecified], optionally limiting the number of splits to num.

Làm cách nào để chia một chuỗi thành nhiều phần?

Bạn có thể chia một chuỗi cho mỗi ký tự bằng một chuỗi trống [''] làm bộ chia.Trong ví dụ dưới đây, chúng tôi chia cùng một thông báo bằng một chuỗi trống.Kết quả của sự phân chia sẽ là một mảng chứa tất cả các ký tự trong chuỗi tin nhắn.using an empty string[''] as the splitter. In the example below, we split the same message using an empty string. The result of the split will be an array containing all the characters in the message string.

Bài Viết Liên Quan

Chủ Đề