Help with nested list 
July 2nd, 2009, 12:34 PM
| | Newbie | | Join Date: Jul 2009
Posts: 3
| |
Hi,
I need help creating nested lists
my code follows - test = ["a","b","c"]
-
testtwo = ["e","f","g"]
-
-
mo = []
-
for i in range (0,len(test)):
-
for j in range(0,len(testweb)):
-
moa = test[i]+testweb[j]
-
mo.append[moa]
I want final output like this
mo[ae[],af[],ag[],be[], ....... cg[]] instead of mo[ae,af,ag,be, ....... cg]
How to get this??? I've searched in this forum and couldn't find an answer.
Thanks,
Rajasankar
| 
July 2nd, 2009, 03:35 PM
|  | Moderator | | Join Date: Oct 2006 Location: Nashville, TN
Posts: 1,545
| | | re: Help with nested list
Your desired output is invalid. The brackets '[]' represent a slice object. What you are wanting is similar to: - >>> s = slice(10,20)
-
>>> s.indices(15)
-
(10, 15, 1)
-
>>> s.indices()
-
Traceback (most recent call last):
-
File "<interactive input>", line 1, in ?
-
TypeError: indices() takes exactly one argument (0 given)
-
>>>
You may want a dictionary with empty lists as values. - >>> test = ["a","b","c"]
-
>>> testtwo = ["e","f","g"]
-
>>> mo = {}
-
>>> for i in test:
-
... for j in testtwo:
-
... mo.setdefault(i+j, [])
-
...
-
[]
-
[]
-
[]
-
[]
-
[]
-
[]
-
[]
-
[]
-
[]
-
>>> mo
-
{'be': [], 'bf': [], 'bg': [], 'ae': [], 'ag': [], 'af': [], 'cg': [], 'cf': [], 'ce': []}
-
>>>
| 
July 3rd, 2009, 03:29 AM
| | Newbie | | Join Date: Jul 2009
Posts: 3
| | | re: Help with nested list
Hi,
Thanks for this info. I used the dict in Python and it worked.
Thanks again.
Rajasankar
| | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 225,662 network members.
|