I have two lists which are:
>>> list1 = ['gain','archive','win','success']
>>> list2 = ['i','win','game','i','am','success','cool']
and also I found the same values of both list by comparing the lists.
>>> result= set(list1) & set(list2)
Output is
set(['win', 'success'])
Now I want to find the next element value of the result. Here it would be: 'game' and 'cool'.
How can I do this (using python 2.7)?
Given that you have the intersection words
result = { 'win', 'success' }
You could find the next words in list2 like this:
next_words = [list2[list2.index(word)+1] for word in result]
index gets you the index of the given element in the list. You can add 1 to it to get the next element.
If your element is at the end of the list, it will throw an exception, because there is no "next" element to get.
You can use the index function and add 1. Be careful though, if your common element is the last one of your list, it will generate an error
list1 = ['gain','archive','win','success']
list2 = ['i','win','game','i','am','success','cool']
result= set(list1) & set(list2)
list3 = [list2[list2.index(e)+1] for e in result]
edit For the case where you last element is a common element:
result= set(list1) & set(list2)
list4 = []
for e in result:
try:
list4.append(list2[list2.index(e)+1])
except:
pass
Output: ['game', 'cool']
You could do a pairwise iteration over your list2 and do the "intersection" manually:
list1 = ['gain','archive','win','success']
list2 = ['i','win','game','i','am','success','cool']
set1 = set(list1)
result = []
for item, nextitem in zip(list2, list2[1:]): # pairwise iteration
if item in set1:
result.append(nextitem) # append the next item if the current item is in the intersection
print(result) # ['game', 'cool']
This does the trick for the next element in list2:
next_result = [list2[list2.index(el)+1] for el in result if list2.index(el)+1<len(list2)]
You could use list2.index, but that's doing a full search just for finding back an index, and artificially increasing complexity from O(n) to O(n*n).
Just keep track of the indexes of each words. There are several ways to do that.
Create your own function that search for common words, and return them as the index of those words in list2. This probably the least pythonic but the fastest.
Create a dictionary from the words of list2 to their index, then after computing the set intersection, lookup on the dict to find the index and increase by one. You need to build a full dictionary the size of list2, this might be expensive (but still better than O(n*n)).
Create a dictionary from the words of list2 to their next word or None if there aren't and do a lookup on the dict to find the index. You need to build a full dictionary the size of list2, this might be expensive.
If you know how to use itertools, you could do an iterator on list2 that yield the index and the word, and filter the result if the word is in list1, then pick only the indexes.
Related
I have a issue in lists. I have a list =['randomf','decisiont','extremeg'] which is appended with another list list2 = ['bike','age','car'], and then sorted according it together which means each value of first list have the appended values of list2(e.g:'randomf_bike','randomf_age' ,'randomf_car') but when I sort it turns to be like this(e.g: 'randomf_age','randomf_bike' ,'randomf_car') Its not only sorting according to the first letter of the value but also sorting according to the letter after '_'. How can I sort according to the first letter?
My code:
list =['randomf','decisiont','extremeg']
list2 = ['bike','age','car']
new_list = [el1+'_'+el2 for el2 in list2 for el1 in sorted(set(list))]
new_list = sorted(new_list)
which gives : new_list = ['decisiont_age','decisiont_bike' ,'decisiont_car','extremeg_age','extremeg_bike' ,'extremeg_car','randomf_age','randomf_bike' ,'randomf_car']
The excepted output is:
new_list = ['decisiont_bike','decisiont_age' ,'decisiont_car','extremeg_bike','extremeg_age' ,'extremeg_car', 'randomf_bike','randomf_age' ,'randomf_car']
You can give a key function to the call to sorted. If you want to sort only on the part of the string before the first _ character, you can do sorted(new_list, key=lambda s: s.split('_', 1)[0]).
But you might also be able to avoid that second sort, if you assemble your compound strings in the right order from the start. I think you can just change the order of your two-level iteration and it should match what you want (you're already sorting the first list):
new_list = [el1+'_'+el2 for el1 in sorted(set(list)) for el2 in list2] # change order here
# don't sort again!
For reference this is my code:
list1 = [('10.180.13.101', '10.50.60.30', 'STCMGMTUNIX01')]
list2 = [('0.0.0.0', 'STCMGMTUNIX01')]
for i in list1:
for j in list2:
for k in j:
print (k)
if k.upper() in i:
matching_app.add(j)
for i in matching_app:
print (i)
When I run it, it does not match. This list can contain two or three variables and I need it to add it to the matching_app set if ANY value from list2 = ANY value from list1. It does not work unless the tuples are of equal length.
Any direction to how to resolve this logic error will be appreciated.
You can solve this in a few different ways. Here are two approaches:
Looping:
list1 = [('10.180.13.101', '10.50.60.30', 'STCMGMTUNIX01')]
list2 = [('0.0.0.0', 'STCMGMTUNIX01')]
matches = []
for i in list1[0]:
if i in list2[0]:
matches.append(i)
print(matches)
#['STCMGMTUNIX01']
List Comp with a set
merged = list(list1[0] + list2[0])
matches2 = set([i for i in merged if merged.count(i) > 1])
print(matches2)
#{'STCMGMTUNIX01'}
I'm not clear of what you want to do. You have two lists, each containing exactly one tuple. There also seems to be one missing comma in the first tuple.
For finding an item from a list in another list you can:
list1 = ['10.180.13.101', '10.50.60.30', 'STCMGMTUNIX01']
list2 = ['0.0.0.0', 'STCMGMTUNIX01']
for item in list2:
if item.upper() in list1: # Check if item is in list
print(item, 'found in', list1)
Works the same way with tuples.
I am having a hard time trying to understand how the "for" function works.
I want to make a script that only outputs the strings in list2 that are not inside list1. For example:
list1 = ["link1.com", "link2.com", "link3.com"]
list2 = ["link2.com", "link123.com"]
for list2 in list1:
print(list2)
{My intention was that the code printed:
link123.com
But instead it printed the strings from list1}
I can't get it to work. Help would be much appreciated. I am using python 3 by the way.
Use Set for this .
set(list2)-set(list1)
Check with python set
The loop for list2 in list1 is actually an assignment: in each iteration the variable list2 gets the value of the next item in list1 - that is why only the elements of list1 are printed.
You could iterate over the elements of list2 and print, if they are not in list1:
for element in list2:
if element not in list1:
print(element)
Or if you want to use a for loop (note that this isn't very efficient for large lists):
for string in list2:
if not string in list1:
print (string)
For loops
For loops allow you to repeat a piece of code multiple times. You could easily just write a conditional and a print statement multiple times for each item in the list. A for loops allows you to write that piece of code once and have it repeated for each item in the list.
You can iterate over item in list2 by doing the following
for item in list2:
print(item)
item is an arbitrary variable name that holds the value of the current item we are on, what follows in is the list that we want to iterate over. print(item) is the piece of code we want to repeat for each element in the list.
What this does is goes through every item in list2 and prints them but that is not what we want. We want to check to make sure that item is not in list1. This can be achieved through a conditional statement.
if item not in list1:
print(item)
Now we can join the two piece of code together.
for item in list2:
if item not in list1:
print(item)
Sets
Are a collection of items in no order where every item is unique. These sets are the same ones we encounter in mathematics, therefore we can perform mathematical set operations on them.
To go from a list of items to a set we use sList1 = set(list1) sList1 is now of type set and stores the elements in list1. The same can be done for list2.
Now that we have sList1 and sList2 we want to eliminate any duplicates in the two for that we can take the difference of sList1 and sList2 and print them out as follows print(sList2-sList1).
We can do all of this in one step.
print( set(list2) - set(list1) )
The semantic, that python will check if these items are in list1 is NOT part of the for-each-loop.
The 'set' - solution is maybe too advanced for you.
So straightforward you would:
for item in list2:
if item not in list1:
print(item)
I have the following two lists:
list1 = [(('diritti', 'umani'), 'diritto uomo'), (('sgomberi', 'forzati'), 'sgombero forza'), (('x', 'x'), 'x x'), ...] ## list of tuples, each tuple contains term and lemma of term
list2 = ['diritto uomo', 'sgombero forza'] ### a small list of lemmas of terms
The task is to extract from the list1 the terms whose lemmas are present in the list2. note that one element in list2 can share the lemma with more than one term in list1, so for every item in list2 I need to find its shared items in list1. I tried this code:
result = []
for item in list2:
for x in list1:
for i, ii in x:
if item.split()[0] in ii or item.split()[1] in ii :
result.append(i)
This code takes a long time to do the task, can someone suggest another way to do this. Thanks
If you just want to match the equal lemmas you don't need to split your words and check the membership you can simply use == operation within a list comprehension:
>>> [item for item, lemm in list1 for w in list2 if w == lemm]
[('diritti', 'umani'), ('sgomberi', 'forzati')]
Otherwise by splitting the lemmas and membership checking within list1's lemmas it won't give you any result.
I have the next list of
testList = []
testList.append([0,-10])
testList.append([-12,122])
testList.append([13,172])
testList.append([17,296])
testList.append([-10,80])
testList.append([-16,230])
testList.append([-18, 296])
testList.append([-2, -8])
testList.append([-5,10])
testList.append([2,-4])
and another lists which contains elements from previous list:
m1 = []
m1.append([0, -10])
m1.append([13, 172])
Then I try to get a subarray from the list testList with the next statement:
[element for i, element in enumerate(testList) if i not in m1]
But I get the same list as testList.
How can I achieve this?
If you don't care about the order in the lists, you can use sets instead:
# result will be a set, not a list
not_in_testlist = set(testlist) - set(m1)
If you want the result to be a list again:
# result will be a list with a new order
not_in_m1 = list(set(testlist) - set(m1))
Be aware that using sets will lose the order of the original lists because sets are unordered types (they use hashing under the hood).
If you need to preserve the order, then Andrew Allaire's answer is correct:
# result is a list, order is preserved
not_in_testlist = [e for e in testlist if e not in m1]
The problem is with your use of enumerate. The i is just going to be an integer, and therefor never in a list that only has lists in it. Try this:
[element for element in testList if element not in m1]
Try with this:
def clean_list(my_list, exclusion_list):
new_list = []
for i in my_list:
if i in exclusion_list:
continue
else:
new_list.append(i)
return new_list