Replacing values from one set with values from another, if the value of set1 = the key of set2?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















EDIT: Changed dictionaries to Sets, as I had not realised {} denoted a set. And fixed to say that sets are included in a tuple.



I want to iterate through each set in cardTuple, and for each value, replace it with the corresponding value(face) from imageDict. I assume we match value with with index, and somehow output the face. Maybe I need a third set or list to store results in before outputting?



imageDict = dict() # Contains index:face and looks like 1 👽 2 🐕 3 🐱 4 🚑 5 🚓 6 🐼 7 🐶 8 🐸 9 🐴 10 🐰 11 🐭 12 🐬 13 🐢 14 🐝

cardTuple = ({7, 42, 15, 47, 20, 52, 25, 30}, {3, 39, 14, 47, 55, 22, 23, 31})


My current approach:



newList = 
newList2 =
for i in cardTuple:
for j in i:
if i == 1: ## maybe this needs to be 0?
newList.append(imageDict[j])
elif i == 2: ## maybe 1?
newList2.append(imageDict[j])


Any advice?










share|improve this question




















  • 4





    Are you aware that dict1 is not a dictionary? What is the expected output?

    – Austin
    Nov 16 '18 at 14:08













  • My advice is to take a look at How to Ask and understand how to provide a Minimal, Complete, and Verifiable example of your last attempt.

    – Idlehands
    Nov 16 '18 at 14:11













  • Sorry. My mistake. I fixed it to reference sets instead. I'll also try to follow IdleHands and add more details in a little while, providing more snippets from my code. Thanks!

    – Karim
    Nov 16 '18 at 14:31













  • If you use sets, the order of result might change as sets are unordered. I believe that's fine.

    – Austin
    Nov 16 '18 at 14:33






  • 2





    @Karim: To be clear, the meaning of curly braces ({}) depends on their content. If they're empty, or they store key-value pairs separated by colons (:), they're a dict. If they're non-empty, but don't have a colon in them, they're a set.

    – ShadowRanger
    Nov 16 '18 at 14:48




















1















EDIT: Changed dictionaries to Sets, as I had not realised {} denoted a set. And fixed to say that sets are included in a tuple.



I want to iterate through each set in cardTuple, and for each value, replace it with the corresponding value(face) from imageDict. I assume we match value with with index, and somehow output the face. Maybe I need a third set or list to store results in before outputting?



imageDict = dict() # Contains index:face and looks like 1 👽 2 🐕 3 🐱 4 🚑 5 🚓 6 🐼 7 🐶 8 🐸 9 🐴 10 🐰 11 🐭 12 🐬 13 🐢 14 🐝

cardTuple = ({7, 42, 15, 47, 20, 52, 25, 30}, {3, 39, 14, 47, 55, 22, 23, 31})


My current approach:



newList = 
newList2 =
for i in cardTuple:
for j in i:
if i == 1: ## maybe this needs to be 0?
newList.append(imageDict[j])
elif i == 2: ## maybe 1?
newList2.append(imageDict[j])


Any advice?










share|improve this question




















  • 4





    Are you aware that dict1 is not a dictionary? What is the expected output?

    – Austin
    Nov 16 '18 at 14:08













  • My advice is to take a look at How to Ask and understand how to provide a Minimal, Complete, and Verifiable example of your last attempt.

    – Idlehands
    Nov 16 '18 at 14:11













  • Sorry. My mistake. I fixed it to reference sets instead. I'll also try to follow IdleHands and add more details in a little while, providing more snippets from my code. Thanks!

    – Karim
    Nov 16 '18 at 14:31













  • If you use sets, the order of result might change as sets are unordered. I believe that's fine.

    – Austin
    Nov 16 '18 at 14:33






  • 2





    @Karim: To be clear, the meaning of curly braces ({}) depends on their content. If they're empty, or they store key-value pairs separated by colons (:), they're a dict. If they're non-empty, but don't have a colon in them, they're a set.

    – ShadowRanger
    Nov 16 '18 at 14:48
















1












1








1








EDIT: Changed dictionaries to Sets, as I had not realised {} denoted a set. And fixed to say that sets are included in a tuple.



I want to iterate through each set in cardTuple, and for each value, replace it with the corresponding value(face) from imageDict. I assume we match value with with index, and somehow output the face. Maybe I need a third set or list to store results in before outputting?



imageDict = dict() # Contains index:face and looks like 1 👽 2 🐕 3 🐱 4 🚑 5 🚓 6 🐼 7 🐶 8 🐸 9 🐴 10 🐰 11 🐭 12 🐬 13 🐢 14 🐝

cardTuple = ({7, 42, 15, 47, 20, 52, 25, 30}, {3, 39, 14, 47, 55, 22, 23, 31})


My current approach:



newList = 
newList2 =
for i in cardTuple:
for j in i:
if i == 1: ## maybe this needs to be 0?
newList.append(imageDict[j])
elif i == 2: ## maybe 1?
newList2.append(imageDict[j])


Any advice?










share|improve this question
















EDIT: Changed dictionaries to Sets, as I had not realised {} denoted a set. And fixed to say that sets are included in a tuple.



I want to iterate through each set in cardTuple, and for each value, replace it with the corresponding value(face) from imageDict. I assume we match value with with index, and somehow output the face. Maybe I need a third set or list to store results in before outputting?



imageDict = dict() # Contains index:face and looks like 1 👽 2 🐕 3 🐱 4 🚑 5 🚓 6 🐼 7 🐶 8 🐸 9 🐴 10 🐰 11 🐭 12 🐬 13 🐢 14 🐝

cardTuple = ({7, 42, 15, 47, 20, 52, 25, 30}, {3, 39, 14, 47, 55, 22, 23, 31})


My current approach:



newList = 
newList2 =
for i in cardTuple:
for j in i:
if i == 1: ## maybe this needs to be 0?
newList.append(imageDict[j])
elif i == 2: ## maybe 1?
newList2.append(imageDict[j])


Any advice?







python python-3.x dictionary replace output






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 14:42







Karim

















asked Nov 16 '18 at 14:07









KarimKarim

316




316








  • 4





    Are you aware that dict1 is not a dictionary? What is the expected output?

    – Austin
    Nov 16 '18 at 14:08













  • My advice is to take a look at How to Ask and understand how to provide a Minimal, Complete, and Verifiable example of your last attempt.

    – Idlehands
    Nov 16 '18 at 14:11













  • Sorry. My mistake. I fixed it to reference sets instead. I'll also try to follow IdleHands and add more details in a little while, providing more snippets from my code. Thanks!

    – Karim
    Nov 16 '18 at 14:31













  • If you use sets, the order of result might change as sets are unordered. I believe that's fine.

    – Austin
    Nov 16 '18 at 14:33






  • 2





    @Karim: To be clear, the meaning of curly braces ({}) depends on their content. If they're empty, or they store key-value pairs separated by colons (:), they're a dict. If they're non-empty, but don't have a colon in them, they're a set.

    – ShadowRanger
    Nov 16 '18 at 14:48
















  • 4





    Are you aware that dict1 is not a dictionary? What is the expected output?

    – Austin
    Nov 16 '18 at 14:08













  • My advice is to take a look at How to Ask and understand how to provide a Minimal, Complete, and Verifiable example of your last attempt.

    – Idlehands
    Nov 16 '18 at 14:11













  • Sorry. My mistake. I fixed it to reference sets instead. I'll also try to follow IdleHands and add more details in a little while, providing more snippets from my code. Thanks!

    – Karim
    Nov 16 '18 at 14:31













  • If you use sets, the order of result might change as sets are unordered. I believe that's fine.

    – Austin
    Nov 16 '18 at 14:33






  • 2





    @Karim: To be clear, the meaning of curly braces ({}) depends on their content. If they're empty, or they store key-value pairs separated by colons (:), they're a dict. If they're non-empty, but don't have a colon in them, they're a set.

    – ShadowRanger
    Nov 16 '18 at 14:48










4




4





Are you aware that dict1 is not a dictionary? What is the expected output?

– Austin
Nov 16 '18 at 14:08







Are you aware that dict1 is not a dictionary? What is the expected output?

– Austin
Nov 16 '18 at 14:08















My advice is to take a look at How to Ask and understand how to provide a Minimal, Complete, and Verifiable example of your last attempt.

– Idlehands
Nov 16 '18 at 14:11







My advice is to take a look at How to Ask and understand how to provide a Minimal, Complete, and Verifiable example of your last attempt.

– Idlehands
Nov 16 '18 at 14:11















Sorry. My mistake. I fixed it to reference sets instead. I'll also try to follow IdleHands and add more details in a little while, providing more snippets from my code. Thanks!

– Karim
Nov 16 '18 at 14:31







Sorry. My mistake. I fixed it to reference sets instead. I'll also try to follow IdleHands and add more details in a little while, providing more snippets from my code. Thanks!

– Karim
Nov 16 '18 at 14:31















If you use sets, the order of result might change as sets are unordered. I believe that's fine.

– Austin
Nov 16 '18 at 14:33





If you use sets, the order of result might change as sets are unordered. I believe that's fine.

– Austin
Nov 16 '18 at 14:33




2




2





@Karim: To be clear, the meaning of curly braces ({}) depends on their content. If they're empty, or they store key-value pairs separated by colons (:), they're a dict. If they're non-empty, but don't have a colon in them, they're a set.

– ShadowRanger
Nov 16 '18 at 14:48







@Karim: To be clear, the meaning of curly braces ({}) depends on their content. If they're empty, or they store key-value pairs separated by colons (:), they're a dict. If they're non-empty, but don't have a colon in them, they're a set.

– ShadowRanger
Nov 16 '18 at 14:48














1 Answer
1






active

oldest

votes


















1














Firstly, as mentioned in the comments, dict1 is not a dict. It is a set. But you probably meant for it to be a list.
Here's a simple way to get what you want using list comprehensions:



mylist = [1,5,7,10,13]
mydict = {
1:face1,2:face2,3:face3,4:face4,5:face5,6:face6,7:face7,
8:face8,9:face9,10:face10,11:face11,12:face12,13:face13
}

output = [mydict[key] for key in mylist]

>>> [face1, face5, face7, face10, face13]





share|improve this answer
























  • So with output, you're creating a new list but I don't understand how the comparison works with "for key in mylist". Maybe you could explain what mydict[key] is doing? Is it comparing mydict key with key in mylist? If so, is it automatic that it outputs the value?

    – Karim
    Nov 16 '18 at 15:11






  • 1





    mydict[key] is extracting the value (face1,face2...) based on the key provided. In this case, the item in mylist. In this example it's essentially creating a list of mydict[1], mydict[5], mydict[7]... based on mylist = [1,5,7...]

    – Idlehands
    Nov 16 '18 at 15:33






  • 1





    dict objects are a collection of key: value pairs. By saying mydict[key] I am accessing the value associated with that key. By doing this in a for loop in a list comprehension, I am creating an output list type where each entry is the value associated with each key in mylist. You might want to look up list comprehensions - they are very useful Python constructions.

    – berkelem
    Nov 16 '18 at 15:54











  • Thank you! Much appreciated for the explanation. I'm going to try this solution than what I was working on now.

    – Karim
    Nov 16 '18 at 20:59











  • If this answered your question feel free to accept the answer :)

    – berkelem
    Nov 17 '18 at 8:37












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
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: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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/3.0/"u003ecc by-sa 3.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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53339438%2freplacing-values-from-one-set-with-values-from-another-if-the-value-of-set1-t%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Firstly, as mentioned in the comments, dict1 is not a dict. It is a set. But you probably meant for it to be a list.
Here's a simple way to get what you want using list comprehensions:



mylist = [1,5,7,10,13]
mydict = {
1:face1,2:face2,3:face3,4:face4,5:face5,6:face6,7:face7,
8:face8,9:face9,10:face10,11:face11,12:face12,13:face13
}

output = [mydict[key] for key in mylist]

>>> [face1, face5, face7, face10, face13]





share|improve this answer
























  • So with output, you're creating a new list but I don't understand how the comparison works with "for key in mylist". Maybe you could explain what mydict[key] is doing? Is it comparing mydict key with key in mylist? If so, is it automatic that it outputs the value?

    – Karim
    Nov 16 '18 at 15:11






  • 1





    mydict[key] is extracting the value (face1,face2...) based on the key provided. In this case, the item in mylist. In this example it's essentially creating a list of mydict[1], mydict[5], mydict[7]... based on mylist = [1,5,7...]

    – Idlehands
    Nov 16 '18 at 15:33






  • 1





    dict objects are a collection of key: value pairs. By saying mydict[key] I am accessing the value associated with that key. By doing this in a for loop in a list comprehension, I am creating an output list type where each entry is the value associated with each key in mylist. You might want to look up list comprehensions - they are very useful Python constructions.

    – berkelem
    Nov 16 '18 at 15:54











  • Thank you! Much appreciated for the explanation. I'm going to try this solution than what I was working on now.

    – Karim
    Nov 16 '18 at 20:59











  • If this answered your question feel free to accept the answer :)

    – berkelem
    Nov 17 '18 at 8:37
















1














Firstly, as mentioned in the comments, dict1 is not a dict. It is a set. But you probably meant for it to be a list.
Here's a simple way to get what you want using list comprehensions:



mylist = [1,5,7,10,13]
mydict = {
1:face1,2:face2,3:face3,4:face4,5:face5,6:face6,7:face7,
8:face8,9:face9,10:face10,11:face11,12:face12,13:face13
}

output = [mydict[key] for key in mylist]

>>> [face1, face5, face7, face10, face13]





share|improve this answer
























  • So with output, you're creating a new list but I don't understand how the comparison works with "for key in mylist". Maybe you could explain what mydict[key] is doing? Is it comparing mydict key with key in mylist? If so, is it automatic that it outputs the value?

    – Karim
    Nov 16 '18 at 15:11






  • 1





    mydict[key] is extracting the value (face1,face2...) based on the key provided. In this case, the item in mylist. In this example it's essentially creating a list of mydict[1], mydict[5], mydict[7]... based on mylist = [1,5,7...]

    – Idlehands
    Nov 16 '18 at 15:33






  • 1





    dict objects are a collection of key: value pairs. By saying mydict[key] I am accessing the value associated with that key. By doing this in a for loop in a list comprehension, I am creating an output list type where each entry is the value associated with each key in mylist. You might want to look up list comprehensions - they are very useful Python constructions.

    – berkelem
    Nov 16 '18 at 15:54











  • Thank you! Much appreciated for the explanation. I'm going to try this solution than what I was working on now.

    – Karim
    Nov 16 '18 at 20:59











  • If this answered your question feel free to accept the answer :)

    – berkelem
    Nov 17 '18 at 8:37














1












1








1







Firstly, as mentioned in the comments, dict1 is not a dict. It is a set. But you probably meant for it to be a list.
Here's a simple way to get what you want using list comprehensions:



mylist = [1,5,7,10,13]
mydict = {
1:face1,2:face2,3:face3,4:face4,5:face5,6:face6,7:face7,
8:face8,9:face9,10:face10,11:face11,12:face12,13:face13
}

output = [mydict[key] for key in mylist]

>>> [face1, face5, face7, face10, face13]





share|improve this answer













Firstly, as mentioned in the comments, dict1 is not a dict. It is a set. But you probably meant for it to be a list.
Here's a simple way to get what you want using list comprehensions:



mylist = [1,5,7,10,13]
mydict = {
1:face1,2:face2,3:face3,4:face4,5:face5,6:face6,7:face7,
8:face8,9:face9,10:face10,11:face11,12:face12,13:face13
}

output = [mydict[key] for key in mylist]

>>> [face1, face5, face7, face10, face13]






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 16 '18 at 14:30









berkelemberkelem

9632721




9632721













  • So with output, you're creating a new list but I don't understand how the comparison works with "for key in mylist". Maybe you could explain what mydict[key] is doing? Is it comparing mydict key with key in mylist? If so, is it automatic that it outputs the value?

    – Karim
    Nov 16 '18 at 15:11






  • 1





    mydict[key] is extracting the value (face1,face2...) based on the key provided. In this case, the item in mylist. In this example it's essentially creating a list of mydict[1], mydict[5], mydict[7]... based on mylist = [1,5,7...]

    – Idlehands
    Nov 16 '18 at 15:33






  • 1





    dict objects are a collection of key: value pairs. By saying mydict[key] I am accessing the value associated with that key. By doing this in a for loop in a list comprehension, I am creating an output list type where each entry is the value associated with each key in mylist. You might want to look up list comprehensions - they are very useful Python constructions.

    – berkelem
    Nov 16 '18 at 15:54











  • Thank you! Much appreciated for the explanation. I'm going to try this solution than what I was working on now.

    – Karim
    Nov 16 '18 at 20:59











  • If this answered your question feel free to accept the answer :)

    – berkelem
    Nov 17 '18 at 8:37



















  • So with output, you're creating a new list but I don't understand how the comparison works with "for key in mylist". Maybe you could explain what mydict[key] is doing? Is it comparing mydict key with key in mylist? If so, is it automatic that it outputs the value?

    – Karim
    Nov 16 '18 at 15:11






  • 1





    mydict[key] is extracting the value (face1,face2...) based on the key provided. In this case, the item in mylist. In this example it's essentially creating a list of mydict[1], mydict[5], mydict[7]... based on mylist = [1,5,7...]

    – Idlehands
    Nov 16 '18 at 15:33






  • 1





    dict objects are a collection of key: value pairs. By saying mydict[key] I am accessing the value associated with that key. By doing this in a for loop in a list comprehension, I am creating an output list type where each entry is the value associated with each key in mylist. You might want to look up list comprehensions - they are very useful Python constructions.

    – berkelem
    Nov 16 '18 at 15:54











  • Thank you! Much appreciated for the explanation. I'm going to try this solution than what I was working on now.

    – Karim
    Nov 16 '18 at 20:59











  • If this answered your question feel free to accept the answer :)

    – berkelem
    Nov 17 '18 at 8:37

















So with output, you're creating a new list but I don't understand how the comparison works with "for key in mylist". Maybe you could explain what mydict[key] is doing? Is it comparing mydict key with key in mylist? If so, is it automatic that it outputs the value?

– Karim
Nov 16 '18 at 15:11





So with output, you're creating a new list but I don't understand how the comparison works with "for key in mylist". Maybe you could explain what mydict[key] is doing? Is it comparing mydict key with key in mylist? If so, is it automatic that it outputs the value?

– Karim
Nov 16 '18 at 15:11




1




1





mydict[key] is extracting the value (face1,face2...) based on the key provided. In this case, the item in mylist. In this example it's essentially creating a list of mydict[1], mydict[5], mydict[7]... based on mylist = [1,5,7...]

– Idlehands
Nov 16 '18 at 15:33





mydict[key] is extracting the value (face1,face2...) based on the key provided. In this case, the item in mylist. In this example it's essentially creating a list of mydict[1], mydict[5], mydict[7]... based on mylist = [1,5,7...]

– Idlehands
Nov 16 '18 at 15:33




1




1





dict objects are a collection of key: value pairs. By saying mydict[key] I am accessing the value associated with that key. By doing this in a for loop in a list comprehension, I am creating an output list type where each entry is the value associated with each key in mylist. You might want to look up list comprehensions - they are very useful Python constructions.

– berkelem
Nov 16 '18 at 15:54





dict objects are a collection of key: value pairs. By saying mydict[key] I am accessing the value associated with that key. By doing this in a for loop in a list comprehension, I am creating an output list type where each entry is the value associated with each key in mylist. You might want to look up list comprehensions - they are very useful Python constructions.

– berkelem
Nov 16 '18 at 15:54













Thank you! Much appreciated for the explanation. I'm going to try this solution than what I was working on now.

– Karim
Nov 16 '18 at 20:59





Thank you! Much appreciated for the explanation. I'm going to try this solution than what I was working on now.

– Karim
Nov 16 '18 at 20:59













If this answered your question feel free to accept the answer :)

– berkelem
Nov 17 '18 at 8:37





If this answered your question feel free to accept the answer :)

– berkelem
Nov 17 '18 at 8:37




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • 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.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53339438%2freplacing-values-from-one-set-with-values-from-another-if-the-value-of-set1-t%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

Florida Star v. B. J. F.

Error while running script in elastic search , gateway timeout

Adding quotations to stringified JSON object values