list comprehension that makes use of a dictionary and if else statements

Jomonsugi

I am a beginner to list comprehension and am having trouble figuring something out. According to examples I have looked at on stackoverflow and other sites, I have a list comprehension that seems like it should work, but I have not been able to accomplish the desired output, as I have been unable to figure out the correct syntax for what I want to accomplish.

Given a string, I would like my function to return the string with the alpha characters replaced with the value associated with a key in the provided dictionary. For that task my list comprehension works, but I also need any characters and spaces to stay intact (no change).

Here is what I have tried:

#random dictionary for my example 
d = {'a': 'b', 'c': 'i', 'b': 'a', 'e': 'j', 'd': 'm', 'g': 'q','f': 'l',  
     'i': 'c', 'h': 'w', 'k': 'r', 'j': 'e', 'm': 'd','l': 'f', 'o': 'v', 
     'n': 's', 'q': 'g', 'p': 't', 's': 'n','r': 'k', 'u': 'x', 't': 'p', 
     'w': 'h', 'v': 'o', 'y': 'z', 'x': 'u', 'z': 'y'}
def cipher(message):
    word = list(message)
    word = [v for x in word for k,v in d.iteritems() if x == k]
    #word = [v for x in word for k,v in d.iteritems() if x == k else x for x in word]
    return "".join(word)

print cipher("that tree is far away!")

This returns my string with the alpha characters correctly changed, but with no spaces and with no ! mark. From further research, that lead me to try the else statement I have in the list comprehension that is commented out in my code example, but that doesn't work.

Can I edit my syntax or can I not accomplish what I am trying to do using list comprehension?

To further clarify:

I am receiving this output: pwbppkjjcnlbkbhbz

I want this output: pwbp pkjj cn lbk bhbz!

Moses Koledoye

Your current approach filters out all characters that are not in the dictionary viz. whitespace and the exclamation.

You could use the .get method of the dictionary instead to fetch replacements and return the original character when a replacement character does not exist in your mapping:

def decipher(message):
    return "".join(d.get(x, x) for x in message)

print decipher("that tree is far away!")
#pwbp pkjj cn lbk bhbz!

Note that strings are iterable so word = list(message) is really not necessary and can be dropped.


On a another note, the name of the function probably reads better as cipher

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Python list comprehension for if-else statements

From Dev

How to put if/else statements in list comprehension along with multiple supporting expressions

From Dev

use python list comprehension to update dictionary value

From Dev

if-else in a dictionary comprehension

From Dev

Order of for statements in a list comprehension

From Dev

Dictionary comprehension with list comprehension as values

From Dev

Dictionary comprehension with list comprehension as values

From Java

if else in a list comprehension

From Java

if/else in a list comprehension

From Dev

R list comprehension if else

From Dev

if and else for list comprehension

From Dev

If else with list comprehension in haskell

From Dev

List comprehension with else pass

From Dev

If else with list comprehension in haskell

From Dev

dictionary and list comprehension in R

From Java

Create a dictionary with list comprehension

From Dev

Dictionary Comprehension for list values

From Dev

Dictionary from list comprehension

From Dev

populating a dictionary with list comprehension

From Dev

List comprehension example with different statements

From Dev

List comprehension inside dictionary comprehension - scope

From Dev

Numpy equivalent of if/else list comprehension

From Dev

Python list comprehension for if else statemets

From Dev

Numpy equivalent of if/else list comprehension

From Dev

Is OR and ELSE similar in list comprehension statement

From Dev

error with else statement in list comprehension

From Dev

list comprehension looping with if/else statement from 2d nested dictionary

From Dev

List Comprehension to Flatten a Dictionary of Dictionaries

From Dev

Is there a dictionary comprehension equivalent to conditional list appending for a dictionary?

Related Related

  1. 1

    Python list comprehension for if-else statements

  2. 2

    How to put if/else statements in list comprehension along with multiple supporting expressions

  3. 3

    use python list comprehension to update dictionary value

  4. 4

    if-else in a dictionary comprehension

  5. 5

    Order of for statements in a list comprehension

  6. 6

    Dictionary comprehension with list comprehension as values

  7. 7

    Dictionary comprehension with list comprehension as values

  8. 8

    if else in a list comprehension

  9. 9

    if/else in a list comprehension

  10. 10

    R list comprehension if else

  11. 11

    if and else for list comprehension

  12. 12

    If else with list comprehension in haskell

  13. 13

    List comprehension with else pass

  14. 14

    If else with list comprehension in haskell

  15. 15

    dictionary and list comprehension in R

  16. 16

    Create a dictionary with list comprehension

  17. 17

    Dictionary Comprehension for list values

  18. 18

    Dictionary from list comprehension

  19. 19

    populating a dictionary with list comprehension

  20. 20

    List comprehension example with different statements

  21. 21

    List comprehension inside dictionary comprehension - scope

  22. 22

    Numpy equivalent of if/else list comprehension

  23. 23

    Python list comprehension for if else statemets

  24. 24

    Numpy equivalent of if/else list comprehension

  25. 25

    Is OR and ELSE similar in list comprehension statement

  26. 26

    error with else statement in list comprehension

  27. 27

    list comprehension looping with if/else statement from 2d nested dictionary

  28. 28

    List Comprehension to Flatten a Dictionary of Dictionaries

  29. 29

    Is there a dictionary comprehension equivalent to conditional list appending for a dictionary?

HotTag

Archive