tags:

views:

82

answers:

4

I need to put 3 items in the list.

  1. state..eg(1,2)
  2. action...eg..west
  3. cost....5

list=[(state,action,cost), (state,action,cost).........]

how can i make it in a form of list. For a particular state , action and cost is there. Moreover, if i need only state from the list, i should be able to extract it from the list.same thing goes for action too.

+1  A: 

I'm not sure I understand the first part of your question ("form of a list"). You can construct the list of tuples in the form you've stated:

 mylist = [(1, 'west', 5), (1, 'east', 3), (2, 'west', 6)]
 # add another tuple 
 mylist.append((2, 'east', 7))

To extract only the states or actions (i.e. the first or second item in each tuple), you can use list comprehensions:

states = [item[0] for item in mylist]
actions = [item[1] for item in mylist]
ars
A: 

The code you listed above is a list of tuples - which pretty much matches what you're asking for.

From the example above, list[0][0] returns the state from the first tuple, list[0][1] the action, and list[0][2] the cost.

You can extract the values with something like (state, action, cost)= list[i] too.

heckj
+3  A: 

Your wording is pretty obscure. Right now you have a list of tuples (horribly named list, which robs the use of the built-in name from the rest of that scope -- please don't reuse built-in names as your own identifiers... call them alist or mylist, if you can't think of a more meaningful and helpful name!). If you want a list of lists, code:

alist = [[state, action, cost], [state, action, cost], ...]

If you want to transform the list of tuples in a list of lists,

alist = [list(t) for t in alist]

(see why you should never usurp built-in identifiers such as list?!-).

If you want to flatten the list-of-lists (or -of-tuples) into a single list,

aflatlist = [x for t in alist for x in t]

To access e.g. "just the state" (first item), say of the Nth item in the list,

justthestate = alist[N][0]

or, if you've flattened it,

justhestate = aflatlist[N*3 + 0]

(the + 0 is clearly redundant, but it's there to show you what to do for the cost, which would be at + 1, etc).

If you want a list with all states,

allstates = [t[0] for t in alist]

or

allstates = aflatlist[0::3]

I'm sure you could mean something even different from this dozen of possible interpretations of your arcane words, but I'm out of juice by now;-).

Alex Martelli
u r genius..thanks...can u also help me in my other question that I posted before....http://stackoverflow.com/questions/3252217/help-in-executing-the-code
Shilpa
@Shilpa, fundamental SO etiquette point #1: you should _accept_ the answer that most helps you (by clicking on the checkmark-shaped icon next to it) -- point #2, which comes into play only once you reach a reputation of 15, is that you should also _upvote_ answers that are good (you can accept only one, but will be able to upvote as many as you'll like, no doubt including the one you accept, once you do get that rep). You'll find that, unless you do this, people won't choose to spend that much time and energy answering your questions.
Alex Martelli
A: 

try this

l = [('a',1,2),('b',3,4),('a',4,6), ('a',6,8)]

state = [sl[0] for sl in l]
or for more,
state = [sl[0] for sl in l if 'a' in sl]
nazmul hasan