This question already has answers here:
Removing duplicates in lists
(56 answers)
Closed 3 years ago.
I am trying to find duplicates inside an array.
my array looks like:
['LM_Auto', 'LM_Auto', 'LM_Op', 'LM_Op']
and much longer with a few thousand of these pairs.
def clear_stemmed_LM(collection):
i = 0
ele = i+1
for i in collection:
for ele in collection:
if i == ele:
del collection[ele]
return collection
the error says the list indices must be integers or slices not strings. How can I compare strings in an array?
[if x+1 for in x range] #Idea
or something similar like this?
If you just need to purge the duplicates in the list collection, as it seems from the code, list(set(collection)).
This is a very, very common and basic issue. I encourage you to Google thoroughly first!
for i in collection iters over the items so in i you'll find the first string not the index. Use for i in range(len(collection)) instead
Related
This question already has answers here:
How can I find same values in a list and group together a new list?
(6 answers)
Closed 2 years ago.
I have a list like so:
[10,10,10,20,20,20,20,30,40,40,40]
I want to split into X amount of lists, where X = how many unique elements there are, in the case above there are 4. So I would want 4 lists like so:
[[10,10,10],[20,20,20,20],[30],[40,40,40]]
Might be a dumb question and there is an easy way to do this but any help is appreciated, language is python3.
itertools.groupby does what you need, except it returns iterators instead of lists. Converting to lists is easy though:
[list(g) for _, g in itertools.groupby(my_list)]
This question already has answers here:
How to remove multiple indexes from a list at the same time? [duplicate]
(8 answers)
Closed 5 years ago.
Although this should be rather easily done with a for loop, I'm wondering wether there is a concise way to delete a set of positions from a Python list. E.g.:
l = ["A","B","C","D","E","F","G"]
pos = [4,6]
# Is there something close to
l.remove(pos)
Best
del is what you are looking for if you have consecutive indices:
From the documentation:
There is a way to remove an item from a list given its index instead of its value: the del statement
Otherwise, you could generate a new list, by filtering out indices, which you aren't interested in
result = [x for i, x in enumerate(l) if i not in pos]
This question already has answers here:
Python -Intersection of multiple lists?
(6 answers)
Closed 5 years ago.
say that I have a nested list like this for example:
List = [['a','d','b'],['a','x','w','t','d'],['g','c','d','z']]
and what I want to do is find the object in the List that all the smaller lists share, so for the example List I gave 'd' would be the object they all share.
here is what I have done so far:
def related(List):
for item in List[0]:
for i in range(1, len(List)):
if item in List[i]:
return item
the problem I am having is that when I do:
related([['a','d','b'],['a','x','w','t','d'],['g','c','d','z']])
'a' is returned, but that isn't the correct answer since 'a' isn't in all the lists and only in the first 2 lists. The correct answer with that list should be 'd'.
My function basically just stops running once it finds the same object in just 1 of the lists.
Will someone be able to send me towards the right path on what I can do to get my code working correctly? Thank You!!!
What you're looking for here is the intersection of these lists. Python lists don't have an intersection functionality built-in, but sets do. We can do
def in_common(l):
if not l:
return set()
return set(l[0]).intersection(*l[1:])
This converts the first element to a set, and then finds the intersection of that set with the rest of the elements in the list.
in_common([['a','d','b'],['a','x','w','t','d'],['g','c','d','z']])
returns
{'d'}
the set containing 'd'
This question already has answers here:
How to remove items from a list while iterating?
(25 answers)
Closed 6 years ago.
I'm trying to delete binary numbers from a list that don't contain the number of zeroes a user enters.
a = []
for i in range(512):
a.append(format(i, '09b'))
b = int(input("Enter zeroes: "))
for i in a:
if i.count('0') != b:
del(i)
for i in a:
print(i)
But running this code still yields the full list of 9 bit numbers. Where am I going wrong?
You need to use a.pop in order to delete something from the list.
However, you'd be much better off simply recreating the list, like this:
new_a = [i for i in a if i.count('0') == b]
It's more functional, and more clearly expresses what you're trying to do. It's also possibly more efficient. Deleting items randomly from a list is a linear operation. If you do it n times, you might end up with a quadratic algorithm. Rebuilding the list is only linear.
This question already has answers here:
Flatten an irregular (arbitrarily nested) list of lists
(51 answers)
Closed 6 months ago.
I asked a while ago about how to make a list of sublists (and of even more sublists) from a string given delimiters here.
How to process a string into layer of sublists
Now, I need to join them back together and I'm not sure how. I have tried to look here
Python : Recursively flatten a list
and
Flatten a list in python
However, neither of these answers work in my case because chain splits my string (single item "lists") into characters, and then therefore cannot join with "\n", and reduce does not concatenate str and list objects.
I will probably need to walk through the sublists (and their sublists) at some point. Is there a way to iterate through each level of sublist? (I can leave this as a separate question, one step at a time.. but just wondering whether this process is making sense or should I try an entire new method. I do think logically this makes the most sense, I'm just having trouble navigating it.)
Thanks.
I'm going to assume what I said in the comment is correct unless you say otherwise.
From the posts you linked, you have:
import collections
def flatten(l):
for el in l:
if isinstance(el, collections.Iterable) and not isinstance(el, basestring):
for sub in flatten(el):
yield sub
else:
yield el
to recursively flatten an irregular list of lists, and
def go(it):
for x in it:
if x == 'b':
yield [x] + list(go(it))
else:
yield x
if x == 'c':
break
to create that irregular list of lists from an iterator.
The inverse of go is just flatten(output_of_go) so the inverse of go(iter(string)) is ''.join(flatten(output_of_go)). You can see that with this test code:
lst = "aaabaabacabaacaca"
assert ''.join(flatten(go(iter(lst)))) == lst