开发者

Printing the values of a tuple [closed]

开发者_开发百科 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 10 years ago.

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]

??

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜