Printing the values of a tuple [closed]
Accessing tuple values
How to access the value of a and b in the following
>> t=[]
>> t.append(("a" , 1))
>> t.append(("b" , 2))
>> print t[0][0]
a
>> print t[1][0]
b
How to print the values of a and b
Just do
print t[0][1]
print t[1][1]
But of course if you really want to look up for a or b, this is not the best construct, then you need a dict:
t = {}
t["a"] = 1
t["b"] = 2
print t["a"]
print t["b"]
It's simpler than expected:
>> t=[]
>> t.append(("a" , 1))
>> t.append(("b" , 2))
>> dict(t).get('a')
# 1
Happy Coding.
>>> t
[('a', 1), ('b', 2)]
>>> t[0][1]
1
>>> t[1][1]
2
print t[0][1]
print t[1][1]
??
精彩评论