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}
Related
This question already has answers here:
How do I make a flat list out of a list of lists?
(34 answers)
Closed 6 months ago.
I have to sort the list based on frequency of occurrence and I am trying this way but getting stuck.
Code snippet of the program
import itertools
some_list = [[4], [42], [1, 1, 1, 1], [2, 2, 2, 2, 2, 2]]
flat = list(itertools.chain(*some_list))
print(flat)
Orrrr some black magic:
some_list = [[4], [42], [1, 1, 1, 1], [2, 2, 2, 2, 2, 2]]
flat = sum(some_list, [])
print(flat)
This question already has answers here:
Get unique values in List of Lists
(6 answers)
Closed 2 years ago.
How can I use list comprehension for the list unique?
original_list = [[1, 2, 3, 5], [2, 3, 5, 4], [0, 5, 4, 1], [3, 7, 2, 1], [1, 2, 1, 2]]
new_list = [elements for items in original_list for elements in items]
unique = []
for item in new_list:
if item not in unique:
unique.append(item)
print(unique)
Use set comprehension:
uniques = {elements for items in original_list for elements in items}
will give you the set
{0, 1, 2, 3, 4, 5, 7}
You can convert it to a list if you want
uniques = list(uniques)
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()])
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]
If there are more than 1 list with different length, what's the fast way to find out same item in each list?
a = [[1, 2, 3], [2, 3], [3, 4, 5, 6, 7]]
s = set.intersection(*map(set, a))
s is a set with a single element 3.