x = [[1,2,3],[2,4,5]]
How do you get the index of 2 when the list in a list of a list?
I have tried below but it is not working
a = x.index(2)
The output I want to see is like 0 and 1.
You can use enumerate in a list comprehension that filters the output based on whether 2 is in the current sub-list:
[i for i, s in enumerate(x) if 2 in s]
Related
p = [i.text.strip() for i in soup.select('p.card-text')]
j = []
for i in p:
if p.index(i)%2 == 0:
j.append(i)
I am doing this because I only want to extract even position element from my list p.
Is there any other way to do this (to get only even position element from list)?
If not, how can I write the code above using list comprehension? I have extracted even position element using this code. I want to know of any other method I can apply or how to write list comprehension for the above code?
You can try the following by using list comprehension along with enumerate in python
p = [i.text.strip() for index,i in enumerate(soup.select('p.card-text')) if index%2==0]
Um... simply slice:
j = p[::2]
Or if select returns a list (looks like it does), do it earlier to save work:
soup.select('p.card-text')[::2]
I came across appending elements to array from left. and it has two solution
Solution 1:
List = [2,3,4,5,6]
List.insert(0,1) # Using insert
List = [1,2,3,4,5,6]
Solution 2:
List = [2,3,4,5,6]
[1] + List # Concatenation of array
List = [1,2,3,4,5,6]
I'm new to Python So please can any one explain the time complexity of both solution according to me both solution takes O(n) am I right or wrong?
I have a python list like
the_list= ['john',"nick","edward","mood","enp","wick"]
i always want the mood and enp to be in the 0th and 1st index of list rest order can be anything.
so the output will be
op_list= ["mood","enp",........rest..]
The following will work:
op_list = ["mood", "enp"] + [x for x in the_list if x not in ("mood", "enp")]
This assumes the two special elements are always present.
I have a list containing the system time from a machine. Since the list contains only the milliseconds part, the values donĀ“t go beyond 1000.
For better viewing I want to add i*1000 to certain intervals in this list for each time the list skips 1000. For better understanding i will give my input list and what my output list should look like:
inputlist = [300,600,900,200,500,800,100,400]
etc, the output list should look like this:
outputlist = [300,600,900,1200,1500,1800,2100,2400]
since I want the list to start with zero i subtracted the first element of the list from each element giving me a new inputlist:
inputlist_new = [0,300,600,-100,200,500,-200,100]
which should give me a new outputlist like:
outputlist_new = [0,300,600,900,1200,1500,1800,2100]
I tried creating a list containing the indices of each element < 0 to cut the list into intervals, to multiply the thousands on each interval but I am not able to do so. My code for this index list is this:
list_index = [i for i, j in enumerate(inputlist_new) if j < 0]
I actually found a solution myself: i copied the list i want to change into a new one, just for the sake of maybe using it later (so this is not necessary for the solution) and then used 2 for loops:
inputlist_new = [0,300,600,-100,200,500,-200,100]
inputlist2 = inputlist_new.copy()
for x in range(len(list_index)):
for y in range(list_index[x],len(inputlist2)):
inputlist2[y] = inputliste2[y]+1000
this gave me the wanted output
Is it possible to convert this into a list comprehension? For example, I have a list v. On the source code below, v = dictionary.keys()
v = ["naive", "bayes", "classifier"]
I have the following nested list t.
t = [["naive", "bayes"], ["lol"]]
The expected output O should be:
O = [[1 1 0], [0 0 0]]
1 if the dictionary contains the word and 0 if not. I'm creating a spam/ham feature matrix. Due to the large dataset, I'd like to convert the code below into a list comprehension for a faster iteration.
ham_feature_matrix = []
for each_file in train_ham:
feature_vector = [0] * len(dictionary)
for each_word in each_file:
for d,dicword in enumerate(dictionary.keys()):
if each_word == dicword:
feature_vector[d] = 1
ham_feature_matrix.append(feature_vector)
I couldn't test this, but this translates as:
ham_feature_matrix = [[[int(each_word == dicword) for dicword in dictionary] for each_word in each_file] for each_file in train_ham]
[int(each_word == dicword) for dicword in dictionary] is the part which changes the most compared to your original code.
Basically, since you're iterating on the words of the dictionary, you don't need enumerate to set the matching slots to 1. The comprehension builds the list with the result of the comparison which is 0 or 1 when converted to integers. You don't need to get the keys since iterating on a dictionary iterates on the keys by default.
The rest of the loops is trivial.
The issue I'm seeing here is that you're iterating on a dictionary to create a list of booleans, but the order of the dictionary isn't fixed, so you'll have different results each time (like in your original code) unless you sort the items somehow.