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:
Get unique values in List of Lists
(6 answers)
Find unique rows in numpy.array
(20 answers)
Count Distinct Values in a List of Lists
(5 answers)
Closed 6 months ago.
I have a list A containing an array with indices. I want to print all the unique index numbers by scanning each [i,j]. In [0,3], i=0,j=3. I present the expected output.
import numpy as np
A=[np.array([[[0, 1],
[0, 3],
[1, 3],
[3, 4],
[3, 6],
[4, 5],
[4, 7],
[5, 7],
[6, 4]]])]
The expected output is
A=[0,1,3,4,5,6,7]
numpy has this very cool function np.unique.
Simply to get the output A=[0,1,3,4,5,6,7]
B = np.unique(A[0])
Output :
[0 1 3 4 5 6 7]
A=[[0, 1],[0, 3],[1, 3],[3, 4],[3, 6],[4, 5],[4, 7],[5, 7],[6, 4]]
K = []
for _ in range(len(A)):
K.extend(A[_])
print(set(K))
OUTPUT:
{0, 1, 3, 4, 5, 6, 7}
A is basically a list and within list you make an array.
Do this:
def unique(A):
x = np.array(A)
print(np.unique(x))
unique(A)
if A is an array we can simply do this:
np.unique(A)
This question already has answers here:
Append value to each sublist in a list
(3 answers)
Closed 2 years ago.
I'm trying to figure out how to increase the size of the lists inside a list of lists. For example [[1,2,3,4], [1,2,3,4],[1,2,3,4]] and want to be able to add 1 more onto it so it is [[1,2,3,4,5], [1,2,3,4,5],[1,2,3,4,5]]. The numbers are just to show i want them to now have a new element in each.
You can use a for-loop and .append().
>>> L = [[1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4]]
>>> for sublist in L:
... sublist.append(5)
...
>>> L
[[1, 2, 3, 4, 5], [1, 2, 3, 4, 5], [1, 2, 3, 4, 5]]
This question already has answers here:
How do I clone a list so that it doesn't change unexpectedly after assignment?
(24 answers)
Python - Appending list to list during while loop - Result not as expected [duplicate]
(2 answers)
Closed 4 years ago.
I am trying to write recursive code to get all permutations of an array.
While appending answer to a list it gives an unexpected answer.
Can someone help..
def permute(A):
p = []
def permute_util(a,l,r):
if l==r:
p.append(a)
print (a)
else:
for i in range(l,r+1):
a[l],a[i] = a[i],a[l]
permute_util(a,l+1,r)
a[l],a[i] = a[i],a[l]
permute_util(A,0,len(A)-1)
print (p)
OUTPUT -
[1, 2, 3]
[1, 3, 2]
[2, 1, 3]
[2, 3, 1]
[3, 2, 1]
[3, 1, 2]
[[1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]]
Last line of output is not what i expected.. what am i missing?
This question already has answers here:
Index all *except* one item in python
(11 answers)
Closed 5 years ago.
If I have the list [0, 1, 2, 3, 4, 5] I can return the last four items using list[1:].
Is there a similar way of doing this that would return the list without the second item?
I.e. list[??] == [0, 2, 3, 4 ,5]
(If there are different methods for Python 2.x and Python 3.x please detail both)
You can add two slices of the list
newLs = ls[:1] + ls[2:]
[0, 2, 3, 4, 5]
You can also delete the element
del ls[1]
you can try this:
n = 1
l = [0, 1, 2, 3, 4, 5]
new_l = [a for i, a in enumerate(l) if i != n]
print(new_l)
Output:
[0, 2, 3, 4, 5]
This question already has answers here:
How do I check if there are duplicates in a flat list?
(15 answers)
Closed 6 years ago.
So I have a list called puzzle which contains the follow lists:
puzzle = [[1, 3, 5, 5, 4],
[3, 5, 1, 3, 4],
[2, 3, 4, 5, 1],
[1, 5, 3, 2, 2],
[5, 4, 1, 3, 2]]
I would like to check each list inside puzzle and test if there are any duplicate numbers that are not zero, in which case the code would return false. How can I do this?
Almost the same approach -- except that you'd run it on a sub-list without zeros in it.
def has_dup(lst):
no_zeros = [x for x in lst if x != 0]
return len(set(no_zeros)) != len(no_zeros)
is_valid = any(has_dup(x) for x in puzzle)