I want to form a tuple with the first elements of each tuple. Im a beginner in python, maybe this is an easy one but i cant find a way to do it plz help.
ex:
input
x = (('A','B','C'),('D','E','F'),('G','H','I'))
output:
y= ('A', 'D', 'G')
x = (('A','B','C'),('D','E','F'),('G','H','I'))
for each in zip(*x):
print(each)
prints:
('A', 'D', 'G')
('B', 'E', 'H')
('C', 'F', 'I')
Use the zip()
function. Read more here.
Edited:
like CAB says if you just want the first one you can do:
y = zip(*x)[0]
Use comprehension and convert to a tuple;
>>> x = (('A','B','C'),('D','E','F'),('G','H','I'))
>>> y = tuple([l[0] for l in x])
>>> y
('A', 'D', 'G')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.