Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Đọc
Examples:
Input : {1, 2, 3, 4} Output : [1, 2, 3, 4] Input : {'Geeks', 'for', 'geeks'} Output : ['Geeks', 'for', 'geeks']
Bàn luận
Approach #1
: Using list[set_name]
.
Đưa ra một bộ, viết một chương trình Python để chuyển đổi tập hợp đã cho thành danh sách.
& nbsp; Cách tiếp cận #1: Sử dụng list[set_name]
.
Typecasting vào danh sách có thể được thực hiện bằng cách sử dụng list[set_name]
.
>>> my_set=[[1,2,3,4]]
>>> my_set
[1, 2, 3, 4]
>>> type[my_set]
>>> list[my_set]
[1, 2, 3, 4]
>>> type[_]
4>>> my_set=[[1,2,3,4]]
>>> my_set
[1, 2, 3, 4]
>>> type[my_set]
>>> list[my_set]
[1, 2, 3, 4]
>>> type[_]
5Output:
['Geeks', 'for', 'geeks']
['Geeks', 'for', 'geeks']1
['Geeks', 'for', 'geeks']2
['Geeks', 'for', 'geeks']3
['Geeks', 'for', 'geeks']4
['Geeks', 'for', 'geeks']5
['Geeks', 'for', 'geeks']6
['Geeks', 'for', 'geeks']5
['Geeks', 'for', 'geeks']8
['Geeks', 'for', 'geeks']9
>>> my_set=[[1,2,3,4]]
>>> my_set
[1, 2, 3, 4]
>>> type[my_set]
>>> list[my_set]
[1, 2, 3, 4]
>>> type[_]
0__12 >>> my_set=[[1,2,3,4]]
>>> my_set
[1, 2, 3, 4]
>>> type[my_set]
>>> list[my_set]
[1, 2, 3, 4]
>>> type[_]
223>>> my_set=[[1,2,3,4]]
>>> my_set
[1, 2, 3, 4]
>>> type[my_set]
>>> list[my_set]
[1, 2, 3, 4]
>>> type[_]
6 >>> my_set=[[1,2,3,4]]
>>> my_set
[1, 2, 3, 4]
>>> type[my_set]
>>> list[my_set]
[1, 2, 3, 4]
>>> type[_]
7>>> my_set=[[1,2,3,4]]
>>> my_set
[1, 2, 3, 4]
>>> type[my_set]
>>> list[my_set]
[1, 2, 3, 4]
>>> type[_]
8>>> my_set=[[1,2,3,4]]
>>> my_set
[1, 2, 3, 4]
>>> type[my_set]
>>> list[my_set]
[1, 2, 3, 4]
>>> type[_]
9>>> my_set=[[1,2,3,4]]
>>> my_set
[1, 2, 3, 4]
>>> type[my_set]
>>> list[my_set]
[1, 2, 3, 4]
>>> type[_]
4my_list = list[my_iterable]7
>>> my_set = set[[1, 2, 3, 4]]
>>> my_set
set[[1, 2, 3, 4]]
>>> type[my_set]
>>> list[my_set]
[1, 2, 3, 4]
>>> type[_]
0>>> my_set = set[[1, 2, 3, 4]]
>>> my_set
set[[1, 2, 3, 4]]
>>> type[my_set]
>>> list[my_set]
[1, 2, 3, 4]
>>> type[_]
1 >>> my_set=[[1,2,3,4]]
>>> my_set
[1, 2, 3, 4]
>>> type[my_set]
>>> list[my_set]
[1, 2, 3, 4]
>>> type[_]
2>>> my_set = set[[1, 2, 3, 4]]
>>> my_set
set[[1, 2, 3, 4]]
>>> type[my_set]
>>> list[my_set]
[1, 2, 3, 4]
>>> type[_]
3>>> my_set=[[1,2,3,4]]
>>> my_set
[1, 2, 3, 4]
>>> type[my_set]
>>> list[my_set]
[1, 2, 3, 4]
>>> type[_]
8>>> my_set = set[[1, 2, 3, 4]]
>>> my_set
set[[1, 2, 3, 4]]
>>> type[my_set]
>>> list[my_set]
[1, 2, 3, 4]
>>> type[_]
5Approach #2 : using
my_list = list[my_iterable]8 method
>>> my_set=[[1,2,3,4]]
>>> my_set
[1, 2, 3, 4]
>>> type[my_set]
>>> list[my_set]
[1, 2, 3, 4]
>>> type[_]
0['Geeks', 'for', 'geeks']2
>>> my_set=[[1,2,3,4]]
>>> my_set
[1, 2, 3, 4]
>>> type[my_set]
>>> list[my_set]
[1, 2, 3, 4]
>>> type[_]
8>>> my_set = set[[1, 2, 3, 4]]
>>> my_set
set[[1, 2, 3, 4]]
>>> type[my_set]
>>> list[my_set]
[1, 2, 3, 4]
>>> type[_]
9my_list = list[my_iterable]0
['Geeks', 'for', 'geeks']5
my_list = list[my_iterable]2
['Geeks', 'for', 'geeks']5
my_list = list[my_iterable]444545
['Geeks', 'for', 'geeks']1
['Geeks', 'for', 'geeks']2
['Geeks', 'for', 'geeks']3
['Geeks', 'for', 'geeks']4
['Geeks', 'for', 'geeks']5
['Geeks', 'for', 'geeks']6
['Geeks', 'for', 'geeks']5
['Geeks', 'for', 'geeks']8
['Geeks', 'for', 'geeks']9
>>> my_set=[[1,2,3,4]]
>>> my_set
[1, 2, 3, 4]
>>> type[my_set]
>>> list[my_set]
[1, 2, 3, 4]
>>> type[_]
0__12 >>> my_set=[[1,2,3,4]]
>>> my_set
[1, 2, 3, 4]
>>> type[my_set]
>>> list[my_set]
[1, 2, 3, 4]
>>> type[_]
223
>>> my_set=[[1,2,3,4]]
>>> my_set
[1, 2, 3, 4]
>>> type[my_set]
>>> list[my_set]
[1, 2, 3, 4]
>>> type[_]
6 >>> my_set=[[1,2,3,4]]
>>> my_set
[1, 2, 3, 4]
>>> type[my_set]
>>> list[my_set]
[1, 2, 3, 4]
>>> type[_]
7>>> my_set=[[1,2,3,4]]
>>> my_set
[1, 2, 3, 4]
>>> type[my_set]
>>> list[my_set]
[1, 2, 3, 4]
>>> type[_]
8>>> my_set=[[1,2,3,4]]
>>> my_set
[1, 2, 3, 4]
>>> type[my_set]
>>> list[my_set]
[1, 2, 3, 4]
>>> type[_]
9>>> my_set = set[[1, 2, 3, 4]]
>>> my_set
set[[1, 2, 3, 4]]
>>> type[my_set]
>>> list[my_set]
[1, 2, 3, 4]
>>> type[_]
0>>> my_set = set[[1, 2, 3, 4]]
>>> my_set
set[[1, 2, 3, 4]]
>>> type[my_set]
>>> list[my_set]
[1, 2, 3, 4]
>>> type[_]
1 >>> my_set=[[1,2,3,4]]
>>> my_set
[1, 2, 3, 4]
>>> type[my_set]
>>> list[my_set]
[1, 2, 3, 4]
>>> type[_]
2>>> my_set = set[[1, 2, 3, 4]]
>>> my_set
set[[1, 2, 3, 4]]
>>> type[my_set]
>>> list[my_set]
[1, 2, 3, 4]
>>> type[_]
3>>> my_set=[[1,2,3,4]]
>>> my_set
[1, 2, 3, 4]
>>> type[my_set]
>>> list[my_set]
[1, 2, 3, 4]
>>> type[_]
8>>> my_set = set[[1, 2, 3, 4]]
>>> my_set
set[[1, 2, 3, 4]]
>>> type[my_set]
>>> list[my_set]
[1, 2, 3, 4]
>>> type[_]
5>>> my_set=[[1,2,3,4]]
>>> my_set
[1, 2, 3, 4]
>>> type[my_set]
>>> list[my_set]
[1, 2, 3, 4]
>>> type[_]
4my_list = list[my_iterable]7
>>> my_set=[[1,2,3,4]]
>>> my_set
[1, 2, 3, 4]
>>> type[my_set]
>>> list[my_set]
[1, 2, 3, 4]
>>> type[_]
0['Geeks', 'for', 'geeks']2
>>> my_set=[[1,2,3,4]]
>>> my_set
[1, 2, 3, 4]
>>> type[my_set]
>>> list[my_set]
[1, 2, 3, 4]
>>> type[_]
8>>> my_set = set[[1, 2, 3, 4]]
>>> my_set
set[[1, 2, 3, 4]]
>>> type[my_set]
>>> list[my_set]
[1, 2, 3, 4]
>>> type[_]
9my_list = list[my_iterable]0
['Geeks', 'for', 'geeks']5
my_list = list[my_iterable]2
['Geeks', 'for', 'geeks']5
my_list = list[my_iterable]444545
Approach #3 : Using
s = set[{1, 2, 3}] a = [] for i in s: a.append[i] print[a]5
This essentially unpacks the set s inside a list literal which is created due to the presence of the single comma [, ]. This approach is a bit faster but suffers from readability.
['Geeks', 'for', 'geeks']1
['Geeks', 'for', 'geeks']2
['Geeks', 'for', 'geeks']3
['Geeks', 'for', 'geeks']4
['Geeks', 'for', 'geeks']5
['Geeks', 'for', 'geeks']6
['Geeks', 'for', 'geeks']5
['Geeks', 'for', 'geeks']8
['Geeks', 'for', 'geeks']9
>>> my_set=[[1,2,3,4]]
>>> my_set
[1, 2, 3, 4]
>>> type[my_set]
>>> list[my_set]
[1, 2, 3, 4]
>>> type[_]
0__12 >>> my_set=[[1,2,3,4]]
>>> my_set
[1, 2, 3, 4]
>>> type[my_set]
>>> list[my_set]
[1, 2, 3, 4]
>>> type[_]
223>>> my_set=[[1,2,3,4]]
>>> my_set
[1, 2, 3, 4]
>>> type[my_set]
>>> list[my_set]
[1, 2, 3, 4]
>>> type[_]
6 >>> my_set=[[1,2,3,4]]
>>> my_set
[1, 2, 3, 4]
>>> type[my_set]
>>> list[my_set]
[1, 2, 3, 4]
>>> type[_]
7>>> my_set=[[1,2,3,4]]
>>> my_set
[1, 2, 3, 4]
>>> type[my_set]
>>> list[my_set]
[1, 2, 3, 4]
>>> type[_]
8>>> my_set=[[1,2,3,4]]
>>> my_set
[1, 2, 3, 4]
>>> type[my_set]
>>> list[my_set]
[1, 2, 3, 4]
>>> type[_]
9>>> my_set=[[1,2,3,4]]
>>> my_set
[1, 2, 3, 4]
>>> type[my_set]
>>> list[my_set]
[1, 2, 3, 4]
>>> type[_]
4my_list = list[my_iterable]7
Xem lại dòng đầu tiên của bạn. Dấu vết ngăn xếp của bạn rõ ràng không phải từ mã bạn đã dán ở đây, vì vậy tôi không biết chính xác những gì bạn đã làm.
>>> my_set=[[1,2,3,4]]
>>> my_set
[1, 2, 3, 4]
>>> type[my_set]
>>> list[my_set]
[1, 2, 3, 4]
>>> type[_]
Những gì bạn muốn là list[set_name]
8.
>>> my_set = set[[1, 2, 3, 4]]
>>> my_set
set[[1, 2, 3, 4]]
>>> type[my_set]
>>> list[my_set]
[1, 2, 3, 4]
>>> type[_]
Ngoại lệ "không thể gọi được" có nghĩa là bạn đang làm một cái gì đó như list[set_name]
9 - cố gắng gọi một ví dụ
>>> my_set=[[1,2,3,4]]
>>> my_set
[1, 2, 3, 4]
>>> type[my_set]
>>> list[my_set]
[1, 2, 3, 4]
>>> type[_]
8. Chúng ta có thể chuyển đổi một tập hợp thành danh sách trong Python bằng phương thức Danh sách tích hợp []. Hãy cùng xem một số ví dụ bằng cách sử dụng chức năng này.list[] method. Let’s take a look at some examples using this function. Chúng tôi sẽ đi qua các phương thức đơn giản cho phép chuyển đổi đối tượng Set Python thành một đối tượng danh sách một cách nhanh chóng. Hàm Danh sách [] lấy một đối số có thể lặp lại và chuyển đổi nó thành một đối tượng loại danh sách. Đây là một phương pháp tích hợp sẵn sàng để bạn sử dụng.list[] function takes an iterable as an argument and converts that into a List type object. This is a built-in method ready for you to use.1. Sử dụng hàm Danh sách []
my_list = list[my_iterable]
Vì một bộ cũng có thể lặp lại, chúng tôi có thể chuyển nó vào phương thức
['Geeks', 'for', 'geeks']01 và nhận danh sách tương ứng của chúng tôi.
my_set = set[{1, 4, 3, 5}] my_list = list[my_set] print[my_list]
Đầu ra, như mong đợi, sẽ là một danh sách chứa các giá trị trên.
Lưu ý rằng thứ tự của danh sách có thể là ngẫu nhiên, và không nhất thiết phải sắp xếp.
Ví dụ, lấy đoạn trích dưới đây.
s = set[] s.add["A"] s.add["B"] print[list[s]]
Đầu ra trong trường hợp của tôi::
2. Sử dụng phép lặp thủ công
Chúng ta cũng có thể thêm các yếu tố theo cách thủ công vào danh sách vì bộ này có thể hiểu được. Phương thức này không có bất kỳ lợi thế trong thế giới thực nào so với sử dụng phương thức danh sách [] ngoài việc được viết bởi bạn.
s = set[{1, 2, 3}] a = [] for i in s: a.append[i] print[a]
Một lần nữa, đầu ra là một danh sách:
Chuyển đổi Frozenset thành danh sách
Đối tượng Python Frozenset tương tự như một tập hợp nhưng là bất biến. Do đó, chúng tôi không thể sửa đổi các yếu tố của một chiếc đông lạnh. Chúng ta cũng có thể chuyển đổi loại tập hợp này, sử dụng danh sách [].frozenset object is similar to a set but is immutable. Therefore, we cannot modify the elements of a frozenset. We can convert this type of set too, using list[].
f_set = frozenset[{1, 3, 2, 5}] a = list[f_set] print[a]
Đầu ra
Người giới thiệu
- Bài viết của tạp chí về chuyển đổi một bộ thành một danh sách