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
Jan 19 at 15:01
@TalhaIsrar No. Clarified the text.
– Daniel Standage
Jan 19 at 15:04
are the elements in the list always unique?
– Cyzanfar
Jan 19 at 15:05
@Cyzanfar No. Clarified the text again. :-)
– Daniel Standage
Jan 19 at 15:07
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
Jan 19 at 15:14
|
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 Jan 19 at 15:07
Daniel Standage
asked Jan 19 at 15:00
Daniel StandageDaniel Standage
3,582135190
3,582135190
will the first list always be of length 3
– Talha Israr
Jan 19 at 15:01
@TalhaIsrar No. Clarified the text.
– Daniel Standage
Jan 19 at 15:04
are the elements in the list always unique?
– Cyzanfar
Jan 19 at 15:05
@Cyzanfar No. Clarified the text again. :-)
– Daniel Standage
Jan 19 at 15:07
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
Jan 19 at 15:14
|
show 4 more comments
will the first list always be of length 3
– Talha Israr
Jan 19 at 15:01
@TalhaIsrar No. Clarified the text.
– Daniel Standage
Jan 19 at 15:04
are the elements in the list always unique?
– Cyzanfar
Jan 19 at 15:05
@Cyzanfar No. Clarified the text again. :-)
– Daniel Standage
Jan 19 at 15:07
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
Jan 19 at 15:14
will the first list always be of length 3
– Talha Israr
Jan 19 at 15:01
will the first list always be of length 3
– Talha Israr
Jan 19 at 15:01
@TalhaIsrar No. Clarified the text.
– Daniel Standage
Jan 19 at 15:04
@TalhaIsrar No. Clarified the text.
– Daniel Standage
Jan 19 at 15:04
are the elements in the list always unique?
– Cyzanfar
Jan 19 at 15:05
are the elements in the list always unique?
– Cyzanfar
Jan 19 at 15:05
@Cyzanfar No. Clarified the text again. :-)
– Daniel Standage
Jan 19 at 15:07
@Cyzanfar No. Clarified the text again. :-)
– Daniel Standage
Jan 19 at 15:07
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
Jan 19 at 15:14
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
Jan 19 at 15:14
|
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
Jan 19 at 15:19
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
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
Jan 19 at 15:15
yup hope that helps!
– Cyzanfar
Jan 19 at 15:16
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 :)
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`
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
Jan 19 at 15:19
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
Jan 19 at 15:19
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 Jan 19 at 16:07
answered Jan 19 at 15:07
AustinAustin
10.3k3828
10.3k3828
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
Jan 19 at 15:19
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
Jan 19 at 15:19
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
Jan 19 at 15:19
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
Jan 19 at 15:19
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
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
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
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
answered Jan 19 at 15:22
DariushDariush
76110
76110
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 Jan 19 at 15:07
abcabc
2,3351130
2,3351130
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
Jan 19 at 15:15
yup hope that helps!
– Cyzanfar
Jan 19 at 15:16
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
Jan 19 at 15:15
yup hope that helps!
– Cyzanfar
Jan 19 at 15:16
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 Jan 19 at 15:11
CyzanfarCyzanfar
4,38932247
4,38932247
Nice. Even gives you the index of the first occurrence.
– Daniel Standage
Jan 19 at 15:15
yup hope that helps!
– Cyzanfar
Jan 19 at 15:16
add a comment |
Nice. Even gives you the index of the first occurrence.
– Daniel Standage
Jan 19 at 15:15
yup hope that helps!
– Cyzanfar
Jan 19 at 15:16
Nice. Even gives you the index of the first occurrence.
– Daniel Standage
Jan 19 at 15:15
Nice. Even gives you the index of the first occurrence.
– Daniel Standage
Jan 19 at 15:15
yup hope that helps!
– Cyzanfar
Jan 19 at 15:16
yup hope that helps!
– Cyzanfar
Jan 19 at 15:16
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 :)
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 :)
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 :)
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 :)
answered Jan 19 at 15:11
Talha IsrarTalha Israr
470212
470212
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 Jan 19 at 15:16
newkidnewkid
725417
725417
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`
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`
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`
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`
edited Jan 19 at 15:16
Cyzanfar
4,38932247
4,38932247
answered Jan 19 at 15:13
PrMiPrMi
112
112
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 Jan 19 at 15:29
Peter WoodPeter Wood
16.4k33270
16.4k33270
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
Jan 19 at 15:01
@TalhaIsrar No. Clarified the text.
– Daniel Standage
Jan 19 at 15:04
are the elements in the list always unique?
– Cyzanfar
Jan 19 at 15:05
@Cyzanfar No. Clarified the text again. :-)
– Daniel Standage
Jan 19 at 15:07
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
Jan 19 at 15:14