View Discussion
Improve Article
Save Article
View Discussion
Improve Article
Save Article
Sometimes, while working with records, we can have a problem in which we need to convert the data records to integer by joining them. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using reduce[] + lambda
The combination of above functions can be used to perform this task. In this, we use lambda function to perform logic of conversion and reduce performs task of iteration and combining result.
Python3
import
functools
test_tuple
=
[
1
,
4
,
5
]
print
[
"The original tuple : "
+
str
[test_tuple]]
res
=
functools.
reduce
[
lambda
sub, ele: sub
*
10
+
ele, test_tuple]
print
[
"Tuple to integer conversion : "
+
str
[res]]
Output :
The original tuple : [1, 4, 5] Tuple to integer conversion : 145
Method #2 : Using int[] + join[] + map[]
The combination of these functions can also be used to perform this task. In this, we convert each element to string using join[] and iterate using map[]. At last we perform integer conversion.
Python3
test_tuple
=
[
1
,
4
,
5
]
print
["The original
tuple
: "
+
str
[test_tuple]]
res
=
int
[''.join[
map
[
str
, test_tuple]]]
print
["
Tuple
to integer conversion : "
+
str
[res]]
Output :
The original tuple : [1, 4, 5] Tuple to integer conversion : 145
Method #3: Using str[] and int[] methods
Python3
test_tuple
=
[
1
,
4
,
5
]
print
[
"The original tuple : "
+
str
[test_tuple]]
res
=
""
for
i
in
test_tuple:
res
+
=
str
[i]
res
=
int
[res]
print
[
"Tuple to integer conversion : "
+
str
[res]]
Output
The original tuple : [1, 4, 5] Tuple to integer conversion : 145
Method: Using in operator + end[] function
Python3
test_tuple
=
[
1
,
4
,
5
]
for
i
in
test_tuple:
print
[i,end
=
""]
View Discussion
Improve Article
Save Article
View Discussion
Improve Article
Save Article
Interconversion of data is a popular problem developer generally deal with. One can face a problem to convert tuple string to integer tuple. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using tuple[] + int[] + replace[] + split[] The combination of above methods can be used to perform this task. In this, we perform the conversion using tuple[] and int[]. Extraction of elements is done by replace[] and split[].
Python3
test_str
=
"[
7
,
8
,
9
]"
print
["The original string
is
: "
+
test_str]
res
=
tuple
[
int
[num]
for
num
in
test_str.replace[
'['
, '
'].replace['
]
', '
'].replace['
...
', '
'].split['
, ']]
print
["The
tuple
after conversion
is
: "
+
str
[res]]
Output :
The original string is : [7, 8, 9] The tuple after conversion is : [7, 8, 9]
Method #2: Using eval[] This is recommended method to solve this task. This performs the interconversion task internally.
Python3
test_str
=
"[
7
,
8
,
9
]"
print
["The original string
is
: "
+
test_str]
res
=
eval
[test_str]
print
["The
tuple
after conversion
is
: "
+
str
[res]]
Output :
The original string is : [7, 8, 9] The tuple after conversion is : [7, 8, 9]
Method #3: Using map[]
Python3
test_tuple
=
[
'1'
,
'4'
,
'3'
,
'6'
,
'7'
]
print
[
"Original tuple is : "
+
str
[test_tuple]]
test_tuple
=
tuple
[
map
[
int
, test_tuple]]
print
[
"Modified tuple is : "
+
str
[test_tuple]]
Output
Original tuple is : ['1', '4', '3', '6', '7'] Modified tuple is : [1, 4, 3, 6, 7]
Method: Using the list comprehension
Python3
tuple1
=
[
'1'
,
'4'
,
'3'
,
'6'
,
'7'
]
x
=
[
int
[i]
for
i
in
tuple1]
print
[
tuple
[x]]
Method: Using enumerate function
Python3
tuple1
=
[
'1'
,
'4'
,
'3'
,
'6'
,
'7'
]
x
=
[
int
[i]
for
a,i
in
enumerate
[tuple1]]
print
[
tuple
[x]]
Method: Using lambda function
Python3
tuple1
=
[
'1'
,
'4'
,
'3'
,
'6'
,
'7'
]
x
=
[
int
[j]
for
j
in
[
tuple
[
filter
[
lambda
i:[i],tuple1]]]]
print
[
tuple
[x]]