Cải thiện bài viết
Lưu bài viết
Cải thiện bài viết
Lưu bài viết
Đọc
Bàn luận
In this method, we convert the string to list and then append to target list and then convert this result list to tuple using tuple[].
Đôi khi, chúng ta có thể có một vấn đề trong đó chúng ta cần xây dựng một container mới với các phần tử từ các thùng chứa khác nhau. Loại vấn đề này có thể xảy ra trong các miền trong đó chúng tôi sử dụng các loại dữ liệu khác nhau. Hãy để thảo luận về các cách để chuyển đổi chuỗi và liệt kê dữ liệu thành tuple.
Phương pháp số 1: Sử dụng chuyển đổi danh sách thành Tuple + tuple[]
Trong phương thức này, chúng tôi chuyển đổi chuỗi thành danh sách và sau đó nối vào danh sách đích và sau đó chuyển đổi danh sách kết quả này thành Tuple bằng Tuple [].
test_list
=
[
"gfg"
,
The original list : ['gfg', 'is'] The original string : best The aggregated tuple is : ['gfg', 'is', 'best']0
The original list : ['gfg', 'is'] The original string : best The aggregated tuple is : ['gfg', 'is', 'best']1
The original list : ['gfg', 'is'] The original string : best The aggregated tuple is : ['gfg', 'is', 'best']2
=
The original list : ['gfg', 'is'] The original string : best The aggregated tuple is : ['gfg', 'is', 'best']4
The original list : ['gfg', 'is'] The original string : best The aggregated tuple is : ['gfg', 'is', 'best']5
The original list : ['gfg', 'is'] The original string : best The aggregated tuple is : ['gfg', 'is', 'best']6
The original list : ['gfg', 'is'] The original string : best The aggregated tuple is : ['gfg', 'is', 'best']7
The original list : ['gfg', 'is'] The original string : best The aggregated tuple is : ['gfg', 'is', 'best']8
The original list : ['gfg', 'is'] The original string : best The aggregated tuple is : ['gfg', 'is', 'best']9
The original string : 1, -5, 4, 6, 7 Tuple after getting conversion from String : [1, -5, 4, 6, 7]0
The original list : ['gfg', 'is'] The original string : best The aggregated tuple is : ['gfg', 'is', 'best']5
The original list : ['gfg', 'is'] The original string : best The aggregated tuple is : ['gfg', 'is', 'best']6
The original string : 1, -5, 4, 6, 7 Tuple after getting conversion from String : [1, -5, 4, 6, 7]3
The original list : ['gfg', 'is'] The original string : best The aggregated tuple is : ['gfg', 'is', 'best']8
The original string : 1, -5, 4, 6, 7 Tuple after getting conversion from String : [1, -5, 4, 6, 7]5
The original string : 1, -5, 4, 6, 7
Tuple after getting conversion from String : [1, -5, 4, 6, 7]
6=
The original string : 1, -5, 4, 6, 7
Tuple after getting conversion from String : [1, -5, 4, 6, 7]
8The original string : 1, -5, 4, 6, 7
Tuple after getting conversion from String : [1, -5, 4, 6, 7]
9The original list : ['gfg', 'is']
The original string : best
The aggregated tuple is : ['gfg', 'is', 'best']
8 The original string : 1, -5, 4, 6, 7
Tuple after getting conversion from String : [1, -5, 4, 6, 7]
1
The original list : ['gfg', 'is'] The original string : best The aggregated tuple is : ['gfg', 'is', 'best']
The original list : ['gfg', 'is']
The original string : best
The aggregated tuple is : ['gfg', 'is', 'best']
5The original list : ['gfg', 'is']
The original string : best
The aggregated tuple is : ['gfg', 'is', 'best']
6The original string : 1, -5, 4, 6, 7
Tuple after getting conversion from String : [1, -5, 4, 6, 7]
4 The original list : ['gfg', 'is']
The original string : best
The aggregated tuple is : ['gfg', 'is', 'best']
8 The original list : ['gfg', 'is']
The original string : best
The aggregated tuple is : ['gfg', 'is', 'best']
9The original string : 1, -5, 4, 6, 7
Tuple after getting conversion from String : [1, -5, 4, 6, 7]
7
This is another way in which this task can be performed. In this, we convert the string and list both to tuple and add them to result tuple. This method is more efficient than the above method.
Đầu ra:
Phương pháp số 2: Sử dụng chuyển đổi Tuple sang Tuple +________ 38 Đây là một cách khác để thực hiện nhiệm vụ này. Trong đó, chúng tôi chuyển đổi chuỗi và liệt kê cả hai thành tuple và thêm chúng để kết quả. Phương pháp này hiệu quả hơn phương pháp trên.
test_list
=
[
"gfg"
,
The original list : ['gfg', 'is'] The original string : best The aggregated tuple is : ['gfg', 'is', 'best']0
The original list : ['gfg', 'is'] The original string : best The aggregated tuple is : ['gfg', 'is', 'best']1
The original list : ['gfg', 'is'] The original string : best The aggregated tuple is : ['gfg', 'is', 'best']2
=
The original list : ['gfg', 'is'] The original string : best The aggregated tuple is : ['gfg', 'is', 'best']4
The original list : ['gfg', 'is'] The original string : best The aggregated tuple is : ['gfg', 'is', 'best']5
The original list : ['gfg', 'is'] The original string : best The aggregated tuple is : ['gfg', 'is', 'best']6
The original list : ['gfg', 'is'] The original string : best The aggregated tuple is : ['gfg', 'is', 'best']7
The original list : ['gfg', 'is'] The original string : best The aggregated tuple is : ['gfg', 'is', 'best']8
The original list : ['gfg', 'is'] The original string : best The aggregated tuple is : ['gfg', 'is', 'best']9
The original string : 1, -5, 4, 6, 7 Tuple after getting conversion from String : [1, -5, 4, 6, 7]0
The original list : ['gfg', 'is'] The original string : best The aggregated tuple is : ['gfg', 'is', 'best']5
The original list : ['gfg', 'is'] The original string : best The aggregated tuple is : ['gfg', 'is', 'best']6
The original string : 1, -5, 4, 6, 7 Tuple after getting conversion from String : [1, -5, 4, 6, 7]3
The original list : ['gfg', 'is'] The original string : best The aggregated tuple is : ['gfg', 'is', 'best']8
The original string : 1, -5, 4, 6, 7 Tuple after getting conversion from String : [1, -5, 4, 6, 7]5
The original string : 1, -5, 4, 6, 7
Tuple after getting conversion from String : [1, -5, 4, 6, 7]
6=
The original string : 1, -5, 4, 6, 7
Tuple after getting conversion from String : [1, -5, 4, 6, 7]
8The original string : 1, -5, 4, 6, 7
Tuple after getting conversion from String : [1, -5, 4, 6, 7]
9The original list : ['gfg', 'is']
The original string : best
The aggregated tuple is : ['gfg', 'is', 'best']
8 The original string : 1, -5, 4, 6, 7
Tuple after getting conversion from String : [1, -5, 4, 6, 7]
1
The original list : ['gfg', 'is'] The original string : best The aggregated tuple is : ['gfg', 'is', 'best']
Cải thiện bài viết
Lưu bài viết
Cải thiện bài viết
Lưu bài viết
Đọc
Bàn luận
Sự xen kẽ của các loại dữ liệu là vấn đề rất phổ biến mà người ta có thể gặp phải trong khi lập trình. Có thể có một vấn đề trong đó chúng ta cần chuyển đổi một chuỗi số nguyên thành một tuple. Hãy để thảo luận về những cách nhất định trong đó điều này có thể được thực hiện. & NBSP;
Python3
Phương thức số 1: Sử dụng map [] + int + split [] + tuple [] & nbsp;
Phương pháp này có thể được sử dụng để giải quyết nhiệm vụ cụ thể này. Trong đó, chúng tôi chỉ chia từng phần tử của chuỗi và chuyển đổi thành danh sách và sau đó chúng tôi chuyển đổi danh sách thành kết quả Tuple. & NBSP;
The original list : ['gfg', 'is'] The original string : best The aggregated tuple is : ['gfg', 'is', 'best']2
=
[
4[
5,
__777778The original list : ['gfg', 'is'] The original string : best The aggregated tuple is : ['gfg', 'is', 'best']5
"gfg"
7The original list : ['gfg', 'is'] The original string : best The aggregated tuple is : ['gfg', 'is', 'best']8
The original list : ['gfg', 'is'] The original string : best The aggregated tuple is : ['gfg', 'is', 'best']9
,
0The original string : 1, -5, 4, 6, 7
Tuple after getting conversion from String : [1, -5, 4, 6, 7]
6=
The original string : 1, -5, 4, 6, 7
Tuple after getting conversion from String : [1, -5, 4, 6, 7]
8The original list : ['gfg', 'is']
The original string : best
The aggregated tuple is : ['gfg', 'is', 'best']
6,
5The original list : ['gfg', 'is']
The original string : best
The aggregated tuple is : ['gfg', 'is', 'best']
6,
7"gfg"
,
9The original list : ['gfg', 'is']
The original string : best
The aggregated tuple is : ['gfg', 'is', 'best']
00
The original string : 1, -5, 4, 6, 7 Tuple after getting conversion from String : [1, -5, 4, 6, 7]
The original list : ['gfg', 'is']
The original string : best
The aggregated tuple is : ['gfg', 'is', 'best']
5The original list : ['gfg', 'is']
The original string : best
The aggregated tuple is : ['gfg', 'is', 'best']
022Method #2 : Using eval[]
Đầu ra: & nbsp;
Python3
Phương thức số 1: Sử dụng map [] + int + split [] + tuple [] & nbsp;
Phương pháp này có thể được sử dụng để giải quyết nhiệm vụ cụ thể này. Trong đó, chúng tôi chỉ chia từng phần tử của chuỗi và chuyển đổi thành danh sách và sau đó chúng tôi chuyển đổi danh sách thành kết quả Tuple. & NBSP;
The original list : ['gfg', 'is'] The original string : best The aggregated tuple is : ['gfg', 'is', 'best']2
=
[
4[
5,
__777778The original list : ['gfg', 'is'] The original string : best The aggregated tuple is : ['gfg', 'is', 'best']5
"gfg"
7The original list : ['gfg', 'is'] The original string : best The aggregated tuple is : ['gfg', 'is', 'best']8
The original list : ['gfg', 'is'] The original string : best The aggregated tuple is : ['gfg', 'is', 'best']9
,
0The original string : 1, -5, 4, 6, 7
Tuple after getting conversion from String : [1, -5, 4, 6, 7]
6=
The original string : 1, -5, 4, 6, 7
Tuple after getting conversion from String : [1, -5, 4, 6, 7]
8The original list : ['gfg', 'is']
The original string : best
The aggregated tuple is : ['gfg', 'is', 'best']
6,
5The original list : ['gfg', 'is']
The original string : best
The aggregated tuple is : ['gfg', 'is', 'best']
6,
7"gfg"
,
9The original list : ['gfg', 'is']
The original string : best
The aggregated tuple is : ['gfg', 'is', 'best']
00
The original string : 1, -5, 4, 6, 7 Tuple after getting conversion from String : [1, -5, 4, 6, 7]
The original list : ['gfg', 'is']
The original string : best
The aggregated tuple is : ['gfg', 'is', 'best']
5The original list : ['gfg', 'is']
The original string : best
The aggregated tuple is : ['gfg', 'is', 'best']
022
Python3
Đầu ra: & nbsp;
& nbsp; Phương pháp số 2: Sử dụng eval [] & nbsp;
The original list : ['gfg', 'is'] The original string : best The aggregated tuple is : ['gfg', 'is', 'best']5
The original list : ['gfg', 'is'] The original string : best The aggregated tuple is : ['gfg', 'is', 'best']53
Đây là tốc ký để thực hiện nhiệm vụ này. Điều này chuyển đổi chuỗi thành tuple mong muốn trong nội bộ. & Nbsp;
The original string : 1, -5, 4, 6, 7 Tuple after getting conversion from String : [1, -5, 4, 6, 7]6
=
The original list : ['gfg', 'is'] The original string : best The aggregated tuple is : ['gfg', 'is', 'best']31
The original list : ['gfg', 'is'] The original string : best The aggregated tuple is : ['gfg', 'is', 'best']32
Phương pháp số 3: Sử dụng phân chia và danh sách hiểu & nbsp;
The original list : ['gfg', 'is'] The original string : best The aggregated tuple is : ['gfg', 'is', 'best']5
The original list : ['gfg', 'is'] The original string : best The aggregated tuple is : ['gfg', 'is', 'best']6
The original string : 1, -5, 4, 6, 7 Tuple after getting conversion from String : [1, -5, 4, 6, 7]8
The original list : ['gfg', 'is'] The original string : best The aggregated tuple is : ['gfg', 'is', 'best']78