Replace items in list with items in tuple of tuples
currently I am trying to unify license names in rpm packages.
Therefore I parse rpms, get the license information as lists and want to compare these list with a lookup table I build.
Here is an example:
lic = ['GPLv2', ' & ', 'LGPLv2+', ' & ', 'LGPLv2.1+', ' | ', 'LGPLv3+']
duplicates = (
('GPL-2.0', 'GPL-2', 'GPLv2', 'GPLv2.0'),
('GPL-2.0+', 'GPL-2+', 'GPLv2+', 'GPLv2.0+'),
('GPL-3.0', 'GPL-3', 'GPLv3', 'GPLv3.0'),
('GPL-3.0+', 'GPL-3+', 'GPLv3+', 'GPLv3.0+'),
('LGPL-2.0', 'LGPLv2.0', 'LGPLv2', 'LGPL2'),
('LGPL-2.0+', 'LGPLv2.0+', 'LGPLv2+', 'LGPL2+'),
('LGPL-2.1', 'LGPLv2.1', 'LGPL2.1'),
('LGPL-2.1+', 'LGPLv2.1+', 'LGPL2.1+'),
('LGPL-3.0', 'LGPLv3.0', 'LGPLv3', 'LGPL3'),
('LGPL-3.0+', 'LGPLv3.0+', 'LGPLv3+', 'LGPL3+')
)
What would be the most efficient and most pythonic way of replacing all occurrences of any element in duplicates
in lic
with the corresponding first element of each tuple?
eg: GPLv2 needs to be replaced with GPL-2.0, LGPLv2+ with LGPL-2.0+ and so on.
I am on Python 3.5.2
python list tuples
add a comment |
currently I am trying to unify license names in rpm packages.
Therefore I parse rpms, get the license information as lists and want to compare these list with a lookup table I build.
Here is an example:
lic = ['GPLv2', ' & ', 'LGPLv2+', ' & ', 'LGPLv2.1+', ' | ', 'LGPLv3+']
duplicates = (
('GPL-2.0', 'GPL-2', 'GPLv2', 'GPLv2.0'),
('GPL-2.0+', 'GPL-2+', 'GPLv2+', 'GPLv2.0+'),
('GPL-3.0', 'GPL-3', 'GPLv3', 'GPLv3.0'),
('GPL-3.0+', 'GPL-3+', 'GPLv3+', 'GPLv3.0+'),
('LGPL-2.0', 'LGPLv2.0', 'LGPLv2', 'LGPL2'),
('LGPL-2.0+', 'LGPLv2.0+', 'LGPLv2+', 'LGPL2+'),
('LGPL-2.1', 'LGPLv2.1', 'LGPL2.1'),
('LGPL-2.1+', 'LGPLv2.1+', 'LGPL2.1+'),
('LGPL-3.0', 'LGPLv3.0', 'LGPLv3', 'LGPL3'),
('LGPL-3.0+', 'LGPLv3.0+', 'LGPLv3+', 'LGPL3+')
)
What would be the most efficient and most pythonic way of replacing all occurrences of any element in duplicates
in lic
with the corresponding first element of each tuple?
eg: GPLv2 needs to be replaced with GPL-2.0, LGPLv2+ with LGPL-2.0+ and so on.
I am on Python 3.5.2
python list tuples
You probably need to use a dictionary.
– Rakesh
Nov 27 '18 at 13:38
add a comment |
currently I am trying to unify license names in rpm packages.
Therefore I parse rpms, get the license information as lists and want to compare these list with a lookup table I build.
Here is an example:
lic = ['GPLv2', ' & ', 'LGPLv2+', ' & ', 'LGPLv2.1+', ' | ', 'LGPLv3+']
duplicates = (
('GPL-2.0', 'GPL-2', 'GPLv2', 'GPLv2.0'),
('GPL-2.0+', 'GPL-2+', 'GPLv2+', 'GPLv2.0+'),
('GPL-3.0', 'GPL-3', 'GPLv3', 'GPLv3.0'),
('GPL-3.0+', 'GPL-3+', 'GPLv3+', 'GPLv3.0+'),
('LGPL-2.0', 'LGPLv2.0', 'LGPLv2', 'LGPL2'),
('LGPL-2.0+', 'LGPLv2.0+', 'LGPLv2+', 'LGPL2+'),
('LGPL-2.1', 'LGPLv2.1', 'LGPL2.1'),
('LGPL-2.1+', 'LGPLv2.1+', 'LGPL2.1+'),
('LGPL-3.0', 'LGPLv3.0', 'LGPLv3', 'LGPL3'),
('LGPL-3.0+', 'LGPLv3.0+', 'LGPLv3+', 'LGPL3+')
)
What would be the most efficient and most pythonic way of replacing all occurrences of any element in duplicates
in lic
with the corresponding first element of each tuple?
eg: GPLv2 needs to be replaced with GPL-2.0, LGPLv2+ with LGPL-2.0+ and so on.
I am on Python 3.5.2
python list tuples
currently I am trying to unify license names in rpm packages.
Therefore I parse rpms, get the license information as lists and want to compare these list with a lookup table I build.
Here is an example:
lic = ['GPLv2', ' & ', 'LGPLv2+', ' & ', 'LGPLv2.1+', ' | ', 'LGPLv3+']
duplicates = (
('GPL-2.0', 'GPL-2', 'GPLv2', 'GPLv2.0'),
('GPL-2.0+', 'GPL-2+', 'GPLv2+', 'GPLv2.0+'),
('GPL-3.0', 'GPL-3', 'GPLv3', 'GPLv3.0'),
('GPL-3.0+', 'GPL-3+', 'GPLv3+', 'GPLv3.0+'),
('LGPL-2.0', 'LGPLv2.0', 'LGPLv2', 'LGPL2'),
('LGPL-2.0+', 'LGPLv2.0+', 'LGPLv2+', 'LGPL2+'),
('LGPL-2.1', 'LGPLv2.1', 'LGPL2.1'),
('LGPL-2.1+', 'LGPLv2.1+', 'LGPL2.1+'),
('LGPL-3.0', 'LGPLv3.0', 'LGPLv3', 'LGPL3'),
('LGPL-3.0+', 'LGPLv3.0+', 'LGPLv3+', 'LGPL3+')
)
What would be the most efficient and most pythonic way of replacing all occurrences of any element in duplicates
in lic
with the corresponding first element of each tuple?
eg: GPLv2 needs to be replaced with GPL-2.0, LGPLv2+ with LGPL-2.0+ and so on.
I am on Python 3.5.2
python list tuples
python list tuples
asked Nov 27 '18 at 13:34
normannorman
31
31
You probably need to use a dictionary.
– Rakesh
Nov 27 '18 at 13:38
add a comment |
You probably need to use a dictionary.
– Rakesh
Nov 27 '18 at 13:38
You probably need to use a dictionary.
– Rakesh
Nov 27 '18 at 13:38
You probably need to use a dictionary.
– Rakesh
Nov 27 '18 at 13:38
add a comment |
2 Answers
2
active
oldest
votes
You could iterate over your lic
and compare each of its elements with duplicates
and if you find match replace that element with first element of matching tuple.
lic = ['GPLv2', ' & ', 'LGPLv2+', ' & ', 'LGPLv2.1+', ' | ', 'LGPLv3+']
duplicates = (
('GPL-2.0', 'GPL-2', 'GPLv2', 'GPLv2.0'),
('GPL-2.0+', 'GPL-2+', 'GPLv2+', 'GPLv2.0+'),
('GPL-3.0', 'GPL-3', 'GPLv3', 'GPLv3.0'),
('GPL-3.0+', 'GPL-3+', 'GPLv3+', 'GPLv3.0+'),
('LGPL-2.0', 'LGPLv2.0', 'LGPLv2', 'LGPL2'),
('LGPL-2.0+', 'LGPLv2.0+', 'LGPLv2+', 'LGPL2+'),
('LGPL-2.1', 'LGPLv2.1', 'LGPL2.1'),
('LGPL-2.1+', 'LGPLv2.1+', 'LGPL2.1+'),
('LGPL-3.0', 'LGPLv3.0', 'LGPLv3', 'LGPL3'),
('LGPL-3.0+', 'LGPLv3.0+', 'LGPLv3+', 'LGPL3+')
)
for idx, i in enumerate(lic):
for match in duplicates:
if i in match:
lic[idx] = match[0]
break
print(lic)
Output:
['GPL-2.0', ' & ', 'LGPL-2.0+', ' & ', 'LGPL-2.1+', ' | ', 'LGPL-3.0+']
add a comment |
I would modify the duplicates
tuple of tuples to a dict first. like so:
duplicates = {k: v for k, *v in duplicates} # {'GPL-2.0': ['GPL-2', 'GPLv2', 'GPLv2.0'], ...}
and then do:
lic_clean = [next((k for k, v in duplicates.items() if x in v), x) for x in lic]
which produces:
['GPL-2.0', ' & ', 'LGPL-2.0+', ' & ', 'LGPL-2.1+', ' | ', 'LGPL-3.0+']
Thank you. I want to keep the seperators, so I can reassemble the original license afterwards
– norman
Nov 27 '18 at 13:47
@norman See the edit then
– Ev. Kounis
Nov 27 '18 at 13:48
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%2f53500942%2freplace-items-in-list-with-items-in-tuple-of-tuples%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You could iterate over your lic
and compare each of its elements with duplicates
and if you find match replace that element with first element of matching tuple.
lic = ['GPLv2', ' & ', 'LGPLv2+', ' & ', 'LGPLv2.1+', ' | ', 'LGPLv3+']
duplicates = (
('GPL-2.0', 'GPL-2', 'GPLv2', 'GPLv2.0'),
('GPL-2.0+', 'GPL-2+', 'GPLv2+', 'GPLv2.0+'),
('GPL-3.0', 'GPL-3', 'GPLv3', 'GPLv3.0'),
('GPL-3.0+', 'GPL-3+', 'GPLv3+', 'GPLv3.0+'),
('LGPL-2.0', 'LGPLv2.0', 'LGPLv2', 'LGPL2'),
('LGPL-2.0+', 'LGPLv2.0+', 'LGPLv2+', 'LGPL2+'),
('LGPL-2.1', 'LGPLv2.1', 'LGPL2.1'),
('LGPL-2.1+', 'LGPLv2.1+', 'LGPL2.1+'),
('LGPL-3.0', 'LGPLv3.0', 'LGPLv3', 'LGPL3'),
('LGPL-3.0+', 'LGPLv3.0+', 'LGPLv3+', 'LGPL3+')
)
for idx, i in enumerate(lic):
for match in duplicates:
if i in match:
lic[idx] = match[0]
break
print(lic)
Output:
['GPL-2.0', ' & ', 'LGPL-2.0+', ' & ', 'LGPL-2.1+', ' | ', 'LGPL-3.0+']
add a comment |
You could iterate over your lic
and compare each of its elements with duplicates
and if you find match replace that element with first element of matching tuple.
lic = ['GPLv2', ' & ', 'LGPLv2+', ' & ', 'LGPLv2.1+', ' | ', 'LGPLv3+']
duplicates = (
('GPL-2.0', 'GPL-2', 'GPLv2', 'GPLv2.0'),
('GPL-2.0+', 'GPL-2+', 'GPLv2+', 'GPLv2.0+'),
('GPL-3.0', 'GPL-3', 'GPLv3', 'GPLv3.0'),
('GPL-3.0+', 'GPL-3+', 'GPLv3+', 'GPLv3.0+'),
('LGPL-2.0', 'LGPLv2.0', 'LGPLv2', 'LGPL2'),
('LGPL-2.0+', 'LGPLv2.0+', 'LGPLv2+', 'LGPL2+'),
('LGPL-2.1', 'LGPLv2.1', 'LGPL2.1'),
('LGPL-2.1+', 'LGPLv2.1+', 'LGPL2.1+'),
('LGPL-3.0', 'LGPLv3.0', 'LGPLv3', 'LGPL3'),
('LGPL-3.0+', 'LGPLv3.0+', 'LGPLv3+', 'LGPL3+')
)
for idx, i in enumerate(lic):
for match in duplicates:
if i in match:
lic[idx] = match[0]
break
print(lic)
Output:
['GPL-2.0', ' & ', 'LGPL-2.0+', ' & ', 'LGPL-2.1+', ' | ', 'LGPL-3.0+']
add a comment |
You could iterate over your lic
and compare each of its elements with duplicates
and if you find match replace that element with first element of matching tuple.
lic = ['GPLv2', ' & ', 'LGPLv2+', ' & ', 'LGPLv2.1+', ' | ', 'LGPLv3+']
duplicates = (
('GPL-2.0', 'GPL-2', 'GPLv2', 'GPLv2.0'),
('GPL-2.0+', 'GPL-2+', 'GPLv2+', 'GPLv2.0+'),
('GPL-3.0', 'GPL-3', 'GPLv3', 'GPLv3.0'),
('GPL-3.0+', 'GPL-3+', 'GPLv3+', 'GPLv3.0+'),
('LGPL-2.0', 'LGPLv2.0', 'LGPLv2', 'LGPL2'),
('LGPL-2.0+', 'LGPLv2.0+', 'LGPLv2+', 'LGPL2+'),
('LGPL-2.1', 'LGPLv2.1', 'LGPL2.1'),
('LGPL-2.1+', 'LGPLv2.1+', 'LGPL2.1+'),
('LGPL-3.0', 'LGPLv3.0', 'LGPLv3', 'LGPL3'),
('LGPL-3.0+', 'LGPLv3.0+', 'LGPLv3+', 'LGPL3+')
)
for idx, i in enumerate(lic):
for match in duplicates:
if i in match:
lic[idx] = match[0]
break
print(lic)
Output:
['GPL-2.0', ' & ', 'LGPL-2.0+', ' & ', 'LGPL-2.1+', ' | ', 'LGPL-3.0+']
You could iterate over your lic
and compare each of its elements with duplicates
and if you find match replace that element with first element of matching tuple.
lic = ['GPLv2', ' & ', 'LGPLv2+', ' & ', 'LGPLv2.1+', ' | ', 'LGPLv3+']
duplicates = (
('GPL-2.0', 'GPL-2', 'GPLv2', 'GPLv2.0'),
('GPL-2.0+', 'GPL-2+', 'GPLv2+', 'GPLv2.0+'),
('GPL-3.0', 'GPL-3', 'GPLv3', 'GPLv3.0'),
('GPL-3.0+', 'GPL-3+', 'GPLv3+', 'GPLv3.0+'),
('LGPL-2.0', 'LGPLv2.0', 'LGPLv2', 'LGPL2'),
('LGPL-2.0+', 'LGPLv2.0+', 'LGPLv2+', 'LGPL2+'),
('LGPL-2.1', 'LGPLv2.1', 'LGPL2.1'),
('LGPL-2.1+', 'LGPLv2.1+', 'LGPL2.1+'),
('LGPL-3.0', 'LGPLv3.0', 'LGPLv3', 'LGPL3'),
('LGPL-3.0+', 'LGPLv3.0+', 'LGPLv3+', 'LGPL3+')
)
for idx, i in enumerate(lic):
for match in duplicates:
if i in match:
lic[idx] = match[0]
break
print(lic)
Output:
['GPL-2.0', ' & ', 'LGPL-2.0+', ' & ', 'LGPL-2.1+', ' | ', 'LGPL-3.0+']
answered Nov 27 '18 at 13:47
Filip MłynarskiFilip Młynarski
1,7961413
1,7961413
add a comment |
add a comment |
I would modify the duplicates
tuple of tuples to a dict first. like so:
duplicates = {k: v for k, *v in duplicates} # {'GPL-2.0': ['GPL-2', 'GPLv2', 'GPLv2.0'], ...}
and then do:
lic_clean = [next((k for k, v in duplicates.items() if x in v), x) for x in lic]
which produces:
['GPL-2.0', ' & ', 'LGPL-2.0+', ' & ', 'LGPL-2.1+', ' | ', 'LGPL-3.0+']
Thank you. I want to keep the seperators, so I can reassemble the original license afterwards
– norman
Nov 27 '18 at 13:47
@norman See the edit then
– Ev. Kounis
Nov 27 '18 at 13:48
add a comment |
I would modify the duplicates
tuple of tuples to a dict first. like so:
duplicates = {k: v for k, *v in duplicates} # {'GPL-2.0': ['GPL-2', 'GPLv2', 'GPLv2.0'], ...}
and then do:
lic_clean = [next((k for k, v in duplicates.items() if x in v), x) for x in lic]
which produces:
['GPL-2.0', ' & ', 'LGPL-2.0+', ' & ', 'LGPL-2.1+', ' | ', 'LGPL-3.0+']
Thank you. I want to keep the seperators, so I can reassemble the original license afterwards
– norman
Nov 27 '18 at 13:47
@norman See the edit then
– Ev. Kounis
Nov 27 '18 at 13:48
add a comment |
I would modify the duplicates
tuple of tuples to a dict first. like so:
duplicates = {k: v for k, *v in duplicates} # {'GPL-2.0': ['GPL-2', 'GPLv2', 'GPLv2.0'], ...}
and then do:
lic_clean = [next((k for k, v in duplicates.items() if x in v), x) for x in lic]
which produces:
['GPL-2.0', ' & ', 'LGPL-2.0+', ' & ', 'LGPL-2.1+', ' | ', 'LGPL-3.0+']
I would modify the duplicates
tuple of tuples to a dict first. like so:
duplicates = {k: v for k, *v in duplicates} # {'GPL-2.0': ['GPL-2', 'GPLv2', 'GPLv2.0'], ...}
and then do:
lic_clean = [next((k for k, v in duplicates.items() if x in v), x) for x in lic]
which produces:
['GPL-2.0', ' & ', 'LGPL-2.0+', ' & ', 'LGPL-2.1+', ' | ', 'LGPL-3.0+']
edited Nov 27 '18 at 13:48
answered Nov 27 '18 at 13:42
Ev. KounisEv. Kounis
11k21548
11k21548
Thank you. I want to keep the seperators, so I can reassemble the original license afterwards
– norman
Nov 27 '18 at 13:47
@norman See the edit then
– Ev. Kounis
Nov 27 '18 at 13:48
add a comment |
Thank you. I want to keep the seperators, so I can reassemble the original license afterwards
– norman
Nov 27 '18 at 13:47
@norman See the edit then
– Ev. Kounis
Nov 27 '18 at 13:48
Thank you. I want to keep the seperators, so I can reassemble the original license afterwards
– norman
Nov 27 '18 at 13:47
Thank you. I want to keep the seperators, so I can reassemble the original license afterwards
– norman
Nov 27 '18 at 13:47
@norman See the edit then
– Ev. Kounis
Nov 27 '18 at 13:48
@norman See the edit then
– Ev. Kounis
Nov 27 '18 at 13:48
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.
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%2f53500942%2freplace-items-in-list-with-items-in-tuple-of-tuples%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
You probably need to use a dictionary.
– Rakesh
Nov 27 '18 at 13:38