Test whether list A is contained in list B
I have two lists, A
& B
, and I would like to test whether A
is contained in B
. By "contained" I mean that the elements of A
appear in the exact same order within B
with no other elements between them. What I'm looking for is very similar to the behavior of A in B
if they were strings.
Some elements of A
will be repeated. We can assume A
will be shorter than B
.
There are many answers to similar questions on SO, but most answer a different question:
- Is
A
an element ofB
? (Not my question:B
is a flat list, not a list of lists.) - Are all the elements of
A
contained inB
? (Not my question: I'm concerned about order as well.) - Is
A
a sublist ofB
? (Not my question: I don't want to know whether the elements ofA
appear in the same order inB
, I want to know if they appear exactly as they are somewhere inB
.)
If the operation were implemented as the keyword containedin
, it would behave like this.
>>> [2, 3, 4] containedin [1, 2, 3, 4, 5]
True
>>> [2, 3, 4] containedin [1, 1, 2, 2, 3, 3, 4, 4, 5, 5]
False
>>> [2, 3, 4] containedin [5, 4, 3, 2, 1]
False
>>> [2, 2, 2] containedin [1, 2, 3, 4, 5]
False
>>> [2, 2, 2] containedin [1, 1, 2, 2, 3, 3, 4, 4, 5, 5]
False
>>> [2, 2, 2] containedin [1, 1, 1, 2, 2, 2, 3, 3, 3]
True
Is there a concise way to perform this operation in Python? Am I missing some important terminology that would have led me to the answer more quickly?
python list contains
|
show 4 more comments
I have two lists, A
& B
, and I would like to test whether A
is contained in B
. By "contained" I mean that the elements of A
appear in the exact same order within B
with no other elements between them. What I'm looking for is very similar to the behavior of A in B
if they were strings.
Some elements of A
will be repeated. We can assume A
will be shorter than B
.
There are many answers to similar questions on SO, but most answer a different question:
- Is
A
an element ofB
? (Not my question:B
is a flat list, not a list of lists.) - Are all the elements of
A
contained inB
? (Not my question: I'm concerned about order as well.) - Is
A
a sublist ofB
? (Not my question: I don't want to know whether the elements ofA
appear in the same order inB
, I want to know if they appear exactly as they are somewhere inB
.)
If the operation were implemented as the keyword containedin
, it would behave like this.
>>> [2, 3, 4] containedin [1, 2, 3, 4, 5]
True
>>> [2, 3, 4] containedin [1, 1, 2, 2, 3, 3, 4, 4, 5, 5]
False
>>> [2, 3, 4] containedin [5, 4, 3, 2, 1]
False
>>> [2, 2, 2] containedin [1, 2, 3, 4, 5]
False
>>> [2, 2, 2] containedin [1, 1, 2, 2, 3, 3, 4, 4, 5, 5]
False
>>> [2, 2, 2] containedin [1, 1, 1, 2, 2, 2, 3, 3, 3]
True
Is there a concise way to perform this operation in Python? Am I missing some important terminology that would have led me to the answer more quickly?
python list contains
will the first list always be of length 3
– Talha Israr
15 hours ago
@TalhaIsrar No. Clarified the text.
– Daniel Standage
15 hours ago
are the elements in the list always unique?
– Cyzanfar
15 hours ago
@Cyzanfar No. Clarified the text again. :-)
– Daniel Standage
15 hours ago
1
Um...someone want to explain their downvote? I put a lot of time into making it a clear question, and responding to requests for clarification.
– Daniel Standage
15 hours ago
|
show 4 more comments
I have two lists, A
& B
, and I would like to test whether A
is contained in B
. By "contained" I mean that the elements of A
appear in the exact same order within B
with no other elements between them. What I'm looking for is very similar to the behavior of A in B
if they were strings.
Some elements of A
will be repeated. We can assume A
will be shorter than B
.
There are many answers to similar questions on SO, but most answer a different question:
- Is
A
an element ofB
? (Not my question:B
is a flat list, not a list of lists.) - Are all the elements of
A
contained inB
? (Not my question: I'm concerned about order as well.) - Is
A
a sublist ofB
? (Not my question: I don't want to know whether the elements ofA
appear in the same order inB
, I want to know if they appear exactly as they are somewhere inB
.)
If the operation were implemented as the keyword containedin
, it would behave like this.
>>> [2, 3, 4] containedin [1, 2, 3, 4, 5]
True
>>> [2, 3, 4] containedin [1, 1, 2, 2, 3, 3, 4, 4, 5, 5]
False
>>> [2, 3, 4] containedin [5, 4, 3, 2, 1]
False
>>> [2, 2, 2] containedin [1, 2, 3, 4, 5]
False
>>> [2, 2, 2] containedin [1, 1, 2, 2, 3, 3, 4, 4, 5, 5]
False
>>> [2, 2, 2] containedin [1, 1, 1, 2, 2, 2, 3, 3, 3]
True
Is there a concise way to perform this operation in Python? Am I missing some important terminology that would have led me to the answer more quickly?
python list contains
I have two lists, A
& B
, and I would like to test whether A
is contained in B
. By "contained" I mean that the elements of A
appear in the exact same order within B
with no other elements between them. What I'm looking for is very similar to the behavior of A in B
if they were strings.
Some elements of A
will be repeated. We can assume A
will be shorter than B
.
There are many answers to similar questions on SO, but most answer a different question:
- Is
A
an element ofB
? (Not my question:B
is a flat list, not a list of lists.) - Are all the elements of
A
contained inB
? (Not my question: I'm concerned about order as well.) - Is
A
a sublist ofB
? (Not my question: I don't want to know whether the elements ofA
appear in the same order inB
, I want to know if they appear exactly as they are somewhere inB
.)
If the operation were implemented as the keyword containedin
, it would behave like this.
>>> [2, 3, 4] containedin [1, 2, 3, 4, 5]
True
>>> [2, 3, 4] containedin [1, 1, 2, 2, 3, 3, 4, 4, 5, 5]
False
>>> [2, 3, 4] containedin [5, 4, 3, 2, 1]
False
>>> [2, 2, 2] containedin [1, 2, 3, 4, 5]
False
>>> [2, 2, 2] containedin [1, 1, 2, 2, 3, 3, 4, 4, 5, 5]
False
>>> [2, 2, 2] containedin [1, 1, 1, 2, 2, 2, 3, 3, 3]
True
Is there a concise way to perform this operation in Python? Am I missing some important terminology that would have led me to the answer more quickly?
python list contains
python list contains
edited 15 hours ago
Daniel Standage
asked 15 hours ago
Daniel StandageDaniel Standage
3,557135190
3,557135190
will the first list always be of length 3
– Talha Israr
15 hours ago
@TalhaIsrar No. Clarified the text.
– Daniel Standage
15 hours ago
are the elements in the list always unique?
– Cyzanfar
15 hours ago
@Cyzanfar No. Clarified the text again. :-)
– Daniel Standage
15 hours ago
1
Um...someone want to explain their downvote? I put a lot of time into making it a clear question, and responding to requests for clarification.
– Daniel Standage
15 hours ago
|
show 4 more comments
will the first list always be of length 3
– Talha Israr
15 hours ago
@TalhaIsrar No. Clarified the text.
– Daniel Standage
15 hours ago
are the elements in the list always unique?
– Cyzanfar
15 hours ago
@Cyzanfar No. Clarified the text again. :-)
– Daniel Standage
15 hours ago
1
Um...someone want to explain their downvote? I put a lot of time into making it a clear question, and responding to requests for clarification.
– Daniel Standage
15 hours ago
will the first list always be of length 3
– Talha Israr
15 hours ago
will the first list always be of length 3
– Talha Israr
15 hours ago
@TalhaIsrar No. Clarified the text.
– Daniel Standage
15 hours ago
@TalhaIsrar No. Clarified the text.
– Daniel Standage
15 hours ago
are the elements in the list always unique?
– Cyzanfar
15 hours ago
are the elements in the list always unique?
– Cyzanfar
15 hours ago
@Cyzanfar No. Clarified the text again. :-)
– Daniel Standage
15 hours ago
@Cyzanfar No. Clarified the text again. :-)
– Daniel Standage
15 hours ago
1
1
Um...someone want to explain their downvote? I put a lot of time into making it a clear question, and responding to requests for clarification.
– Daniel Standage
15 hours ago
Um...someone want to explain their downvote? I put a lot of time into making it a clear question, and responding to requests for clarification.
– Daniel Standage
15 hours ago
|
show 4 more comments
8 Answers
8
active
oldest
votes
Use any
with list slicing:
def contained_in(lst, sub):
n = len(sub)
return any(sub == lst[i:i+n] for i in range(len(lst)-n+1))
Or, use join
to join both lists to strings and use in
operator:
def contained_in(lst, sub):
return ','.join(map(str, sub)) in ','.join(map(str, lst))
Usage:
>>> contained_in([1, 2, 3, 4, 5], [2, 3, 4])
True
>>> contained_in([1, 2, 2, 4, 5], [2, 3, 4])
False
I have a love/hate relationship with that second suggestion. It's the hackiest, but in my opinion the clearest in terms of intent & syntax, and very concise. +1
– Daniel Standage
14 hours ago
add a comment |
many people have posted their answers. but I want to post my efforts anyway ;)
this is my code:
def containedin(a,b):
for j in range(len(b)-len(a)+1):
if a==b[j:j+len(a)]:
return True
return False
print(containedin([2, 3, 4],[1, 2, 3, 4, 5]))
print(containedin([2, 3, 4],[1, 1, 2, 2, 3, 3, 4, 4, 5, 5]))
print(containedin([2, 3, 4],[5, 4, 3, 2, 1]))
print(containedin([2, 2, 2],[1, 2, 3, 4, 5]))
print(containedin([2, 2, 2],[1, 1, 1, 2, 2, 2, 3, 3, 3]))
this is the output:
True
False
False
False
True
New contributor
add a comment |
Assuming a
always shorter than b
what you can do is as follows.
any(a == b[i:i+len(a)] for i in range(len(b)-len(a)+1))
add a comment |
Considering you need to preserve order:
def contains(sub_array, array):
for i in range(len(array)-len(sub_array)+1):
for j in range(len(sub_array)):
if array[i+j] != sub_array[j]:
break
else:
return i, i+len(sub_array)
return False
Nice. Even gives you the index of the first occurrence.
– Daniel Standage
14 hours ago
yup hope that helps!
– Cyzanfar
14 hours ago
add a comment |
Use this function
I tried to not make it complex
def contains(list1,list2):
str1=""
for i in list1:
str1+=str(i)
str2=""
for j in list2:
str2+=str(j)
if str1 in str2:
return True
else:
return False
Hope it works :)
New contributor
add a comment |
Something like this?
class myList(list):
def in_other(self, other_list):
for i in range(0, len(other_list)-len(self)):
if other_list[i:i+len(self)] == self:
return True
else:
continue
if __name__ == "__main__":
x = myList([1, 2, 3])
b = [0, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6]
print(x.in_other(b))
add a comment |
You can create the concatenate the 2 lists into two different strings. Then, write a function to check if one string is in another.
def containedin(a, b):
if b in a:
return True
return False`
New contributor
add a comment |
No need to slice for every element:
def contains(seq, sub):
sub_length = len(sub)
sub_first = sub[0]
return any(sub == seq[index:index+sub_length]
for index, element in enumerate(seq)
if element == sub_first)
Usage:
>>> seq = [1, 2, 3, 4, 5]
>>> sub = [2, 3, 4]
>>> contains(seq, sub)
True
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%2f54268367%2ftest-whether-list-a-is-contained-in-list-b%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
8 Answers
8
active
oldest
votes
8 Answers
8
active
oldest
votes
active
oldest
votes
active
oldest
votes
Use any
with list slicing:
def contained_in(lst, sub):
n = len(sub)
return any(sub == lst[i:i+n] for i in range(len(lst)-n+1))
Or, use join
to join both lists to strings and use in
operator:
def contained_in(lst, sub):
return ','.join(map(str, sub)) in ','.join(map(str, lst))
Usage:
>>> contained_in([1, 2, 3, 4, 5], [2, 3, 4])
True
>>> contained_in([1, 2, 2, 4, 5], [2, 3, 4])
False
I have a love/hate relationship with that second suggestion. It's the hackiest, but in my opinion the clearest in terms of intent & syntax, and very concise. +1
– Daniel Standage
14 hours ago
add a comment |
Use any
with list slicing:
def contained_in(lst, sub):
n = len(sub)
return any(sub == lst[i:i+n] for i in range(len(lst)-n+1))
Or, use join
to join both lists to strings and use in
operator:
def contained_in(lst, sub):
return ','.join(map(str, sub)) in ','.join(map(str, lst))
Usage:
>>> contained_in([1, 2, 3, 4, 5], [2, 3, 4])
True
>>> contained_in([1, 2, 2, 4, 5], [2, 3, 4])
False
I have a love/hate relationship with that second suggestion. It's the hackiest, but in my opinion the clearest in terms of intent & syntax, and very concise. +1
– Daniel Standage
14 hours ago
add a comment |
Use any
with list slicing:
def contained_in(lst, sub):
n = len(sub)
return any(sub == lst[i:i+n] for i in range(len(lst)-n+1))
Or, use join
to join both lists to strings and use in
operator:
def contained_in(lst, sub):
return ','.join(map(str, sub)) in ','.join(map(str, lst))
Usage:
>>> contained_in([1, 2, 3, 4, 5], [2, 3, 4])
True
>>> contained_in([1, 2, 2, 4, 5], [2, 3, 4])
False
Use any
with list slicing:
def contained_in(lst, sub):
n = len(sub)
return any(sub == lst[i:i+n] for i in range(len(lst)-n+1))
Or, use join
to join both lists to strings and use in
operator:
def contained_in(lst, sub):
return ','.join(map(str, sub)) in ','.join(map(str, lst))
Usage:
>>> contained_in([1, 2, 3, 4, 5], [2, 3, 4])
True
>>> contained_in([1, 2, 2, 4, 5], [2, 3, 4])
False
edited 14 hours ago
answered 15 hours ago
AustinAustin
9,7523828
9,7523828
I have a love/hate relationship with that second suggestion. It's the hackiest, but in my opinion the clearest in terms of intent & syntax, and very concise. +1
– Daniel Standage
14 hours ago
add a comment |
I have a love/hate relationship with that second suggestion. It's the hackiest, but in my opinion the clearest in terms of intent & syntax, and very concise. +1
– Daniel Standage
14 hours ago
I have a love/hate relationship with that second suggestion. It's the hackiest, but in my opinion the clearest in terms of intent & syntax, and very concise. +1
– Daniel Standage
14 hours ago
I have a love/hate relationship with that second suggestion. It's the hackiest, but in my opinion the clearest in terms of intent & syntax, and very concise. +1
– Daniel Standage
14 hours ago
add a comment |
many people have posted their answers. but I want to post my efforts anyway ;)
this is my code:
def containedin(a,b):
for j in range(len(b)-len(a)+1):
if a==b[j:j+len(a)]:
return True
return False
print(containedin([2, 3, 4],[1, 2, 3, 4, 5]))
print(containedin([2, 3, 4],[1, 1, 2, 2, 3, 3, 4, 4, 5, 5]))
print(containedin([2, 3, 4],[5, 4, 3, 2, 1]))
print(containedin([2, 2, 2],[1, 2, 3, 4, 5]))
print(containedin([2, 2, 2],[1, 1, 1, 2, 2, 2, 3, 3, 3]))
this is the output:
True
False
False
False
True
New contributor
add a comment |
many people have posted their answers. but I want to post my efforts anyway ;)
this is my code:
def containedin(a,b):
for j in range(len(b)-len(a)+1):
if a==b[j:j+len(a)]:
return True
return False
print(containedin([2, 3, 4],[1, 2, 3, 4, 5]))
print(containedin([2, 3, 4],[1, 1, 2, 2, 3, 3, 4, 4, 5, 5]))
print(containedin([2, 3, 4],[5, 4, 3, 2, 1]))
print(containedin([2, 2, 2],[1, 2, 3, 4, 5]))
print(containedin([2, 2, 2],[1, 1, 1, 2, 2, 2, 3, 3, 3]))
this is the output:
True
False
False
False
True
New contributor
add a comment |
many people have posted their answers. but I want to post my efforts anyway ;)
this is my code:
def containedin(a,b):
for j in range(len(b)-len(a)+1):
if a==b[j:j+len(a)]:
return True
return False
print(containedin([2, 3, 4],[1, 2, 3, 4, 5]))
print(containedin([2, 3, 4],[1, 1, 2, 2, 3, 3, 4, 4, 5, 5]))
print(containedin([2, 3, 4],[5, 4, 3, 2, 1]))
print(containedin([2, 2, 2],[1, 2, 3, 4, 5]))
print(containedin([2, 2, 2],[1, 1, 1, 2, 2, 2, 3, 3, 3]))
this is the output:
True
False
False
False
True
New contributor
many people have posted their answers. but I want to post my efforts anyway ;)
this is my code:
def containedin(a,b):
for j in range(len(b)-len(a)+1):
if a==b[j:j+len(a)]:
return True
return False
print(containedin([2, 3, 4],[1, 2, 3, 4, 5]))
print(containedin([2, 3, 4],[1, 1, 2, 2, 3, 3, 4, 4, 5, 5]))
print(containedin([2, 3, 4],[5, 4, 3, 2, 1]))
print(containedin([2, 2, 2],[1, 2, 3, 4, 5]))
print(containedin([2, 2, 2],[1, 1, 1, 2, 2, 2, 3, 3, 3]))
this is the output:
True
False
False
False
True
New contributor
New contributor
answered 14 hours ago
DariushDariush
6310
6310
New contributor
New contributor
add a comment |
add a comment |
Assuming a
always shorter than b
what you can do is as follows.
any(a == b[i:i+len(a)] for i in range(len(b)-len(a)+1))
add a comment |
Assuming a
always shorter than b
what you can do is as follows.
any(a == b[i:i+len(a)] for i in range(len(b)-len(a)+1))
add a comment |
Assuming a
always shorter than b
what you can do is as follows.
any(a == b[i:i+len(a)] for i in range(len(b)-len(a)+1))
Assuming a
always shorter than b
what you can do is as follows.
any(a == b[i:i+len(a)] for i in range(len(b)-len(a)+1))
answered 15 hours ago
abcabc
2,2851130
2,2851130
add a comment |
add a comment |
Considering you need to preserve order:
def contains(sub_array, array):
for i in range(len(array)-len(sub_array)+1):
for j in range(len(sub_array)):
if array[i+j] != sub_array[j]:
break
else:
return i, i+len(sub_array)
return False
Nice. Even gives you the index of the first occurrence.
– Daniel Standage
14 hours ago
yup hope that helps!
– Cyzanfar
14 hours ago
add a comment |
Considering you need to preserve order:
def contains(sub_array, array):
for i in range(len(array)-len(sub_array)+1):
for j in range(len(sub_array)):
if array[i+j] != sub_array[j]:
break
else:
return i, i+len(sub_array)
return False
Nice. Even gives you the index of the first occurrence.
– Daniel Standage
14 hours ago
yup hope that helps!
– Cyzanfar
14 hours ago
add a comment |
Considering you need to preserve order:
def contains(sub_array, array):
for i in range(len(array)-len(sub_array)+1):
for j in range(len(sub_array)):
if array[i+j] != sub_array[j]:
break
else:
return i, i+len(sub_array)
return False
Considering you need to preserve order:
def contains(sub_array, array):
for i in range(len(array)-len(sub_array)+1):
for j in range(len(sub_array)):
if array[i+j] != sub_array[j]:
break
else:
return i, i+len(sub_array)
return False
answered 15 hours ago
CyzanfarCyzanfar
4,31132246
4,31132246
Nice. Even gives you the index of the first occurrence.
– Daniel Standage
14 hours ago
yup hope that helps!
– Cyzanfar
14 hours ago
add a comment |
Nice. Even gives you the index of the first occurrence.
– Daniel Standage
14 hours ago
yup hope that helps!
– Cyzanfar
14 hours ago
Nice. Even gives you the index of the first occurrence.
– Daniel Standage
14 hours ago
Nice. Even gives you the index of the first occurrence.
– Daniel Standage
14 hours ago
yup hope that helps!
– Cyzanfar
14 hours ago
yup hope that helps!
– Cyzanfar
14 hours ago
add a comment |
Use this function
I tried to not make it complex
def contains(list1,list2):
str1=""
for i in list1:
str1+=str(i)
str2=""
for j in list2:
str2+=str(j)
if str1 in str2:
return True
else:
return False
Hope it works :)
New contributor
add a comment |
Use this function
I tried to not make it complex
def contains(list1,list2):
str1=""
for i in list1:
str1+=str(i)
str2=""
for j in list2:
str2+=str(j)
if str1 in str2:
return True
else:
return False
Hope it works :)
New contributor
add a comment |
Use this function
I tried to not make it complex
def contains(list1,list2):
str1=""
for i in list1:
str1+=str(i)
str2=""
for j in list2:
str2+=str(j)
if str1 in str2:
return True
else:
return False
Hope it works :)
New contributor
Use this function
I tried to not make it complex
def contains(list1,list2):
str1=""
for i in list1:
str1+=str(i)
str2=""
for j in list2:
str2+=str(j)
if str1 in str2:
return True
else:
return False
Hope it works :)
New contributor
New contributor
answered 15 hours ago
Talha IsrarTalha Israr
1178
1178
New contributor
New contributor
add a comment |
add a comment |
Something like this?
class myList(list):
def in_other(self, other_list):
for i in range(0, len(other_list)-len(self)):
if other_list[i:i+len(self)] == self:
return True
else:
continue
if __name__ == "__main__":
x = myList([1, 2, 3])
b = [0, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6]
print(x.in_other(b))
add a comment |
Something like this?
class myList(list):
def in_other(self, other_list):
for i in range(0, len(other_list)-len(self)):
if other_list[i:i+len(self)] == self:
return True
else:
continue
if __name__ == "__main__":
x = myList([1, 2, 3])
b = [0, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6]
print(x.in_other(b))
add a comment |
Something like this?
class myList(list):
def in_other(self, other_list):
for i in range(0, len(other_list)-len(self)):
if other_list[i:i+len(self)] == self:
return True
else:
continue
if __name__ == "__main__":
x = myList([1, 2, 3])
b = [0, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6]
print(x.in_other(b))
Something like this?
class myList(list):
def in_other(self, other_list):
for i in range(0, len(other_list)-len(self)):
if other_list[i:i+len(self)] == self:
return True
else:
continue
if __name__ == "__main__":
x = myList([1, 2, 3])
b = [0, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6]
print(x.in_other(b))
answered 14 hours ago
newkidnewkid
414215
414215
add a comment |
add a comment |
You can create the concatenate the 2 lists into two different strings. Then, write a function to check if one string is in another.
def containedin(a, b):
if b in a:
return True
return False`
New contributor
add a comment |
You can create the concatenate the 2 lists into two different strings. Then, write a function to check if one string is in another.
def containedin(a, b):
if b in a:
return True
return False`
New contributor
add a comment |
You can create the concatenate the 2 lists into two different strings. Then, write a function to check if one string is in another.
def containedin(a, b):
if b in a:
return True
return False`
New contributor
You can create the concatenate the 2 lists into two different strings. Then, write a function to check if one string is in another.
def containedin(a, b):
if b in a:
return True
return False`
New contributor
edited 14 hours ago
Cyzanfar
4,31132246
4,31132246
New contributor
answered 15 hours ago
PrMiPrMi
112
112
New contributor
New contributor
add a comment |
add a comment |
No need to slice for every element:
def contains(seq, sub):
sub_length = len(sub)
sub_first = sub[0]
return any(sub == seq[index:index+sub_length]
for index, element in enumerate(seq)
if element == sub_first)
Usage:
>>> seq = [1, 2, 3, 4, 5]
>>> sub = [2, 3, 4]
>>> contains(seq, sub)
True
add a comment |
No need to slice for every element:
def contains(seq, sub):
sub_length = len(sub)
sub_first = sub[0]
return any(sub == seq[index:index+sub_length]
for index, element in enumerate(seq)
if element == sub_first)
Usage:
>>> seq = [1, 2, 3, 4, 5]
>>> sub = [2, 3, 4]
>>> contains(seq, sub)
True
add a comment |
No need to slice for every element:
def contains(seq, sub):
sub_length = len(sub)
sub_first = sub[0]
return any(sub == seq[index:index+sub_length]
for index, element in enumerate(seq)
if element == sub_first)
Usage:
>>> seq = [1, 2, 3, 4, 5]
>>> sub = [2, 3, 4]
>>> contains(seq, sub)
True
No need to slice for every element:
def contains(seq, sub):
sub_length = len(sub)
sub_first = sub[0]
return any(sub == seq[index:index+sub_length]
for index, element in enumerate(seq)
if element == sub_first)
Usage:
>>> seq = [1, 2, 3, 4, 5]
>>> sub = [2, 3, 4]
>>> contains(seq, sub)
True
answered 14 hours ago
Peter WoodPeter Wood
16.3k33269
16.3k33269
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.
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%2f54268367%2ftest-whether-list-a-is-contained-in-list-b%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
will the first list always be of length 3
– Talha Israr
15 hours ago
@TalhaIsrar No. Clarified the text.
– Daniel Standage
15 hours ago
are the elements in the list always unique?
– Cyzanfar
15 hours ago
@Cyzanfar No. Clarified the text again. :-)
– Daniel Standage
15 hours ago
1
Um...someone want to explain their downvote? I put a lot of time into making it a clear question, and responding to requests for clarification.
– Daniel Standage
15 hours ago