Mapping string into integersDistinguishing slots in a function mapping Position across a listMapping over an association producing an unexpected resultConverting a list of associations into a single associationReplace single element of a nested list with multiple elementsFlattening a list desiring a specific formatString Join but not fullyManipulation of two lists of strings and integersDeleting unwanted elements in lists of wordsCoverting list of string into integers and reshaping the original list
Find number 8 with the least number of tries
Meaning of 'pound' in "felt a fury that was not his own pound through his body"
A man condemned to serve his sentence in other times
Does immunity to fear prevent a mummy's Dreadful Glare from paralyzing a character?
how to say 'nerd' or 'geek' in french?
If I did not sign promotion bonus document, my career would be over. Is this duress?
Why does Principal Vagina say, "no relation" after introducing himself?
In the twin paradox does the returning twin also come back permanently length contracted flatter than the twin on earth?
Easy way of generating a 50-150W load @12V
Usefulness of Nash embedding theorem
How do I get my boyfriend to remove pictures of his ex girlfriend hanging in his apartment?
Is there a historical explanation as to why the USA people are so litigious compared to France?
Does a restocking fee still qualify as a business expense?
Did Terry Pratchett ever explain the inspiration behind Luggage?
How do I copy an installed steam game on my PC to an external hard drive?
Remove last letter 4 times, get a real word each time, starting word is a car model
How to snip same part of screen as last time?
Do businesses save their customers' credit card information until the payment is finalized?
Does my protagonist need to be the most important character?
Charges from Dollar General have never shown up on my debit card - how to resolve?
This is a Noteworthy Riddle
When to use the gestalt principle of common region?
What could possibly power an Alcubierre drive?
What does the British parliament hope to achieve by requesting a third Brexit extension?
Mapping string into integers
Distinguishing slots in a function mapping Position across a listMapping over an association producing an unexpected resultConverting a list of associations into a single associationReplace single element of a nested list with multiple elementsFlattening a list desiring a specific formatString Join but not fullyManipulation of two lists of strings and integersDeleting unwanted elements in lists of wordsCoverting list of string into integers and reshaping the original list
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty
margin-bottom:0;
.everyonelovesstackoverflowposition:absolute;height:1px;width:1px;opacity:0;top:0;left:0;pointer-events:none;
$begingroup$
Suppose I have the following list,
l = "b", "c", "d", "e", "b", "a", "b", "d", "e"
and further suppose I have the following association,
l1=<|1 -> "a", 2 -> "b", 3 -> "c", 4 -> "d", 5 -> "e"|>
I wonder how can I replace the keys into my list such that I get,
2, 3, 4, 5, 2, 1, 2, 4, 5
list-manipulation map
$endgroup$
add a comment
|
$begingroup$
Suppose I have the following list,
l = "b", "c", "d", "e", "b", "a", "b", "d", "e"
and further suppose I have the following association,
l1=<|1 -> "a", 2 -> "b", 3 -> "c", 4 -> "d", 5 -> "e"|>
I wonder how can I replace the keys into my list such that I get,
2, 3, 4, 5, 2, 1, 2, 4, 5
list-manipulation map
$endgroup$
add a comment
|
$begingroup$
Suppose I have the following list,
l = "b", "c", "d", "e", "b", "a", "b", "d", "e"
and further suppose I have the following association,
l1=<|1 -> "a", 2 -> "b", 3 -> "c", 4 -> "d", 5 -> "e"|>
I wonder how can I replace the keys into my list such that I get,
2, 3, 4, 5, 2, 1, 2, 4, 5
list-manipulation map
$endgroup$
Suppose I have the following list,
l = "b", "c", "d", "e", "b", "a", "b", "d", "e"
and further suppose I have the following association,
l1=<|1 -> "a", 2 -> "b", 3 -> "c", 4 -> "d", 5 -> "e"|>
I wonder how can I replace the keys into my list such that I get,
2, 3, 4, 5, 2, 1, 2, 4, 5
list-manipulation map
list-manipulation map
edited 6 hours ago
Henrik Schumacher
69.1k5 gold badges101 silver badges193 bronze badges
69.1k5 gold badges101 silver badges193 bronze badges
asked 8 hours ago
WilliamWilliam
1,1736 silver badges9 bronze badges
1,1736 silver badges9 bronze badges
add a comment
|
add a comment
|
4 Answers
4
active
oldest
votes
$begingroup$
The following gives you a "reversed" version of your association, with keys and values flipped:
l1 = <|1 -> "a", 2 -> "b", 3 -> "c", 4 -> "d", 5 -> "e"|>;
lookup = First /@ PositionIndex@l1
(* <|"a" -> 1, "b" -> 2, "c" -> 3, "d" -> 4, "e" -> 5|> *)
You can then use ReplaceAll (/.) to do the replacement:
l = "b", "c", "d", "e", "b", "a", "b", "d", "e";
l /. lookup
(* 2, 3, 4, 5, 2, 1, 2, 4, 5 *)
Alternatives
Other possible solutions for creating lookup:
lookup = <|Reverse /@ Normal@l1|>
lookup = Reverse /@ Normal@l1 (* doesn't need to be an association *)
$endgroup$
add a comment
|
$begingroup$
For the specific numbering in OP, you can also use LetterNumber:
LetterNumber[l]
2, 3, 4, 5, 2, 1, 2, 4, 5
$endgroup$
add a comment
|
$begingroup$
l /. Reverse /@ Normal[l1]
(* 2, 3, 4, 5, 2, 1, 2, 4, 5 *)
or
l /. AssociationMap[Reverse, l1]
(* 2, 3, 4, 5, 2, 1, 2, 4, 5 *)
$endgroup$
add a comment
|
$begingroup$
For this particular mapping, you could also use ToCharacterCode:
ToCharacterCode[StringJoin /@ l] - First@ToCharacterCode@"a" + 1
2, 3, 4, 5, 2, 1, 2, 4, 5
$endgroup$
add a comment
|
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "387"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f207515%2fmapping-string-into-integers%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
The following gives you a "reversed" version of your association, with keys and values flipped:
l1 = <|1 -> "a", 2 -> "b", 3 -> "c", 4 -> "d", 5 -> "e"|>;
lookup = First /@ PositionIndex@l1
(* <|"a" -> 1, "b" -> 2, "c" -> 3, "d" -> 4, "e" -> 5|> *)
You can then use ReplaceAll (/.) to do the replacement:
l = "b", "c", "d", "e", "b", "a", "b", "d", "e";
l /. lookup
(* 2, 3, 4, 5, 2, 1, 2, 4, 5 *)
Alternatives
Other possible solutions for creating lookup:
lookup = <|Reverse /@ Normal@l1|>
lookup = Reverse /@ Normal@l1 (* doesn't need to be an association *)
$endgroup$
add a comment
|
$begingroup$
The following gives you a "reversed" version of your association, with keys and values flipped:
l1 = <|1 -> "a", 2 -> "b", 3 -> "c", 4 -> "d", 5 -> "e"|>;
lookup = First /@ PositionIndex@l1
(* <|"a" -> 1, "b" -> 2, "c" -> 3, "d" -> 4, "e" -> 5|> *)
You can then use ReplaceAll (/.) to do the replacement:
l = "b", "c", "d", "e", "b", "a", "b", "d", "e";
l /. lookup
(* 2, 3, 4, 5, 2, 1, 2, 4, 5 *)
Alternatives
Other possible solutions for creating lookup:
lookup = <|Reverse /@ Normal@l1|>
lookup = Reverse /@ Normal@l1 (* doesn't need to be an association *)
$endgroup$
add a comment
|
$begingroup$
The following gives you a "reversed" version of your association, with keys and values flipped:
l1 = <|1 -> "a", 2 -> "b", 3 -> "c", 4 -> "d", 5 -> "e"|>;
lookup = First /@ PositionIndex@l1
(* <|"a" -> 1, "b" -> 2, "c" -> 3, "d" -> 4, "e" -> 5|> *)
You can then use ReplaceAll (/.) to do the replacement:
l = "b", "c", "d", "e", "b", "a", "b", "d", "e";
l /. lookup
(* 2, 3, 4, 5, 2, 1, 2, 4, 5 *)
Alternatives
Other possible solutions for creating lookup:
lookup = <|Reverse /@ Normal@l1|>
lookup = Reverse /@ Normal@l1 (* doesn't need to be an association *)
$endgroup$
The following gives you a "reversed" version of your association, with keys and values flipped:
l1 = <|1 -> "a", 2 -> "b", 3 -> "c", 4 -> "d", 5 -> "e"|>;
lookup = First /@ PositionIndex@l1
(* <|"a" -> 1, "b" -> 2, "c" -> 3, "d" -> 4, "e" -> 5|> *)
You can then use ReplaceAll (/.) to do the replacement:
l = "b", "c", "d", "e", "b", "a", "b", "d", "e";
l /. lookup
(* 2, 3, 4, 5, 2, 1, 2, 4, 5 *)
Alternatives
Other possible solutions for creating lookup:
lookup = <|Reverse /@ Normal@l1|>
lookup = Reverse /@ Normal@l1 (* doesn't need to be an association *)
edited 8 hours ago
answered 8 hours ago
Lukas LangLukas Lang
10.6k1 gold badge14 silver badges41 bronze badges
10.6k1 gold badge14 silver badges41 bronze badges
add a comment
|
add a comment
|
$begingroup$
For the specific numbering in OP, you can also use LetterNumber:
LetterNumber[l]
2, 3, 4, 5, 2, 1, 2, 4, 5
$endgroup$
add a comment
|
$begingroup$
For the specific numbering in OP, you can also use LetterNumber:
LetterNumber[l]
2, 3, 4, 5, 2, 1, 2, 4, 5
$endgroup$
add a comment
|
$begingroup$
For the specific numbering in OP, you can also use LetterNumber:
LetterNumber[l]
2, 3, 4, 5, 2, 1, 2, 4, 5
$endgroup$
For the specific numbering in OP, you can also use LetterNumber:
LetterNumber[l]
2, 3, 4, 5, 2, 1, 2, 4, 5
edited 4 hours ago
answered 6 hours ago
kglrkglr
220k10 gold badges250 silver badges506 bronze badges
220k10 gold badges250 silver badges506 bronze badges
add a comment
|
add a comment
|
$begingroup$
l /. Reverse /@ Normal[l1]
(* 2, 3, 4, 5, 2, 1, 2, 4, 5 *)
or
l /. AssociationMap[Reverse, l1]
(* 2, 3, 4, 5, 2, 1, 2, 4, 5 *)
$endgroup$
add a comment
|
$begingroup$
l /. Reverse /@ Normal[l1]
(* 2, 3, 4, 5, 2, 1, 2, 4, 5 *)
or
l /. AssociationMap[Reverse, l1]
(* 2, 3, 4, 5, 2, 1, 2, 4, 5 *)
$endgroup$
add a comment
|
$begingroup$
l /. Reverse /@ Normal[l1]
(* 2, 3, 4, 5, 2, 1, 2, 4, 5 *)
or
l /. AssociationMap[Reverse, l1]
(* 2, 3, 4, 5, 2, 1, 2, 4, 5 *)
$endgroup$
l /. Reverse /@ Normal[l1]
(* 2, 3, 4, 5, 2, 1, 2, 4, 5 *)
or
l /. AssociationMap[Reverse, l1]
(* 2, 3, 4, 5, 2, 1, 2, 4, 5 *)
edited 8 hours ago
answered 8 hours ago
Suba ThomasSuba Thomas
4,26411 silver badges20 bronze badges
4,26411 silver badges20 bronze badges
add a comment
|
add a comment
|
$begingroup$
For this particular mapping, you could also use ToCharacterCode:
ToCharacterCode[StringJoin /@ l] - First@ToCharacterCode@"a" + 1
2, 3, 4, 5, 2, 1, 2, 4, 5
$endgroup$
add a comment
|
$begingroup$
For this particular mapping, you could also use ToCharacterCode:
ToCharacterCode[StringJoin /@ l] - First@ToCharacterCode@"a" + 1
2, 3, 4, 5, 2, 1, 2, 4, 5
$endgroup$
add a comment
|
$begingroup$
For this particular mapping, you could also use ToCharacterCode:
ToCharacterCode[StringJoin /@ l] - First@ToCharacterCode@"a" + 1
2, 3, 4, 5, 2, 1, 2, 4, 5
$endgroup$
For this particular mapping, you could also use ToCharacterCode:
ToCharacterCode[StringJoin /@ l] - First@ToCharacterCode@"a" + 1
2, 3, 4, 5, 2, 1, 2, 4, 5
answered 6 hours ago
Carl WollCarl Woll
92k3 gold badges121 silver badges233 bronze badges
92k3 gold badges121 silver badges233 bronze badges
add a comment
|
add a comment
|
Thanks for contributing an answer to Mathematica Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f207515%2fmapping-string-into-integers%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown