Check If an Item Is in a Nested List

Check if an item is in a nested list

Try this, using the built-in any function. It's the most idiomatic solution, and it's also efficient, because any short-circuits and stops as soon as it finds the first match:

x = [[1, 2, 3], [2, 3, 4]]
any(2 in sl for sl in x)
=> True

Python - Check if element is in 1 or more nested lists and return whole list if TRUE

Here is a rough way to do it:

lists = [
["banana", 10, "yellow"],
["apple", 12, "red"],
["pear", 60, "green"],
["mango", 5, "yellow"],
]

keyword = 'banana'
for lst in lists:
if keyword in lst:
print(lst)

keyword = 'yellow'
for lst in lists:
if keyword in lst:
print(lst)

Ideally you would extract the search to a function accepting the lists and the keyword:

def get_sublists_containing_keyword(lists, keyword):
sublists = []
for lst in lists:
if keyword in lst:
sublists.append(lst)
return sublists

lists = [
["banana", 10, "yellow"],
["apple", 12, "red"],
["pear", 60, "green"],
["mango", 5, "yellow"],
]

banana_lists = get_sublists_containing_keyword(lists, 'banana')
yellow_lists = get_sublists_containing_keyword(lists, 'yellow')

for banana_list in banana_lists:
print(banana_list)
for yellow_list in yellow_lists:
print(yellow_list)

How to check if a list item within a nested list exists in a set?

You can create a dictionary which keeps tracks of the number of occurrences of each word in your corpus with collections.Counter

from collections import Counter

corpus = [['Hello', ',', 'my', 'name', 'is', 'Walter'], ['I', 'like', 'my', 'cats']]

corpus_unnested = []
for sentence in corpus:
corpus_unnested += sentence
my_dict = Counter(corpus_unnested)

for i, sentence in enumerate(corpus):
for j, word in enumerate(sentence):
if my_dict[word] > 1:
corpus[i][j] = '<UNK>'
>>> print(corpus)
[['Hello', ',', '<UNK>', 'name', 'is', 'Walter'], ['I', 'like', '<UNK>', 'cats']]

Python check if an item is in a nested list at a certain index

The problem is that you don't have any "already" list to reference. You need to build that as you go. Here is an excruciatingly simple example solution:

# Keep separate lists for each position.
good_list_0 = []
good_list_1 = []

for pair in my_list:
# If either element already exists at that index ...
if pair[0] in good_list_0 or \
pair[1] in good_list_1:

# ... reverse the pair
a, b = pair[1], pair[0]
else:
a, b = pair

# add the values to the appropriate index lists
good_list_0.append(a)
good_list_1.append(b)

# Zip the two lists back together.
good_list = list(zip(good_list_0, good_list_1))

for pair in good_list:
print(pair)

Output:

('item1', 'item2')
('item3', 'item4')
('item5', 'item6')
('item7', 'item8')
('item9', 'item1')
('item10', 'item3')
('item11', 'item5')
('item12', 'item7')
('item2', 'item13')
('item4', 'item14')
('item6', 'item15')
('item8', 'item16')
('item13', 'item9')
('item14', 'item10')
('item15', 'item11')
('item16', 'item12')

Check if an item exist in a nested list in R

AllVotes is an unnamed list of 2336 elements, each is a particular voting session. So we'll need to loop on its elements for example with map(), or lapply(), or a for loop.

In addition, for a given session i, the IDs of the MEPs who voted + can be obtained with:

unlist(AllVotes[[i]]$votes$`+`$groups)

and same for - and 0.

Since you want the output to be with a column for each election, let's create a blank table and fill it. I'll use a matrix which I find slightly more practical here.

meps2 <- matrix(NA_character_,
nrow = nrow(meps),
ncol=length(AllVotes),
dimnames = list(meps$mepid,
as.character(1:length(AllVotes))))

for(i in 1:length(AllVotes)){
meps2[as.character(unlist(AllVotes[[i]]$votes$`+`$groups)), i] <- "+"
meps2[as.character(unlist(AllVotes[[i]]$votes$`-`$groups)), i] <- "-"
meps2[as.character(unlist(AllVotes[[i]]$votes$`0`$groups)), i] <- "0"
}

For loops are not generally recommended in R, but here it works quite well enough, I'm not sure there would be any gain with an apply-family function.

Take a look:

meps2[1:10,1:10]
table(is.na(meps2))
# -> note there still are lots of NA.
# Possibly MEPs that were not present?

And finally we just need to assemble the final table. The row order is the same, so we don't even need merge or match.

meps <- cbind(meps, meps2)

EDIT: your idea of using a %in% would work, but its not very efficient. You would need to loop on every voting session, extract the 3 lists of voters, then loop for every MEP on each list applying a %in% (which is a loop itself). That would be 3 loops. Here by reversing the problem, we're looping explicitly on the voting sessions and implicitly on the MEPs from each list (-, +, 0). That's only 2 loops, and filling specific rows in a matrix is pretty efficient. It could look something like (with a proper initialization):

for(vote in AllVotes){
voters_+ <- unlist(AllVotes[[i]]$votes$`+`$groups)
voters_- <- unlist(AllVotes[[i]]$votes$`-`$groups)
voters_0 <- unlist(AllVotes[[i]]$votes$`0`$groups)

meps[,vote] <-ifelse(meps$mepid %in% voters_+, "+",
ifelse(meps$mepid %in% voters_-, "-",
ifelse(meps$mepid %in% voters_0, "0", NA)))
}


Related Topics



Leave a reply



Submit