This question already has answers here:
Removing duplicates in lists
(56 answers)
One-liner to remove duplicates, keep ordering of list [duplicate]
(6 answers)
Closed 3 years ago.
I have a big list of strings appearing many times and I want a list of the same strings to appear only once.
An example with numbers would be:
a = [1, 2, 2, 3, 4, 4]
and I want to get
b = [1, 2, 3, 4]
What I tried is something like:
a = [1, 2, 2, 3, 4, 4]
[x for x in a if a.count(x) == 1]
[1, 3]
but this omits the duplicate numbers and takes only those appearing once.
You can try this:
import collections
a = [1, 2, 2, 2, 3, 3, 4, 4, 5, 6, 7, 7, 8]
print([item for item, count in collections.Counter(a).items()])
Related
This question already has answers here:
Strange result when removing item from a list while iterating over it
(8 answers)
Closed 5 months ago.
So i want to delete all 3s from numbers
numbers = [3, 3, 3, 3, 3, 1, 2, 2, 3, 3, 3, 3, 3, 3]
for i in numbers:
if i == 3:
numbers.remove(3)
print(numbers)
It feels like this should do it but it prints
[1, 2, 2, 3, 3, 3, 3, 3]
as the result.. It seems like consecutive 3s are the problem but I don't know why..
result = [i for i in numbers if i != 3]
This question already has answers here:
Python -Intersection of multiple lists?
(6 answers)
Closed 4 years ago.
def query_RR(postings, qtext):
words = tokenize(qtext)
allpostings = [postings[w] for w in words]
for a in allpostings:
print a.keys()
And this was the result of the query [0, 2, 3, 4, 6] [1, 4, 5] [0, 2, 4] [4, 5]
The query is taking a user inputted term ('qtext'), tokenizing and generating a postings list for each token.
The posting list is a list of nested dictionaries (e.g. [{0 : 0.68426, 1: 0.26423}, {2: 0.6842332, 0: 0.9823}]. I am attempting to find the intersection for these nested dictionaries using the keys
Assuming the order does not matter, you could use set.intersection():
>>> lst = [[0, 2, 3, 4, 6], [1, 4, 5], [0, 2, 4], [4, 5]]
>>> set.intersection(*map(set,lst))
{4}
>>> set(lst[0]).intersection(*lst[1:])
{4}
This question already has answers here:
What do ellipsis [...] mean in a list?
(5 answers)
Closed 5 years ago.
x = [4, 5, 6]
li = [1, 2, 3, 7]
li.insert(3,x)
x+=li
print(x)
The output is:
[4, 5, 6, 1, 2, 3, [...], 7]
I'm new to python/coding and I don't know what these ellipses are but when I do other code it starts getting weird. Wasn't sure what to ask since I have no clue what's going on. Thank you!
you're inserting a list inside your list, probably not what you want.
Then when doing this
x+=li
the representation of the list then shows an ellipsis because you're referencing the list from itself (x is referenced in li already)
To insert several items at once in a list in-place you could use slice assignment:
>>> x = [4, 5, 6]
>>> li = [1, 2, 3, 7]
>>> li[3:3] = x
>>> li
[1, 2, 3, 4, 5, 6, 7]
This question already has answers here:
Element-wise addition of 2 lists?
(17 answers)
Closed 7 years ago.
I'm sure there is a good way to accomplish what i want without looping over lists and creating new objects. Here is what I have
a = [1, 2, 3, 4]
b = [2, 3, 4, 5]
What I am looking to do is take each set of lists and sum each placeholder so that the output is
[3, 5, 7, 9]
Thoughts?
you should use zip function and list comprehension
a = [1, 2, 3, 4]
b = [2, 3, 4, 5]
[sum(t) for t in zip(a,b)]
Use numpy
import numpy as np
a = np.array([1, 2, 3, 4])
b = np.array([2, 3, 4, 5])
a+b
>>> array([3, 5, 7, 9])
This question already has answers here:
Python list rotation [duplicate]
(4 answers)
Closed 8 years ago.
Suppose I have a list u = [1, 2, 3, 4, 5], and u[1:] returns [2, 3, 4, 5].
I wonder what indexing returns [2, 3, 4, 5, 1], going from the second position to the last and then the first?
You can make a general function that does this at any point in your list, just by adding two slices. This was an intentional design as to why slicing is half-open (includes left index, but excludes right index)
def rotate(l, i):
return l[i:] + l[:i]
>>> u = [1, 2, 3, 4, 5]
>>> rotate(u, 1)
[2, 3, 4, 5, 1]
>>> rotate(u, 2)
[3, 4, 5, 1, 2]