I have a tuple with two numbers in it, I need to get both numbers. The first number is the x-coordinate, while the second is the y-coordinate. My pseudo code is my idea about how to go about it, however I'm not quite sure how to make it work.
pseudo code:
tuple = [46, 153]
string = str[tuple]
ss = string.search[]
int1 = first_int[ss]
int2 = first_int[ss]
print int1
print int2
int1 would return 46, while int2 would return 153.
SilentGhost
294k64 gold badges301 silver badges291 bronze badges
asked Jul 20, 2010 at 8:37
rectangletanglerectangletangle
48k91 gold badges198 silver badges272 bronze badges
3
answered Jul 20, 2010 at 8:39
The other way is to use array subscripts:
int1 = tuple[0]
int2 = tuple[1]
This is useful if you find you only need to access one member of the tuple at some point.
answered Jul 20, 2010 at 8:40
SkilldrickSkilldrick
67.7k33 gold badges172 silver badges227 bronze badges
The third way is to use the new namedtuple type:
from collections import namedtuple
Coordinates = namedtuple['Coordinates','x,y']
coords = Coordinates[46,153]
print coords
print 'x coordinate is:',coords.x,'y coordinate is:',coords.y
answered Jul 20, 2010 at 10:48
a way better way is using *
:
a = [1,2,3]
b = [*a]
print[b]
it gives you a list
answered Mar 18, 2020 at 22:28
JasonJason
2,8202 gold badges19 silver badges33 bronze badges
Returns a match where the string contains digits [numbers from 0-9]
import re
tl = [[1, 11], [5, 9] , [6,3]]
list1 = re.findall[r'\d+',str[tl]]
tlstr = ''.join[list1]
num = list[set[tlstr]]
print[num]
Ty Hitzeman
8351 gold badge13 silver badges24 bronze badges
answered Feb 25, 2021 at 16:18
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
=
""]