Remove items in an array that are contained in other items [Python] - python

I'm not sure if the title is a great explanation of what I'm trying to do so here is an example:
Array = ['orangutan', 'sun', 'tan']
I want to print:
'orangutan', 'sun'
I've tried looping through the array and looking to see if one item contains another item as followed:
for i in Array:
remove_duplicates = [x for x in Array if x in i]
But this hasn't worked.
Any help would be greatly appreciated, thanks!

Here is a possible solution:
result = [x
for i, x in enumerate(Array)
if all(x not in y for j, y in enumerate(Array) if i != j)]

You can do:
lst = ['orangutan', 'sun', 'tan']
print([x for i, x in enumerate(lst)
if not any(x in y for j, y in enumerate(lst) if i != j)])
Update:
It won't escape notice that I have arrived at essentially the same solution as Riccardo Bucco. But the tests are different here. For the equivalence of these two tests, see De Morgan's laws. The any (i.e. a reduction of or over the boolean inputs) gives the inverse of all (i.e. reduction of and) on the inverse of those inputs -- hence the not any(... in ...) is equivalent to all(... not in ...).
In either case, iteration will stop at the first match: all will stop iterating and return False if it sees a false-like value, and any will stop iterating and return True if it sees a true-like value.

I assume that you have another array with the strings that you want to keep.
If this is the case, you can loop and use .pop():
array=['orangutan', 'sun', 'tan']
other_array=['orangutan', 'sun']
for i in range(0,len(array)):
if array[i] not in other_array:
array.pop(i)
print(array)
array=['orangutan', 'sun']

The remove() method removes the first matching element (which is passed as an argument) from the list.

I've tried looping through the array and looking to see if one item contains another item as followed.
you were almost there:
list_ = []
Array = ['orangutan', 'sun', 'tan']
for i in Array:
list_.extend([x for x in Array if x in i and x != i])
print(list_)
output:
['tan']

Related

How delete a element of a list and save the original index of deleted element?

I want delete some elements of one list equal to a value:
I can do it :
List =[1,2,3.....]
List = [x for x in List if x != 2]
How can i save the indexs of the deleted elements ?
I want to use this index to delete elements of another list.
Simplest solution is to make a list of indices to keep, then use that to strip the elements from both of your lists. itertools provides a handy compress utility to apply the indices to keep quickly:
from itertools import compress
tokeep = [x != 2 for x in List]
List = list(compress(List, tokeep))
otherlist = list(compress(otherlist, tokeep))
Alternatively (and frankly more clearly) you can just use one loop to strip both inputs; listcomps are fun, but sometimes they're not the way to go.
newlist = []
newotherlist = []
for x, y in zip(List, otherlist):
if x != 2:
newlist.append(x)
newotherlist.append(y)
which gets the same effect in a single pass. Even if it does feel less overtly clever, it's very clear, which is a good thing; brevity for the sake of brevity that creates complexity is not a win.
And now, to contradict that last paragraph, the amusingly overtly clever and brief solution to one-line this:
List, otherlist = map(list, zip(*[(x, y) for x, y in zip(List, otherlist) if x != 2]))
For the love of sanity, please don't actually use this, I just had to write it for funsies.
You can also leverage enumerate
for index, val in enumerate(List):
if val == value:
del List[index]
break
print(index)
Based on documentation
list_first = ['d', 'a']
list_second = ['x', 'z']
def remove_from_lists(element):
index_deleted = list_first.index(element)
list_first.remove(element)
list_second.pop(index_deleted)
remove_from_lists('d')

Append an element to the list if no other element after it is the same as it [duplicate]

This question already has answers here:
Fastest way to check if a value exists in a list
(11 answers)
Closed 1 year ago.
x = "haaiiillll"
y=list(x)
z=[]
a=len(y)
n=0
for i in y:
print(y[n:])
if i != y[n:]:
z.append(i)
n+=1
print(z)
How do I implement this, I can't understand. I have tried to implement a program where it checks if the value with i is there anywhere in the list y if not it will add it otherwise it shouldn't.
As far as I understood, you need to add the element nf the final list only if it appears once in the input list. So,
Change the value of n to 1. Keep a set to track whether the element is already seen. And change the condition to check whether the element is in the rest of the list and not seen already.
n=1
seen = set()
for i in y:
if i not in y[n:] and i not in seen:
z.append(i)
seen.add(i)
n+=1
You problem will be solved.
This is the simplest way of doing it and works well for lists which aren't very huge:
x = "haaiiillll"
res = [item for i, item in enumerate(x) if x.find(item, i + 1) == -1]
print(res)
output :
['h', 'a', 'i', 'l']
Using .find() we can see if there is any other values equal to item from it's position until the end. (second parameter in find is the starting point)
if not your_element in your_list:
your_list.append(your_element)
This way, an element will be added to the list only if it isn't in the list.
In the list z there will be only elements different from each other:
x = "haaiiillll"
y=list(x)
z=[]
a=len(y)
n=0
for i in y:
if i not in z:
z.append(i)
print(z)

How to compare the list elements in Python

Here i am having two lists :
list1 = ['2C535EB58F19B58' , '7B89D9071EB531B143594FF909BAC846' , '0509']
list2 = ['1641AB0C9C5B8867' , '0098968C' , '509']
I need to compare the elements inside list2 with list1 elements.
I want the output to be , after comparing :
509
since 509 is present in 0509.
How can i achieve so ? Does regex can help me in this ?
Try this:
Here we are checking whether element in list2 is a substring of an element in list1.
list1 = ['2C535EB58F19B58' , '7B89D9071EB531B143594FF909BAC846' , '0509']
list2 = ['1641AB0C9C5B8867' , '0098968C' , '509']
for i, j in zip(list1, list2):
if j in i:
print(j)
One-liner which will append to a list:
print( [j for i, j in zip(list1, list2) if j in i])
There could be more simple and better answers. If it helps, you can opt it.
You can do something like this:
common_elements = []
for x in list1:
for y in list2:
if y in x:
common_elements.append(y)
common_elements will contain the elements you need.
As already proposed by BearBrown in a comment, there is an easy way to achieve your goal. There is no reason to think about regular expressions at all (the in-operator is powerful enough).
[x for x in list2 if any(y for y in list1 if x in y)]
Here you are looking for each string x in list2 if it is a substring of any string y in list1 and finally save each matching substring x in a new list.
for x in list2:
for y in list1:
if x in y:
print x #or whatever
nested for-loops, I think this is a simple way, but I'm sure there is a better one
You can simply use in operation in python to check whether one string is in another string.
The easiest way to solve your problem would be
[y for y in list2 if any(y in x for x in list1)]

How to return the index of the list item

I have the following list in Python:
x = [[1,2],[3,4],[5,'inf'],[6,7],['nan',10]]
How can I return the index of the items that contain 'inf' or 'nan'?
EDIT-1
The first thing I thought about is to make an if-statement, as follows:
if (float('inf') in i) or (float('nan') in i)
But, didn't know how to go about returning the index value.
EDIT-2
The question that has been marked as duplicate didn't actually answer my question.
Thanks.
you can iterate over the list and check for nan or inf:
x = [[1,2],[3,4],[5,'inf'],[6,7],['nan',10]]
for y in x:
if 'inf' in y:
print(x.index(y),y.index('inf'))
elif 'nan' in y:
print(x.index(y), y.index('nan'))
the result will be:
2 1
4 0
I'm glad you added an attempt! We SO users love when folks show their work per say before asking a question. It shows that you are genuinely care about the problem you're trying to solve and not just here for a free-work service.
That being said, I like the simplicity of a list comprehension here:
>>> [(i, j) for i, seq in enumerate(x) for j, item in enumerate(seq) if item == 'inf' or item == 'nan']
[(2, 1), (4, 0)]
A more concise version of the above that takes advantage of sets and their performant membership checks:
>>> [(i, j) for i, seq in enumerate(x) for j, item in enumerate(seq) if item in {'inf', 'nan'}]
What we do here is (1) iterate through the nested list in a nested order, (2) check to see if our item matches the conditions you laid out, (3) capture the index of that position via the result of our enumerate() call.
Suggested Readings:
Python Strings
Enumerate function
[(i, j) for i, outer in enumerate(x) for j, inner in enumerate(outer ) if inner in ['inf','nan']]

Create list from 2D list based on conditions

I have a 2D Array in Python that is set up like
MY_ARRAY = [
['URL1', "ABC"],
['URL2'],
['URL3'],
['URL4', "ABC"]
]
I want to make an array of first element of each array only if the 2nd parameter is "ABC". So the result of the above example would be ['URL1', 'URL4']
I tried to do [x[0] for x in MY_ARRAY if x[1] == 'ABC'] but this returns IndexError: list index out of range. I think this is because sometimes x[1] is nonexistant.
I am looking for this to be a simple one-liner.
You could simply add a length check to the filtering criteria first. This works because Python short-circuits boolean expressions.
[ele[0] for ele in MY_ARRAY if len(ele) > 1 and ele[1] == 'ABC']
Also note that the proper terminology here is a list of lists, not an array.
I think you should try doing this:
if len(x) > 1:
if x[1] == 'ABC':
#do something here
This is a work around, but you can try it on one-liner code using:
if len(x) > 1 and x[1] == "ABC"
Cheers!
Try this
[x[0] for x in MY_ARRAY if len(x) > 1 and x[1] == 'ABC']
It is happening because you have two list are having only one item and you are trying to access second item from that list
First, let me tell you are on the right track, when x[1] doesn't exist you get the error
But, it's a bad habit to insist on doing things as a one-liner if it complicates matters.
Having said that, here's a one-liner that does that:
NEW_ARRAY = [x[0] for x in MY_ARRAY if len(x)>1 and x[1]=='ABC']
I found a simple solution that works for my usage.
[x[0] for x in MY_ARRAY if x[-1] == 'ABC']
x[-1] will always exist since thats the last element in the array/list.

Categories