Python - Replacing repeating elements in a list with unique elements from another lists

Multi tool use
I'm trying to assign unique colour values to each repeating element in a list. For this I have two different lists, one being the list which contains the repeating values (eg. labels = [1, 5, 6, 7, 8, 12, 13, 17]
).
The second list (or list of list) contains the hue information itself:
group_pal = seaborn.husl_palette(len(set(labels)), s=.45)
.
The key here is that the length of group_pal
is always the number of unique elements in labels
.
Is there a python one liner to perform this operation ?
Input:
labels = [0, 0, 0, 1, 1, 2, 3]
group_pal = sns.husl_palette(len(set(labels)), s=.45)
Desired output:
labels = mapingMacro(labels, group_pal)
labels:
[0.8167028311697733, 0.5345122109266688, 0.5750280113923723]
[0.8167028311697733, 0.5345122109266688, 0.5750280113923723]
[0.8167028311697733, 0.5345122109266688, 0.5750280113923723]
[0.7256380093027939, 0.5865684184445076, 0.45124969098702544]
[0.7256380093027939, 0.5865684184445076, 0.45124969098702544]
[0.601243246823196, 0.6281411529879642, 0.44959498566071004]
[0.46712078684915886, 0.6454760674453914, 0.6277122757100324]
python list dictionary matplotlib seaborn
|
show 5 more comments
I'm trying to assign unique colour values to each repeating element in a list. For this I have two different lists, one being the list which contains the repeating values (eg. labels = [1, 5, 6, 7, 8, 12, 13, 17]
).
The second list (or list of list) contains the hue information itself:
group_pal = seaborn.husl_palette(len(set(labels)), s=.45)
.
The key here is that the length of group_pal
is always the number of unique elements in labels
.
Is there a python one liner to perform this operation ?
Input:
labels = [0, 0, 0, 1, 1, 2, 3]
group_pal = sns.husl_palette(len(set(labels)), s=.45)
Desired output:
labels = mapingMacro(labels, group_pal)
labels:
[0.8167028311697733, 0.5345122109266688, 0.5750280113923723]
[0.8167028311697733, 0.5345122109266688, 0.5750280113923723]
[0.8167028311697733, 0.5345122109266688, 0.5750280113923723]
[0.7256380093027939, 0.5865684184445076, 0.45124969098702544]
[0.7256380093027939, 0.5865684184445076, 0.45124969098702544]
[0.601243246823196, 0.6281411529879642, 0.44959498566071004]
[0.46712078684915886, 0.6454760674453914, 0.6277122757100324]
python list dictionary matplotlib seaborn
So you just want[group_pal[i] for i in labels]
?
– Jack Moody
Dec 28 '18 at 3:23
@JackMoody That work for now, but with a listlabels
like even:[3,3,3,2,2,1,0]
won't work as expected.
– U9-Forward
Dec 28 '18 at 3:36
@U9-Forward I’m a bit confused. Why wouldn’t it work for that list?
– Jack Moody
Dec 28 '18 at 3:40
@JackMoody Because your simply getting the values with the element as the index, so if the first one is the last one, it wouldn't work.
– U9-Forward
Dec 28 '18 at 3:41
@JackMoody Try it. and see
– U9-Forward
Dec 28 '18 at 3:42
|
show 5 more comments
I'm trying to assign unique colour values to each repeating element in a list. For this I have two different lists, one being the list which contains the repeating values (eg. labels = [1, 5, 6, 7, 8, 12, 13, 17]
).
The second list (or list of list) contains the hue information itself:
group_pal = seaborn.husl_palette(len(set(labels)), s=.45)
.
The key here is that the length of group_pal
is always the number of unique elements in labels
.
Is there a python one liner to perform this operation ?
Input:
labels = [0, 0, 0, 1, 1, 2, 3]
group_pal = sns.husl_palette(len(set(labels)), s=.45)
Desired output:
labels = mapingMacro(labels, group_pal)
labels:
[0.8167028311697733, 0.5345122109266688, 0.5750280113923723]
[0.8167028311697733, 0.5345122109266688, 0.5750280113923723]
[0.8167028311697733, 0.5345122109266688, 0.5750280113923723]
[0.7256380093027939, 0.5865684184445076, 0.45124969098702544]
[0.7256380093027939, 0.5865684184445076, 0.45124969098702544]
[0.601243246823196, 0.6281411529879642, 0.44959498566071004]
[0.46712078684915886, 0.6454760674453914, 0.6277122757100324]
python list dictionary matplotlib seaborn
I'm trying to assign unique colour values to each repeating element in a list. For this I have two different lists, one being the list which contains the repeating values (eg. labels = [1, 5, 6, 7, 8, 12, 13, 17]
).
The second list (or list of list) contains the hue information itself:
group_pal = seaborn.husl_palette(len(set(labels)), s=.45)
.
The key here is that the length of group_pal
is always the number of unique elements in labels
.
Is there a python one liner to perform this operation ?
Input:
labels = [0, 0, 0, 1, 1, 2, 3]
group_pal = sns.husl_palette(len(set(labels)), s=.45)
Desired output:
labels = mapingMacro(labels, group_pal)
labels:
[0.8167028311697733, 0.5345122109266688, 0.5750280113923723]
[0.8167028311697733, 0.5345122109266688, 0.5750280113923723]
[0.8167028311697733, 0.5345122109266688, 0.5750280113923723]
[0.7256380093027939, 0.5865684184445076, 0.45124969098702544]
[0.7256380093027939, 0.5865684184445076, 0.45124969098702544]
[0.601243246823196, 0.6281411529879642, 0.44959498566071004]
[0.46712078684915886, 0.6454760674453914, 0.6277122757100324]
python list dictionary matplotlib seaborn
python list dictionary matplotlib seaborn
edited Dec 28 '18 at 3:16


meW
1,701114
1,701114
asked Dec 28 '18 at 3:03
Siddharth
10411
10411
So you just want[group_pal[i] for i in labels]
?
– Jack Moody
Dec 28 '18 at 3:23
@JackMoody That work for now, but with a listlabels
like even:[3,3,3,2,2,1,0]
won't work as expected.
– U9-Forward
Dec 28 '18 at 3:36
@U9-Forward I’m a bit confused. Why wouldn’t it work for that list?
– Jack Moody
Dec 28 '18 at 3:40
@JackMoody Because your simply getting the values with the element as the index, so if the first one is the last one, it wouldn't work.
– U9-Forward
Dec 28 '18 at 3:41
@JackMoody Try it. and see
– U9-Forward
Dec 28 '18 at 3:42
|
show 5 more comments
So you just want[group_pal[i] for i in labels]
?
– Jack Moody
Dec 28 '18 at 3:23
@JackMoody That work for now, but with a listlabels
like even:[3,3,3,2,2,1,0]
won't work as expected.
– U9-Forward
Dec 28 '18 at 3:36
@U9-Forward I’m a bit confused. Why wouldn’t it work for that list?
– Jack Moody
Dec 28 '18 at 3:40
@JackMoody Because your simply getting the values with the element as the index, so if the first one is the last one, it wouldn't work.
– U9-Forward
Dec 28 '18 at 3:41
@JackMoody Try it. and see
– U9-Forward
Dec 28 '18 at 3:42
So you just want
[group_pal[i] for i in labels]
?– Jack Moody
Dec 28 '18 at 3:23
So you just want
[group_pal[i] for i in labels]
?– Jack Moody
Dec 28 '18 at 3:23
@JackMoody That work for now, but with a list
labels
like even: [3,3,3,2,2,1,0]
won't work as expected.– U9-Forward
Dec 28 '18 at 3:36
@JackMoody That work for now, but with a list
labels
like even: [3,3,3,2,2,1,0]
won't work as expected.– U9-Forward
Dec 28 '18 at 3:36
@U9-Forward I’m a bit confused. Why wouldn’t it work for that list?
– Jack Moody
Dec 28 '18 at 3:40
@U9-Forward I’m a bit confused. Why wouldn’t it work for that list?
– Jack Moody
Dec 28 '18 at 3:40
@JackMoody Because your simply getting the values with the element as the index, so if the first one is the last one, it wouldn't work.
– U9-Forward
Dec 28 '18 at 3:41
@JackMoody Because your simply getting the values with the element as the index, so if the first one is the last one, it wouldn't work.
– U9-Forward
Dec 28 '18 at 3:41
@JackMoody Try it. and see
– U9-Forward
Dec 28 '18 at 3:42
@JackMoody Try it. and see
– U9-Forward
Dec 28 '18 at 3:42
|
show 5 more comments
1 Answer
1
active
oldest
votes
Use:
group_pal = [i for x,y in zip(sns.husl_palette(len(set(labels)), s=.45),
sorted(set(labels),key=labels.index))
for i in np.repeat([x],labels.count(y),axis=0).tolist()]
And now:
print(group_pal)
Is:
[[0.8167028311697733, 0.5345122109266688, 0.5750280113923723],
[0.8167028311697733, 0.5345122109266688, 0.5750280113923723],
[0.8167028311697733, 0.5345122109266688, 0.5750280113923723],
[0.601243246823196, 0.6281411529879642, 0.44959498566071004],
[0.601243246823196, 0.6281411529879642, 0.44959498566071004],
[0.46712078684915886, 0.6454760674453914, 0.6277122757100324],
[0.6254162090818173, 0.5854245228463807, 0.7893617517727602]]
add a comment |
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
});
}
});
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%2fstackoverflow.com%2fquestions%2f53953219%2fpython-replacing-repeating-elements-in-a-list-with-unique-elements-from-anothe%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
Use:
group_pal = [i for x,y in zip(sns.husl_palette(len(set(labels)), s=.45),
sorted(set(labels),key=labels.index))
for i in np.repeat([x],labels.count(y),axis=0).tolist()]
And now:
print(group_pal)
Is:
[[0.8167028311697733, 0.5345122109266688, 0.5750280113923723],
[0.8167028311697733, 0.5345122109266688, 0.5750280113923723],
[0.8167028311697733, 0.5345122109266688, 0.5750280113923723],
[0.601243246823196, 0.6281411529879642, 0.44959498566071004],
[0.601243246823196, 0.6281411529879642, 0.44959498566071004],
[0.46712078684915886, 0.6454760674453914, 0.6277122757100324],
[0.6254162090818173, 0.5854245228463807, 0.7893617517727602]]
add a comment |
Use:
group_pal = [i for x,y in zip(sns.husl_palette(len(set(labels)), s=.45),
sorted(set(labels),key=labels.index))
for i in np.repeat([x],labels.count(y),axis=0).tolist()]
And now:
print(group_pal)
Is:
[[0.8167028311697733, 0.5345122109266688, 0.5750280113923723],
[0.8167028311697733, 0.5345122109266688, 0.5750280113923723],
[0.8167028311697733, 0.5345122109266688, 0.5750280113923723],
[0.601243246823196, 0.6281411529879642, 0.44959498566071004],
[0.601243246823196, 0.6281411529879642, 0.44959498566071004],
[0.46712078684915886, 0.6454760674453914, 0.6277122757100324],
[0.6254162090818173, 0.5854245228463807, 0.7893617517727602]]
add a comment |
Use:
group_pal = [i for x,y in zip(sns.husl_palette(len(set(labels)), s=.45),
sorted(set(labels),key=labels.index))
for i in np.repeat([x],labels.count(y),axis=0).tolist()]
And now:
print(group_pal)
Is:
[[0.8167028311697733, 0.5345122109266688, 0.5750280113923723],
[0.8167028311697733, 0.5345122109266688, 0.5750280113923723],
[0.8167028311697733, 0.5345122109266688, 0.5750280113923723],
[0.601243246823196, 0.6281411529879642, 0.44959498566071004],
[0.601243246823196, 0.6281411529879642, 0.44959498566071004],
[0.46712078684915886, 0.6454760674453914, 0.6277122757100324],
[0.6254162090818173, 0.5854245228463807, 0.7893617517727602]]
Use:
group_pal = [i for x,y in zip(sns.husl_palette(len(set(labels)), s=.45),
sorted(set(labels),key=labels.index))
for i in np.repeat([x],labels.count(y),axis=0).tolist()]
And now:
print(group_pal)
Is:
[[0.8167028311697733, 0.5345122109266688, 0.5750280113923723],
[0.8167028311697733, 0.5345122109266688, 0.5750280113923723],
[0.8167028311697733, 0.5345122109266688, 0.5750280113923723],
[0.601243246823196, 0.6281411529879642, 0.44959498566071004],
[0.601243246823196, 0.6281411529879642, 0.44959498566071004],
[0.46712078684915886, 0.6454760674453914, 0.6277122757100324],
[0.6254162090818173, 0.5854245228463807, 0.7893617517727602]]
edited Dec 28 '18 at 3:25


James
13.1k11532
13.1k11532
answered Dec 28 '18 at 3:23


U9-Forward
13.3k21237
13.3k21237
add a comment |
add a comment |
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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.
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%2fstackoverflow.com%2fquestions%2f53953219%2fpython-replacing-repeating-elements-in-a-list-with-unique-elements-from-anothe%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
8k7353DE70cvJaQFHV N207IlFTWbE7Ie j4kBIhaqiEvOqL6m,o43dj
So you just want
[group_pal[i] for i in labels]
?– Jack Moody
Dec 28 '18 at 3:23
@JackMoody That work for now, but with a list
labels
like even:[3,3,3,2,2,1,0]
won't work as expected.– U9-Forward
Dec 28 '18 at 3:36
@U9-Forward I’m a bit confused. Why wouldn’t it work for that list?
– Jack Moody
Dec 28 '18 at 3:40
@JackMoody Because your simply getting the values with the element as the index, so if the first one is the last one, it wouldn't work.
– U9-Forward
Dec 28 '18 at 3:41
@JackMoody Try it. and see
– U9-Forward
Dec 28 '18 at 3:42