user3013513
user3013513

Reputation: 23

How can I "condense" this code?

I'm new to python, so I'm probably using the incorrect terminology, but how would I "condense" this code?

            for i in self.letterWord[0:1]:
                    if i == letter:
                            self.binaryWord[0] = 1

            for i in self.letterWord[1:2]:
                    if i == letter:
                            self.binaryWord[1] = 1

            for i in self.letterWord[2:3]:
                    if i == letter:
                            self.binaryWord[2] = 1

Would it look something like this?

            for i in self.letterWord[i:i+1]:
                    if i == letter:
                            self.binaryWord[i]=1

I tried using the code above but it keeps saying

local variable 'i' referenced before assignment

Then, when I type in global i, it says

global name 'i' not defined

Hey guys! Thanks for all the responses! Sorry for the vague information, so thanks for answering my question anyways.

Upvotes: 2

Views: 700

Answers (4)

itdxer
itdxer

Reputation: 1256

One row solution

self.binaryWord = "%s%s" % (self.binaryWord[:3].replace(letter, "1"), self.binaryWord[3:])

And if you want change all letters in row you can do somethinf like this

self.binaryWord = self.binaryWord.replace(letter, "1")

Upvotes: 1

Random832
Random832

Reputation: 39050

First of all, your suggested code uses i to mean two different things. If anything, it would be:

    for l in self.letterWord[i:i+1]:
        if l == letter:
            self.binaryWord[i]=1

But also, self.letterWord[0:1] is going to contain exactly one item: self.letterWord[0]. Or nothing if it's out of range. So you don't need these for loops at all.

    if len(self.letterWord) < i and self.letterWord[i] == letter:
        self.binaryWord[i] = 1

You changed it to use i for the index; does that mean there is an outer loop? If you're looping over the length of letterWord, you don't need the len check I added.

for i in range(len(self.letterWord)):
    if self.letterWord[i] == letter:
        self.binaryWord[i] = 1

Also, looping over the indexes of a list isn't best practice, use enumerate if you need the index.

for i, l in enumerate(self.letterWord):
    if l == letter:
        self.binaryWord[i] = 1

And if you're just now creating binaryWord (you don't show how it's created), you could just create it as a list comprehension:

self.binaryWord = [int(l == letter) for l in self.letterWord]
# int(True) is 1, int(False) is 0

Upvotes: 1

chepner
chepner

Reputation: 532013

The problem with

for i in self.letterWord[i:i+1]:
    if i == letter:
        self.binaryWord[i]=1

is that i needs to be looked up to evaluate self.letterWord[i:i+1] before i is used as the list iterator, so that is the source of the error.

Try something like this:

for i, ltr in enumerate(self.letterWord):
    if ltr == letter:
        self.binaryWord[i] = 1

or even better (if possible, to create self.binaryWord in one line of code)

self.binaryWord = [ 1 if ltr == letter else 0 for ltr in self.letterWord ]

Upvotes: 2

Jon Clements
Jon Clements

Reputation: 142216

Working with your existing setup, purely to condense the code, you could do:

for idx, val in enumerate(self.letterWord[:3]):
    if val == letter:
        self.binaryWord[idx] = 1

Although it's possible that you could use other methods to build these objects such as list-comps/zipping them together/etc...

Upvotes: 1

Related Questions