Hướng dẫn how do you split a list with commas in python? - làm thế nào để bạn tách danh sách bằng dấu phẩy trong python?

sử dụng itertools.chain:

from itertools import chain

print[list[chain.from_iterable[ele.split[","] for ele in l]]]
['60', '78', '70', '77', '80', '74', '90', '75', '100', '74', '110', '75']

Bạn càng có nhiều mục để làm phẳng chuỗi thực hiện nó một cách hiệu quả hơn một chút:

In [1]: l= ["1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20" for _ in range[100000]]

In [2]: from itertools import chain

In [3]: l= ["1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30" for _ in range[10000]]

In [4]: timeit [list[chain.from_iterable[ele.split[","] for ele in l]]]
100 loops, best of 3: 17.7 ms per loop

In [5]: timeit  [item for items in l for item in items.split[","]]
10 loops, best of 3: 20.9 ms per loop

Chia một chuỗi bằng dấu phẩy trong python #

Sử dụng phương thức str.split[] để phân chia một chuỗi bằng dấu phẩy, ví dụ:

In [1]: l= ["1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20" for _ in range[100000]]

In [2]: from itertools import chain

In [3]: l= ["1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30" for _ in range[10000]]

In [4]: timeit [list[chain.from_iterable[ele.split[","] for ele in l]]]
100 loops, best of 3: 17.7 ms per loop

In [5]: timeit  [item for items in l for item in items.split[","]]
10 loops, best of 3: 20.9 ms per loop
0. Phương pháp
In [1]: l= ["1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20" for _ in range[100000]]

In [2]: from itertools import chain

In [3]: l= ["1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30" for _ in range[10000]]

In [4]: timeit [list[chain.from_iterable[ele.split[","] for ele in l]]]
100 loops, best of 3: 17.7 ms per loop

In [5]: timeit  [item for items in l for item in items.split[","]]
10 loops, best of 3: 20.9 ms per loop
1 sẽ phân chia chuỗi trên mỗi lần xuất hiện của dấu phẩy và sẽ trả về một danh sách chứa kết quả.

Copied!

# ✅ split string on each occurrence of comma my_str = 'one,two,three,four' my_list = my_str.split[','] print[my_list] # 👉️ ['one', 'two', 'three', 'four'] # ✅ split string on each space or comma my_str_2 = 'one two,three four five' my_list_2 = my_str_2.replace[',', ' '].split[' '] print[my_list_2] # 👉️ ['one', 'two', 'three', 'four', 'five']

Phương thức str.split [] chia chuỗi thành một danh sách các chuỗi con bằng cách sử dụng dấu phân cách.

Phương thức lấy 2 tham số sau:

TênSự mô tả
máy tách biệtChia chuỗi thành chuỗi con trên mỗi lần xuất hiện
MaxSplitNhiều nhất
In [1]: l= ["1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20" for _ in range[100000]]

In [2]: from itertools import chain

In [3]: l= ["1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30" for _ in range[10000]]

In [4]: timeit [list[chain.from_iterable[ele.split[","] for ele in l]]]
100 loops, best of 3: 17.7 ms per loop

In [5]: timeit  [item for items in l for item in items.split[","]]
10 loops, best of 3: 20.9 ms per loop
2 chia tách được thực hiện [tùy chọn]

Nếu bộ phân cách không được tìm thấy trong chuỗi, một danh sách chỉ chứa 1 phần tử được trả về.

Copied!

my_str = 'one' my_list = my_str.split[','] # 👇️ ['one'] print[my_list]

Nếu bạn có khoảng trắng giữa các từ được phân tách bằng dấu phẩy trong chuỗi và cần xóa nó, hãy sử dụng phương thức

In [1]: l= ["1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20" for _ in range[100000]]

In [2]: from itertools import chain

In [3]: l= ["1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30" for _ in range[10000]]

In [4]: timeit [list[chain.from_iterable[ele.split[","] for ele in l]]]
100 loops, best of 3: 17.7 ms per loop

In [5]: timeit  [item for items in l for item in items.split[","]]
10 loops, best of 3: 20.9 ms per loop
3.

Copied!

my_str = 'one , two , three ,four' my_list = [item.strip[] for item in my_str.split[',']] print[my_list] # 👉️ ['one', 'two', 'three', 'four']

Chúng tôi đã sử dụng một danh sách hiểu để loại bỏ khoảng trắng dẫn đầu và dấu vết từ mỗi chuỗi.

Danh sách các hệ thống được sử dụng để thực hiện một số hoạt động cho mọi yếu tố hoặc chọn một tập hợp con của các phần tử đáp ứng một điều kiện.

Phương thức str.strip trả về một bản sao của chuỗi với khoảng trắng dẫn đầu và dấu vết đã bị loại bỏ.

Nếu chuỗi của bạn bắt đầu bằng hoặc kết thúc bằng dấu phẩy, bạn sẽ nhận được các phần tử chuỗi trống trong danh sách.

Copied!

my_str = ',one,two,three,four,' my_list = my_str.split[','] print[my_list] # 👉️ ['', 'one', 'two', 'three', 'four', '']

Bạn có thể sử dụng chức năng

In [1]: l= ["1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20" for _ in range[100000]]

In [2]: from itertools import chain

In [3]: l= ["1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30" for _ in range[10000]]

In [4]: timeit [list[chain.from_iterable[ele.split[","] for ele in l]]]
100 loops, best of 3: 17.7 ms per loop

In [5]: timeit  [item for items in l for item in items.split[","]]
10 loops, best of 3: 20.9 ms per loop
4 để xóa bất kỳ chuỗi trống nào khỏi danh sách.

Copied!

my_str = ',one,two,three,four,' my_list = list[filter[None, my_str.split[',']]] print[my_list] # 👉️ ['one', 'two', 'three', 'four']

Hàm bộ lọc có hàm và có thể lặp lại như các đối số và xây dựng một trình lặp lại từ các phần tử của có thể điều chỉnh được hàm trả về giá trị sự thật.

Nếu bạn vượt qua

In [1]: l= ["1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20" for _ in range[100000]]

In [2]: from itertools import chain

In [3]: l= ["1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30" for _ in range[10000]]

In [4]: timeit [list[chain.from_iterable[ele.split[","] for ele in l]]]
100 loops, best of 3: 17.7 ms per loop

In [5]: timeit  [item for items in l for item in items.split[","]]
10 loops, best of 3: 20.9 ms per loop
5 cho đối số chức năng, tất cả các yếu tố giả mạo của IT có thể bị xóa.

Tất cả các giá trị không phải là sự thật được coi là giả mạo. Các giá trị giả trong Python là:

  • Các hằng số được xác định là giả mạo:
    In [1]: l= ["1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20" for _ in range[100000]]
    
    In [2]: from itertools import chain
    
    In [3]: l= ["1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30" for _ in range[10000]]
    
    In [4]: timeit [list[chain.from_iterable[ele.split[","] for ele in l]]]
    100 loops, best of 3: 17.7 ms per loop
    
    In [5]: timeit  [item for items in l for item in items.split[","]]
    10 loops, best of 3: 20.9 ms per loop
    
    5 và
    In [1]: l= ["1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20" for _ in range[100000]]
    
    In [2]: from itertools import chain
    
    In [3]: l= ["1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30" for _ in range[10000]]
    
    In [4]: timeit [list[chain.from_iterable[ele.split[","] for ele in l]]]
    100 loops, best of 3: 17.7 ms per loop
    
    In [5]: timeit  [item for items in l for item in items.split[","]]
    10 loops, best of 3: 20.9 ms per loop
    
    7.
  • In [1]: l= ["1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20" for _ in range[100000]]
    
    In [2]: from itertools import chain
    
    In [3]: l= ["1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30" for _ in range[10000]]
    
    In [4]: timeit [list[chain.from_iterable[ele.split[","] for ele in l]]]
    100 loops, best of 3: 17.7 ms per loop
    
    In [5]: timeit  [item for items in l for item in items.split[","]]
    10 loops, best of 3: 20.9 ms per loop
    
    8 [không] thuộc bất kỳ loại số nào
  • Trình tự trống và bộ sưu tập:
    In [1]: l= ["1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20" for _ in range[100000]]
    
    In [2]: from itertools import chain
    
    In [3]: l= ["1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30" for _ in range[10000]]
    
    In [4]: timeit [list[chain.from_iterable[ele.split[","] for ele in l]]]
    100 loops, best of 3: 17.7 ms per loop
    
    In [5]: timeit  [item for items in l for item in items.split[","]]
    10 loops, best of 3: 20.9 ms per loop
    
    9 [Chuỗi trống],

    Copied!

    # ✅ split string on each occurrence of comma my_str = 'one,two,three,four' my_list = my_str.split[','] print[my_list] # 👉️ ['one', 'two', 'three', 'four'] # ✅ split string on each space or comma my_str_2 = 'one two,three four five' my_list_2 = my_str_2.replace[',', ' '].split[' '] print[my_list_2] # 👉️ ['one', 'two', 'three', 'four', 'five']
    0 [Tuple trống],

    Copied!

    # ✅ split string on each occurrence of comma my_str = 'one,two,three,four' my_list = my_str.split[','] print[my_list] # 👉️ ['one', 'two', 'three', 'four'] # ✅ split string on each space or comma my_str_2 = 'one two,three four five' my_list_2 = my_str_2.replace[',', ' '].split[' '] print[my_list_2] # 👉️ ['one', 'two', 'three', 'four', 'five']
    1 [danh sách trống],

    Copied!

    # ✅ split string on each occurrence of comma my_str = 'one,two,three,four' my_list = my_str.split[','] print[my_list] # 👉️ ['one', 'two', 'three', 'four'] # ✅ split string on each space or comma my_str_2 = 'one two,three four five' my_list_2 = my_str_2.replace[',', ' '].split[' '] print[my_list_2] # 👉️ ['one', 'two', 'three', 'four', 'five']
    2 [Từ điển trống],

    Copied!

    # ✅ split string on each occurrence of comma my_str = 'one,two,three,four' my_list = my_str.split[','] print[my_list] # 👉️ ['one', 'two', 'three', 'four'] # ✅ split string on each space or comma my_str_2 = 'one two,three four five' my_list_2 = my_str_2.replace[',', ' '].split[' '] print[my_list_2] # 👉️ ['one', 'two', 'three', 'four', 'five']
    3 [bộ trống],

    Copied!

    # ✅ split string on each occurrence of comma my_str = 'one,two,three,four' my_list = my_str.split[','] print[my_list] # 👉️ ['one', 'two', 'three', 'four'] # ✅ split string on each space or comma my_str_2 = 'one two,three four five' my_list_2 = my_str_2.replace[',', ' '].split[' '] print[my_list_2] # 👉️ ['one', 'two', 'three', 'four', 'five']
    4 [phạm vi trống].

Lưu ý rằng hàm

In [1]: l= ["1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20" for _ in range[100000]]

In [2]: from itertools import chain

In [3]: l= ["1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30" for _ in range[10000]]

In [4]: timeit [list[chain.from_iterable[ele.split[","] for ele in l]]]
100 loops, best of 3: 17.7 ms per loop

In [5]: timeit  [item for items in l for item in items.split[","]]
10 loops, best of 3: 20.9 ms per loop
4 trả về đối tượng

Copied!

# ✅ split string on each occurrence of comma my_str = 'one,two,three,four' my_list = my_str.split[','] print[my_list] # 👉️ ['one', 'two', 'three', 'four'] # ✅ split string on each space or comma my_str_2 = 'one two,three four five' my_list_2 = my_str_2.replace[',', ' '].split[' '] print[my_list_2] # 👉️ ['one', 'two', 'three', 'four', 'five']
6, vì vậy chúng tôi phải sử dụng lớp

Copied!

# ✅ split string on each occurrence of comma my_str = 'one,two,three,four' my_list = my_str.split[','] print[my_list] # 👉️ ['one', 'two', 'three', 'four'] # ✅ split string on each space or comma my_str_2 = 'one two,three four five' my_list_2 = my_str_2.replace[',', ' '].split[' '] print[my_list_2] # 👉️ ['one', 'two', 'three', 'four', 'five']
7 để chuyển đổi đối tượng

Copied!

# ✅ split string on each occurrence of comma my_str = 'one,two,three,four' my_list = my_str.split[','] print[my_list] # 👉️ ['one', 'two', 'three', 'four'] # ✅ split string on each space or comma my_str_2 = 'one two,three four five' my_list_2 = my_str_2.replace[',', ' '].split[' '] print[my_list_2] # 👉️ ['one', 'two', 'three', 'four', 'five']
6 thành danh sách.

Nếu bạn cần chia một chuỗi về sự xuất hiện của dấu phẩy và một nhân vật khác, hãy thay thế dấu phẩy bằng ký tự khác và chia cho nhân vật đó.

Copied!

my_str = 'one,two,three,four' my_str_2 = 'one two,three four five' my_list_2 = my_str_2.replace[',', ' '].split[' '] print[my_list_2] # 👉️ ['one', 'two', 'three', 'four', 'five']

Chúng tôi đã thay thế tất cả các lần xuất hiện của một dấu phẩy bằng một không gian và chia chuỗi trên mỗi không gian.

Bạn có thể đạt được kết quả tương tự bằng cách thay thế mỗi lần xuất hiện một không gian bằng dấu phẩy và chia tách trên mỗi dấu phẩy.

Copied!

my_str = 'one,two,three,four' my_str_2 = 'one two,three four five' my_list_2 = my_str_2.replace[' ', ','].split[','] print[my_list_2] # 👉️ ['one', 'two', 'three', 'four', 'five']

Làm thế nào để bạn phân tách một danh sách với dấu phẩy?

[Khi có nhiều hơn hai mục danh sách, dấu phẩy được sử dụng để tách chúng.]When there are more than two list items, commas are used to separate them.]

Làm thế nào để bạn chia dữ liệu trong một danh sách trong Python?

Phương thức phân chia chuỗi python [] Phương thức phân tách một chuỗi thành một danh sách.Bạn có thể chỉ định phân tách, dấu phân cách mặc định là bất kỳ khoảng trắng nào.Lưu ý: Khi MaxSplit được chỉ định, danh sách sẽ chứa số lượng phần tử được chỉ định cộng với một.The split[] method splits a string into a list. You can specify the separator, default separator is any whitespace. Note: When maxsplit is specified, the list will contain the specified number of elements plus one.

Làm thế nào để bạn đặt một dấu phẩy trong một danh sách trong Python?

Phương thức 1: Sử dụng phương thức Jof [] Tạo danh sách và thêm một số chuỗi giả vào nó.Nhận chuỗi được phân tách bằng dấu phẩy từ danh sách bằng cách chuyển danh sách dưới dạng đối số cho hàm nối [] [tham gia [] là một hàm chuỗi trong python được sử dụng để tham gia các phần tử của chuỗi được phân tách bằng bộ phân cách chuỗi.Using join[] method Create a list and add some dummy strings to it. Get the comma-separated string from the list by passing the list as an argument to the join[] function[join[] is a string function in Python that is used to join elements of a sequence that are separated by a string separator.

Bài Viết Liên Quan

Chủ Đề