[Tutor] arrays

alan.gauld@bt.com alan.gauld@bt.com
Mon, 2 Apr 2001 15:31:01 +0100


------_=_NextPart_001_01C0BB81.8A6FC550
Content-type: text/plain; charset="iso-8859-1"

Can you put an array inside of another array? 
 

Usually, it depends on the language.
Since Python doesn't have arrays you need to use lists and
yes you can put a list inside amnother list:
 
>>> L1 = [1,2,3]
>>> L2 = ['a','b','c']
>>> L3 = [L1,L2]  # create nested lists
>>> print L3
[ [1, 2, 3], ['a', 'b', 'c'] ]
>>> print L3[1][1]   # how to access members
b
>>> L1.append(L2) # another way of nesting lists
>>> print L1
[1, 2, 3, ['a', 'b', 'c'] ]
 
HTH,
 
Alan G

 


------_=_NextPart_001_01C0BB81.8A6FC550
Content-type: text/html; charset="iso-8859-1"

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">


<META content="MSHTML 5.00.3013.2600" name=GENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=#ffffff>
<BLOCKQUOTE 
style="BORDER-LEFT: #0000ff 2px solid; MARGIN-LEFT: 5px; MARGIN-RIGHT: 0px; PADDING-LEFT: 5px">
  <DIV><FONT size=2><FONT face=Arial>Can you put an array inside of another 
  array?<FONT color=#0000ff><SPAN 
  class=730512614-02042001>&nbsp;</SPAN></FONT></FONT></FONT></DIV>
  <DIV><FONT size=2><FONT face=Arial><FONT color=#0000ff><SPAN 
  class=730512614-02042001></SPAN></FONT></FONT></FONT>&nbsp;</DIV></BLOCKQUOTE>
<DIV><FONT size=2><FONT face=Arial><FONT color=#0000ff><SPAN 
class=730512614-02042001>Usually, it depends on the 
language.</SPAN></FONT></FONT></FONT></DIV>
<DIV><FONT size=2><FONT face=Arial><FONT color=#0000ff><SPAN 
class=730512614-02042001>Since Python doesn't have arrays you need to use 
lists&nbsp;and</SPAN></FONT></FONT></FONT></DIV>
<DIV><FONT size=2><FONT face=Arial><FONT color=#0000ff><SPAN 
class=730512614-02042001>yes you can put a list inside amnother 
list:</SPAN></FONT></FONT></FONT></DIV>
<DIV><FONT size=2><FONT face=Arial><FONT color=#0000ff><SPAN 
class=730512614-02042001></SPAN></FONT></FONT></FONT>&nbsp;</DIV>
<DIV><FONT size=2><FONT face=Arial><FONT color=#0000ff><SPAN 
class=730512614-02042001>&gt;&gt;&gt; L1 = 
[1,2,3]</SPAN></FONT></FONT></FONT></DIV>
<DIV><FONT size=2><FONT face=Arial><FONT color=#0000ff><SPAN 
class=730512614-02042001>&gt;&gt;&gt; L2 = 
['a','b','c']</SPAN></FONT></FONT></FONT></DIV>
<DIV><FONT size=2><FONT face=Arial><FONT color=#0000ff><SPAN 
class=730512614-02042001>&gt;&gt;&gt; L3 = [L1,L2]&nbsp; # create nested 
lists</SPAN></FONT></FONT></FONT></DIV><SPAN class=730512614-02042001>
<DIV><FONT color=#0000ff face=Arial size=2><SPAN 
class=730512614-02042001>&gt;&gt;&gt; print L3</SPAN></FONT></DIV>
<DIV><FONT size=2><FONT color=#0000ff><FONT face=Arial><SPAN 
class=730512614-02042001>[&nbsp;[1, 2, 3], ['a', 'b', 'c'] 
]</SPAN></FONT></FONT></FONT></DIV></SPAN>
<DIV><FONT size=2><FONT face=Arial><FONT color=#0000ff><SPAN 
class=730512614-02042001>&gt;&gt;&gt; print L3[1][1]&nbsp;&nbsp; # how to access 
members</SPAN></FONT></FONT></FONT></DIV>
<DIV><FONT size=2><FONT face=Arial><FONT color=#0000ff><SPAN 
class=730512614-02042001>b</SPAN></FONT></FONT></FONT></DIV>
<DIV><FONT size=2><FONT face=Arial><FONT color=#0000ff><SPAN 
class=730512614-02042001>&gt;&gt;&gt; L1.append(L2) # another way of nesting 
lists</SPAN></FONT></FONT></FONT></DIV>
<DIV><FONT size=2><FONT face=Arial><FONT color=#0000ff><SPAN 
class=730512614-02042001>&gt;&gt;&gt; print L1</SPAN></FONT></FONT></FONT></DIV>
<DIV><FONT size=2><FONT face=Arial><FONT color=#0000ff><SPAN 
class=730512614-02042001>[1, 2, 3, ['a', 'b', 'c'] 
]</SPAN></FONT></FONT></FONT></DIV>
<DIV><FONT size=2><FONT face=Arial><FONT color=#0000ff><SPAN 
class=730512614-02042001></SPAN></FONT></FONT></FONT>&nbsp;</DIV>
<DIV><FONT size=2><FONT face=Arial><FONT color=#0000ff><SPAN 
class=730512614-02042001>HTH,</SPAN></FONT></FONT></FONT></DIV>
<DIV><FONT size=2><FONT face=Arial><FONT color=#0000ff><SPAN 
class=730512614-02042001></SPAN></FONT></FONT></FONT>&nbsp;</DIV>
<DIV><FONT size=2><FONT face=Arial><FONT color=#0000ff><SPAN 
class=730512614-02042001>Alan G</SPAN></FONT></FONT></FONT></DIV>
<BLOCKQUOTE 
style="BORDER-LEFT: #0000ff 2px solid; MARGIN-LEFT: 5px; MARGIN-RIGHT: 0px; PADDING-LEFT: 5px">
  <DIV>&nbsp;</DIV></BLOCKQUOTE></BODY></HTML>

------_=_NextPart_001_01C0BB81.8A6FC550--