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ụ:
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ả.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
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:
máy tách biệt | Chia chuỗi thành chuỗi con trên mỗi lần xuất hiện |
MaxSplit | Nhiều nhất 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
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.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
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:
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àoIn [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
- Trình tự trống và bộ sưu tập:
9 [Chuỗi trố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
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].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']
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 6, vì vậy chúng tôi phải sử dụng lớpCopied!
# ✅ 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ượngCopied!
# ✅ 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.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']
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']