OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Swapping elements in a list based on their match in a dictionary

  • Thread starter Thread starter lpanderson
  • Start date Start date
L

lpanderson

Guest
Alright so basically I'm trying to write a function that takes a string and a dictionary that relates every letter in the alphabet to another letter in the alphabet. The function then splits the string into individual letters, swaps them for their pair in the dictionary, then puts it all back together. It's important to note that in my dictionary the input alphabet is the values in the (key,value) pair and I'm trying to swap to the key alphabet.

Code:
def substitutor(text,theDict):
    for n in list(text):
        n = [key  for (key, value) in theDict.items() if value == n]

What I've tried here is to take every letter n in the list of letters "list(text)" and redefine it as the key in my (key,value) pair in the dictionary. I know I need to return something but I can't figure out what. The output ought to be either a list equivalent to list(text) but with the letters swapped or better yet a single string combining that list into one. I'm super new to python so I might be missing something obvious here, would love help.
<p>Alright so basically I'm trying to write a function that takes a string and a dictionary that relates every letter in the alphabet to another letter in the alphabet. The function then splits the string into individual letters, swaps them for their pair in the dictionary, then puts it all back together.
It's important to note that in my dictionary the input alphabet is the values in the (key,value) pair and I'm trying to swap to the key alphabet. </p>

<pre><code>def substitutor(text,theDict):
for n in list(text):
n = [key for (key, value) in theDict.items() if value == n]
</code></pre>

<p>What I've tried here is to take every letter n in the list of letters "list(text)" and redefine it as the key in my (key,value) pair in the dictionary.
I know I need to return something but I can't figure out what. The output ought to be either a list equivalent to list(text) but with the letters swapped or better yet a single string combining that list into one. I'm super new to python so I might be missing something obvious here, would love help. </p>
 

Latest posts

A
Replies
0
Views
1
Ashrik Ahamed
A
A
Replies
0
Views
1
Ashrik Ahamed
A

Online statistics

Members online
1
Guests online
4
Total visitors
5
Top