Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm trying to call up an element from an array within an array in Python. For example:

array = [[a1,a2,a3,a4], [b1,b2,b3,b4], [c1,c2,c3,c4]]

The question is, how would I print just the value b1?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
159 views
Welcome To Ask or Share your Answers For Others

1 Answer

To access b1, do this:

print array[1][0]

See an example:

>>> array=[['a1','a2','a3','a4'],['b1','b2','b3','b4'],['c1','c2','c3','c4']]
>>> array[1]
['b1', 'b2', 'b3', 'b4']
>>> array[1][0]
'b1'
>>>

Basically, you are indexing array at position 1 (which returns the b list), and then indexing that list at position 0 (which returns b1).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...