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
Bàn luận
Sự xen kẽ của các loại dữ liệu là một vấn đề khá phổ biến trong lập trình. Đôi khi chúng ta cần chuyển đổi một số duy nhất thành danh sách các số nguyên và chúng tôi không muốn dành một số dòng mã để làm điều đó. Do đó, có cách để thực hiện nhiệm vụ này bằng cách sử dụng tốc ký có ích. Hãy để thảo luận về những cách mà điều này có thể được thực hiện. & NBSP;can be used as a shorthand for the longer format of the naive method. In this method, we convert the number to a string and then extract each character and re-convert it to an integer.
Python3
Phương pháp số 1: Sử dụng danh sách hiểu
ITCAN được sử dụng như một tốc ký cho định dạng dài hơn của phương pháp ngây thơ. Trong phương thức này, chúng tôi chuyển đổi số thành một chuỗi và sau đó trích xuất từng ký tự và chuyển đổi lại nó thành một số nguyên. & NBSP;
num
=
The original number is 2019 The list from number is [2, 0, 1, 9]0
The original number is 2019 The list from number is [2, 0, 1, 9]1
The original number is 2019 The list from number is [2, 0, 1, 9]2
The original number is 2019 The list from number is [2, 0, 1, 9]3
The original number is 2019 The list from number is [2, 0, 1, 9]4
The original number is 2019 The list from number is [2, 0, 1, 9]5
The original number is 2019 The list from number is [2, 0, 1, 9]6
The original number is 2019
The list from number is [2, 0, 1, 9]
7=
The original number is 2019
The list from number is [2, 0, 1, 9]
9['2', '0', '1', '9']
0['2', '0', '1', '9']
1['2', '0', '1', '9']
2 ['2', '0', '1', '9']
3['2', '0', '1', '9']
4
The original number is 2019 The list from number is [2, 0, 1, 9]
The original number is 2019
The list from number is [2, 0, 1, 9]
1The original number is 2019
The list from number is [2, 0, 1, 9]
2['2', '0', '1', '9']
9 The original number is 2019
The list from number is [2, 0, 1, 9]
4 The original number is 2019
The list from number is [2, 0, 1, 9]
5Input : [1, 2, 3]
Output : 123
Input : [55, 32, 890]
Output : 5532890
2 map function can be used to perform the following task converting each of the string converted numbers to the desired integer value to be
reconverted to the list format.
Python3
Phương pháp số 1: Sử dụng danh sách hiểu
ITCAN được sử dụng như một tốc ký cho định dạng dài hơn của phương pháp ngây thơ. Trong phương thức này, chúng tôi chuyển đổi số thành một chuỗi và sau đó trích xuất từng ký tự và chuyển đổi lại nó thành một số nguyên. & NBSP;
num
=
The original number is 2019 The list from number is [2, 0, 1, 9]0
The original number is 2019 The list from number is [2, 0, 1, 9]1
The original number is 2019 The list from number is [2, 0, 1, 9]2
The original number is 2019 The list from number is [2, 0, 1, 9]3
The original number is 2019 The list from number is [2, 0, 1, 9]4
The original number is 2019 The list from number is [2, 0, 1, 9]5
The original number is 2019 The list from number is [2, 0, 1, 9]6
The original number is 2019
The list from number is [2, 0, 1, 9]
7=
The original number is 2019
The list from number is [2, 0, 1, 9]
9['2', '0', '1', '9']
0['2', '0', '1', '9']
1['2', '0', '1', '9']
2 ['2', '0', '1', '9']
3['2', '0', '1', '9']
4
The original number is 2019 The list from number is [2, 0, 1, 9]
The original number is 2019
The list from number is [2, 0, 1, 9]
1The original number is 2019
The list from number is [2, 0, 1, 9]
2['2', '0', '1', '9']
9 The original number is 2019
The list from number is [2, 0, 1, 9]
4 The original number is 2019
The list from number is [2, 0, 1, 9]
5Input : [1, 2, 3]
Output : 123
Input : [55, 32, 890]
Output : 5532890
2 Using enumerate function
Python3
Đầu ra
Phương pháp số 2: Sử dụng hàm MAP [] có thể được sử dụng để thực hiện tác vụ sau khi chuyển đổi từng số được chuyển đổi chuỗi thành giá trị số nguyên mong muốn để được chuyển đổi thành định dạng danh sách. & NBSP;
The original number is 2019 The list from number is [2, 0, 1, 9]1
1234
The original number is 2019
The list from number is [2, 0, 1, 9]
7=
121517
4The original number is 2019
The list from number is [2, 0, 1, 9]
2121517
6The original number is 2019
The list from number is [2, 0, 1, 9]
2['2', '0', '1', '9']
0121517
9The original number is 2019
The list from number is [2, 0, 1, 9]
5123
1
Python3
1238
=
The original number is 2019 The list from number is [2, 0, 1, 9]0
Phương pháp số 3: Sử dụng chức năng liệt kê
The original number is 2019 The list from number is [2, 0, 1, 9]1
=
1The original number is 2019
The list from number is [2, 0, 1, 9]
7=
The original number is 2019
The list from number is [2, 0, 1, 9]
9['2', '0', '1', '9']
0['2', '0', '1', '9']
1['2', '0', '1', '9']
2 ['2', '0', '1', '9']
3['2', '0', '1', '9']
4
['2', '0', '1', '9']
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
ĐọcExamples:
Input : [1, 2, 3] Output : 123 Input : [55, 32, 890] Output : 5532890
Bàn luậnApproach #1 : Naive Method Simply iterate each element in the list and print them without space in between.
Python3
Đưa ra một danh sách các số nguyên, hãy viết một chương trình Python để chuyển đổi danh sách đã cho thành một số nguyên duy nhất. Ví dụ:
Có nhiều cách tiếp cận có thể để chuyển đổi danh sách đã cho thành một số nguyên duy nhất. Hãy cùng xem từng người một. Cách tiếp cận số 1: Phương pháp ngây thơ chỉ đơn giản là lặp lại từng phần tử trong danh sách và in chúng không có khoảng trống ở giữa. & NBSP;
The original number is 2019 The list from number is [2, 0, 1, 9]05
The original number is 2019 The list from number is [2, 0, 1, 9]1
The original number is 2019 The list from number is [2, 0, 1, 9]07
=
The original number is 2019 The list from number is [2, 0, 1, 9]09
Output:
121517
=
2=
The original number is 2019 The list from number is [2, 0, 1, 9]9
=
51215179
=
7____49=
9The original number is 2019 The list from number is [2, 0, 1, 9]00Approach #2 : Using join[] Use the join[] method of Python. First convert the list of integer into a list of strings[ as join[] works with strings only]. Then, simply join them using join[] method. It takes a time complexity of O[n].
Python3
['2', '0', '1', '9']2
The original number is 2019 The list from number is [2, 0, 1, 9]0224
The original number is 2019 The list from number is [2, 0, 1, 9]04
& nbsp; Cách tiếp cận số 2: Sử dụng tham gia [] Sử dụng phương thức python tham gia []. Đầu tiên chuyển đổi danh sách số nguyên thành một danh sách các chuỗi [AS tham gia [] chỉ hoạt động với các chuỗi]. Sau đó, chỉ cần tham gia chúng bằng phương thức Jop []. Phải mất một độ phức tạp về thời gian của O [n]. & NBSP;
The original number is 2019 The list from number is [2, 0, 1, 9]10
The original number is 2019 The list from number is [2, 0, 1, 9]11
1215174
The original number is 2019 The list from number is [2, 0, 1, 9]13
The original number is 2019 The list from number is [2, 0, 1, 9]05
The original number is 2019 The list from number is [2, 0, 1, 9]31
1234
The original number is 2019 The list from number is [2, 0, 1, 9]05
The original number is 2019 The list from number is [2, 0, 1, 9]15
=
The original number is 2019 The list from number is [2, 0, 1, 9]9
The original number is 2019 The list from number is [2, 0, 1, 9]5
The original number is 2019 The list from number is [2, 0, 1, 9]19__
The original number is 2019 The list from number is [2, 0, 1, 9]1
The original number is 2019 The list from number is [2, 0, 1, 9]43
1215174
The original number is 2019 The list from number is [2, 0, 1, 9]45
Output:
123
The original number is 2019 The list from number is [2, 0, 1, 9]05
The original number is 2019 The list from number is [2, 0, 1, 9]7
=
['2', '0', '1', '9']0
The original number is 2019 The list from number is [2, 0, 1, 9]29Approach #3 : Using map[] Another approach to convert a list of multiple integers into a single integer is to use map[] function of Python with str function to convert the Integer list to string list. After this, join them on the empty string and then cast back to integer.
Python3
['2', '0', '1', '9']2
The original number is 2019 The list from number is [2, 0, 1, 9]0224
The original number is 2019 The list from number is [2, 0, 1, 9]04
& nbsp; Cách tiếp cận số 2: Sử dụng tham gia [] Sử dụng phương thức python tham gia []. Đầu tiên chuyển đổi danh sách số nguyên thành một danh sách các chuỗi [AS tham gia [] chỉ hoạt động với các chuỗi]. Sau đó, chỉ cần tham gia chúng bằng phương thức Jop []. Phải mất một độ phức tạp về thời gian của O [n]. & NBSP;
The original number is 2019 The list from number is [2, 0, 1, 9]10
The original number is 2019 The list from number is [2, 0, 1, 9]11
1215174
The original number is 2019 The list from number is [2, 0, 1, 9]13
The original number is 2019 The list from number is [2, 0, 1, 9]05
The original number is 2019 The list from number is [2, 0, 1, 9]15
=
The original number is 2019 The list from number is [2, 0, 1, 9]9
The original number is 2019 The list from number is [2, 0, 1, 9]5
The original number is 2019 The list from number is [2, 0, 1, 9]19__
The original number is 2019 The list from number is [2, 0, 1, 9]1
The original number is 2019 The list from number is [2, 0, 1, 9]43
1215174
The original number is 2019 The list from number is [2, 0, 1, 9]45
Output:
123
The original number is 2019 The list from number is [2, 0, 1, 9]05
The original number is 2019 The list from number is [2, 0, 1, 9]7
=
['2', '0', '1', '9']0
The original number is 2019 The list from number is [2, 0, 1, 9]29Approach #4 : Multiplying by corresponding power of 10 A more mathematical way, which does not require to convert the integer list to string list is, to multiply each integer element with its corresponding power of 10, and then summing it up. It takes a time complexity of O[n].
Python3
['2', '0', '1', '9']2
The original number is 2019 The list from number is [2, 0, 1, 9]0224
The original number is 2019 The list from number is [2, 0, 1, 9]04
& nbsp; Cách tiếp cận số 2: Sử dụng tham gia [] Sử dụng phương thức python tham gia []. Đầu tiên chuyển đổi danh sách số nguyên thành một danh sách các chuỗi [AS tham gia [] chỉ hoạt động với các chuỗi]. Sau đó, chỉ cần tham gia chúng bằng phương thức Jop []. Phải mất một độ phức tạp về thời gian của O [n]. & NBSP;
The original number is 2019 The list from number is [2, 0, 1, 9]05
The original number is 2019 The list from number is [2, 0, 1, 9]31
1234
The original number is 2019 The list from number is [2, 0, 1, 9]05
The original number is 2019 The list from number is [2, 0, 1, 9]15
=
The original number is 2019 The list from number is [2, 0, 1, 9]9
The original number is 2019 The list from number is [2, 0, 1, 9]5
The original number is 2019 The list from number is [2, 0, 1, 9]19__
The original number is 2019 The list from number is [2, 0, 1, 9]1
The original number is 2019 The list from number is [2, 0, 1, 9]43
1215174
The original number is 2019 The list from number is [2, 0, 1, 9]45
Output:
123
The original number is 2019
The list from number is [2, 0, 1, 9]
05The original number is 2019
The list from number is [2, 0, 1, 9]
7=
['2', '0', '1', '9']
0The original number is 2019
The list from number is [2, 0, 1, 9]
29
Python3
['2', '0', '1', '9']17
=
The original number is 2019 The list from number is [2, 0, 1, 9]9
The original number is 2019 The list from number is [2, 0, 1, 9]36
['2', '0', '1', '9']21
The original number is 2019 The list from number is [2, 0, 1, 9]38
['2', '0', '1', '9']21
The original number is 2019 The list from number is [2, 0, 1, 9]40
The original number is 2019 The list from number is [2, 0, 1, 9]00
1215174
=
The original number is 2019 The list from number is [2, 0, 1, 9]9
The original number is 2019 The list from number is [2, 0, 1, 9]36
1215179
The original number is 2019 The list from number is [2, 0, 1, 9]38
1215179
The original number is 2019 The list from number is [2, 0, 1, 9]40
The original number is 2019 The list from number is [2, 0, 1, 9]00
The original number is 2019 The list from number is [2, 0, 1, 9]1
The original number is 2019 The list from number is [2, 0, 1, 9]29
& nbsp; Cách tiếp cận số 3: Sử dụng Map [] Một cách tiếp cận khác để chuyển đổi danh sách nhiều số nguyên thành một số nguyên là sử dụng hàm map [] của python với hàm str để chuyển đổi danh sách số nguyên thành danh sách chuỗi. Sau đó, hãy tham gia với họ trên chuỗi trống và sau đó quay trở lại Integer. & NBSP;
Python3
The original number is 2019 The list from number is [2, 0, 1, 9]7
=
['2', '0', '1', '9']39____240
The original number is 2019 The list from number is [2, 0, 1, 9]2
num
4