2

I have some problems with python 3.x. In python 2.x. I could use replace attr in filter obj, but now I cannot use this. Here is a section of my code:

def uniq(seq):
    seen = {}
    return [seen.setdefault(x, x) for x in seq if x not in seen]

def partition(seq, n):
    return [seq[i : i + n] for i in xrange(0, len(seq), n)]

def PlayFairCipher(key, from_ = 'J', to = None):
    if to is None:
        to = 'I' if from_ == 'J' else ''

    def canonicalize(s):
        return list(filter(str.isupper, s.upper()).replace(from_, to))

    m = partition(uniq(canonicalize(key + ascii_uppercase)), 5)

    enc = {}

    for row in m:
        for i, j in product(xrange(5), repeat=2):
            if i != j:
                enc[row[i] + row[j]] = row[(i + 1) % 5] + row[(j + 1) % 5]

    for c in zip(*m):
        for i, j in product(xrange(5), repeat=2):
            if i != j:
                enc[c[i] + c[j]] = c[(i + 1) % 5] + c[(j + 1) % 5]

    for i1, j1, i2, j2 in product(xrange(5), repeat=4):
        if i1 != i2 and j1 != j2:
            enc[m[i1][j1] + m[i2][j2]] = m[i1][j2] + m[i2][j1]

    def sub_enc(txt):
        lst = findall(r"(.)(?:(?!\1)(.))?", canonicalize(txt))
        return ''.join(enc[a + (b if b else 'X')] for a, b in lst)

    return sub_enc

But when this compiled, I receive this:

AttributeError: 'filter' object has no attribute 'replace'

How can I fix it?

4
  • plus your code is full of xrange... Commented Oct 14, 2016 at 15:33
  • I don't run this, I run "filter(str.isupper, s.upper()).replace(from_, to)" in python 2 and this works good. but how about in python 3? @vaultah Commented Oct 14, 2016 at 15:33
  • I solved xrange problem. this is a section of my code, as I said. @Jean-FrançoisFabre Commented Oct 14, 2016 at 15:35
  • replace is the string method. Function filter return a generator and not a string Commented Oct 14, 2016 at 15:40

2 Answers 2

3

in python 2, filter returns a string if a string is passed as input.

filter(...) filter(function or None, sequence) -> list, tuple, or string

Return those items of sequence for which function(item) is true. If function is None, return the items that are true. If sequence is a >tuple or string, return the same type, else return a list.

To simulate this behaviour in python 3 just do

"".join(filter(str.isupper, s.upper()))

to convert the iterable to string, then you can perform the replace

Sign up to request clarification or add additional context in comments.

Comments

1

I think you can use a list comprehension:

[c.replace(from_, to) for c in s.upper() if c.isupper()]

Is this what you want? There's a lot of code there so I might be missing something

Comments

Your Answer

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.