2

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.

Philipp
50.1k12 gold badges88 silver badges112 bronze badges
asked Jul 15, 2010 at 5:34
1
  • 2
    What exactly is your question? Commented Jul 15, 2010 at 6:02

4 Answers 4

3

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;-).

answered Jul 15, 2010 at 5:45
Sign up to request clarification or add additional context in comments.

2 Comments

u r genius..thanks...can u also help me in my other question that I posted before....stackoverflow.com/questions/3252217/help-in-executing-the-code
@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.
1

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]
answered Jul 15, 2010 at 5:42

Comments

0

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.

answered Jul 15, 2010 at 5:43

Comments

0

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]
answered Jul 15, 2010 at 5:45

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.