user1416451
user1416451

Reputation:

Splitting a python list into multiple lists

for example, if i have a list like:

one = [1,2,3]

what function or method can i use to split each element into their own separate list like:

one = [1]

RANDOM_DYNAMIC_NAME = [2]

RANDOM_DYNAMIC_NAME_AGAIN = [3]

and at any given time, the unsplit list called one may have more than 1 element, its dynamic, and this algorithm is needed for a hangman game i am coding as self-given homework.

the algorithm is needed to complete this example purpose:

pick a word: mississippi

guess a letter: s

['_','_','s','s','_','s','s','_','_','_','_']

Here is my code:

http://pastebin.com/gcCZv67D

Upvotes: 1

Views: 3281

Answers (3)

abarnert
abarnert

Reputation: 366213

Looking at your code, if the part you're trying to solve is the comments in lines 24-26, you definitely don't need dynamically-created variables for that at all, and in fact I can't even imagine how they could help you.

You've got this:

enum = [i for i,x in enumerate(letterlist) if x == word]

The names of your variables are very confusing—something called word is the guessed letter, while you've got a different variable letterguess that's something else, and then a variable called letter that's the whole word… But I think I get what you're aiming for.

enum is a list of all of the indices of word within letterlist. For example, if letterlist is 'letter' and word is t, it will be [2, 3].

Then you do this:

bracketstrip = (str(w) for w in enum)

So now bracketstrip is ['2', '3']. I'm not sure why you want that.

z = int(''.join(bracketstrip))

And ''.join(bracketstrip) is '23', so z is 23.

letterguess[z] = word

And now you get an IndexError, because you're trying to set letterguess[23] instead of setting letterguess[2] and letterguess[3].

Here's what I think you want to replace that with:

enum = [i for i,x in enumerate(letterlist) if x == word]
for i in enum:
    letterguess[i] = word

A few hints about some other parts of your code:

You've got a few places where you do things like this:

letterlist = []
for eachcharacter in letter:
  letterlist.append(eachcharacter)

This is the same as letterlist = list(letter). But really, you don't need that list at all. The only thing you do with that is for i, x in enumerate(letterlist), and you could have done the exact same thing with letter in the first place. You're generally making things much harder for yourself than you have to. Make sure you actually understand why you've written each line of code.

"Because I couldn't get it to work any other way" isn't a reason—what were you trying to get to work? Why did you think you needed a list of letters? Nobody can keep all of those decisions in their head at once. The more skill you have, the more of your code will be so obvious to you that it doesn't need comments, but you'll never get to the point where you don't need any. When you're just starting out, every time you figure out how to do something, add a comment reminding yourself what you were trying to do, and why it works. You can always remove comments later; you can never get back comments that you didn't write.

Upvotes: 4

Antimony
Antimony

Reputation: 39511

As for a literal answer to your question, here's how you do it, though I can't immagine why you would want to to this. Generally, dynamic variable names are poor design. You probably just want a single list, or list of lists.

import random

for x in one:
    name = 'x' + str(random.getrandbits(10))
    globals()[name] = [x]

Upvotes: -1

Shawn Zhang
Shawn Zhang

Reputation: 1884

for question one ,just list comprehension is good . it will return each element as a separate list

[ [x,] for x in one ]

Upvotes: 0

Related Questions