mp252
mp252

Reputation: 475

Loop through two lists where one is smaller than the other

I have two lists where one list is a list of tuples (will call it list2), and another is a list of strings (will call it list1).

list1 is smaller than list2 but I want to be able to loop through them both and compare values from list1 in each tuple that is in list2.

So far I have been managing to get results but it does not have the in the correct order.

My code so far is;

list1 = ['2018-24', '2018-23', '2018-22', '2018-21', '2018-20', '2018-19', '2018-18', '2018-17', '2018-16', '2018-15', '2018-14', '2018-13', '2018-12', '2018-11']

list2 = [(27, 'Deutsche Bank', 'Opportunities email 2018-7'), (5, 'Deutsche Bank', 'Opportunities email 2018-2'), (4, 'Deutsche Bank', 'Opportunities email 2018-6'), (3, 'Deutsche Bank', 'Opportunities email 2018-10'), (3, 'Deutsche Bank', 'Opportunities email 2018-14'), (3, 'Deutsche Bank', 'Opportunities email 2017-50'), (3, 'Deutsche Bank', 'Opportunities email 2018-12'), (3, 'Deutsche Bank', 'Opportunities email 2018-4'), (2, 'Deutsche Bank', 'Opportunities email 2018-5'), (2, 'Deutsche Bank', 'Opportunities email 2018-3'), (2, 'Deutsche Bank', 'Opportunities email 2017-51'), (1, 'Deutsche Bank', 'Opportunities email 2018-13'), (1, 'Deutsche Bank', 'Opportunities email 2018-11')]

new_list = []

for x in list2:
    ii = x[2].split(' ')[2]
    if ii not in list1:
        new_list.append(0)
    else:

        print('This is ii ' + ii)
        for y in list1:
            if ii in y:
                new_list.append(x[0]) 
print(new_list)
print(len(new_list))
print(len(list1))
o/p > 
'This is ii 2018-14'
'This is ii 2018-13'
'This is ii 2018-12'
'This is ii 2018-11'
[0, 0, 0, 3, 0, 3, 0, 0, 0, 0, 0, 1, 1]
13
14

It can be seen above that the len of the new_list and list1 are different they should both be the same. But also it can be seen that the order of the new_list does not match up with the list1 values so for example the value at index 3 of the new_list should actually be at index 10. I am not sure why it seems to be re-ordering the new_list.

The output I am expecting to get is new_list and list1 to be the same lengths, but to also have corresponding values, so for example if ii in y was true and ii was 2018-14 then when taking into consideration index 10 for both new_list and list1 the values respectively should be 3 and 2018-14, another example of the expected output if considering index 0 of both new_list and list1 then the values respectively should be 0 and 2018-7.

A full breakdown of expected outputs is listed below;

list1 = ['2018-24', '2018-23', '2018-22', '2018-21', '2018-20', '2018-19', '2018-18', '2018-17', '2018-16', '2018-15', '2018-14', '2018-13', '2018-12', '2018-11']

new_list = [0, 0, 0, 0, 0, 0, 0, 0 , 0, 0, 3, 1, 3, 1]

Upvotes: 0

Views: 99

Answers (4)

hygull
hygull

Reputation: 8740

@mp252, I appreciate the answers of this problem and I've few set of suggestions that I want to share with a simple code example executed on Interactive Python Terminal. Finally, I've answered the question using map() and dictionary comprehension, it's similar to Kevin's answer.

Try problem online at http://rextester.com/VODUZ45704.

» Don't use spit(' '), use split() to ignore extra spaces.

Have a look at the below sequence of statements executed on Python Interactive Terminal.

>>> s = 'Opportunities email 2018-7'
>>> s.split();
['Opportunities', 'email', '2018-7']
>>>
>>> s.split(' ');
['Opportunities', 'email', '2018-7']
>>>
>>> s = 'Opportunities           email    2018-7'
>>> s.split();
['Opportunities', 'email', '2018-7']
>>>
>>> s.split(' ');
['Opportunities', '', '', '', '', '', '', '', '', '', '', 'email', '', '', '', '2018-7']
>>>
>>> s = '  Opportunities           email    2018-7  '
>>> s.split();
['Opportunities', 'email', '2018-7']
>>>
>>> s.split(' ');
['', '', 'Opportunities', '', '', '', '', '', '', '', '', '', '', 'email', '', '', '', '2018-7', '', '']
>>>

Now, you can try the below code to get the new_list.

  • Dictionary is fast as any of its item can be searched in O(1) time and it supports string indexes unlike arrays so we can use it to map '2018-7'=>27, 2018-2=>5 etc.

  • So finally, we can iterate over list by looking for the existence of its item as key inside dictionary. If it presents add the corresponding value to new_list else add 0.

list1 = ['2018-24', '2018-23', '2018-22', '2018-21', '2018-20', '2018-19', '2018-18', '2018-17', '2018-16', '2018-15', '2018-14', '2018-13', '2018-12', '2018-11']

list2 = [(27, 'Deutsche Bank', 'Opportunities email 2018-7'), (5, 'Deutsche Bank', 'Opportunities email 2018-2'), (4, 'Deutsche Bank', 'Opportunities email 2018-6'), (3, 'Deutsche Bank', 'Opportunities email 2018-10'), (3, 'Deutsche Bank', 'Opportunities email 2018-14'), (3, 'Deutsche Bank', 'Opportunities email 2017-50'), (3, 'Deutsche Bank', 'Opportunities email 2018-12'), (3, 'Deutsche Bank', 'Opportunities email 2018-4'), (2, 'Deutsche Bank', 'Opportunities email 2018-5'), (2, 'Deutsche Bank', 'Opportunities email 2018-3'), (2, 'Deutsche Bank', 'Opportunities email 2017-51'), (1, 'Deutsche Bank', 'Opportunities email 2018-13'), (1, 'Deutsche Bank', 'Opportunities email 2018-11')]

d = {tup[2].split()[2]:tup[0] for tup in list2};
new_list = list(map(lambda date: d[date] if date in d else 0, list1));

print(new_list);

» Output

[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 1, 3, 1]

Upvotes: 0

Kevin
Kevin

Reputation: 76194

Assuming that there are no repeats in the third elements of list2, I'd be inclined to put them into a dictionary that maps dates to numbers. Then you can iterate through list1 and fetch those numbers out of the dictionary, using 0 as a fallback value if the date doesn't exist.

d = {description.split(' ')[2]: number for number, _, description in list2}
result = [d.get(date, 0) for date in list1]
print(result)

Result:

[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 1, 3, 1]

Upvotes: 1

MooingRawr
MooingRawr

Reputation: 4991

list1 = ['2018-24', '2018-23', '2018-22', '2018-21', '2018-20', '2018-19', '2018-18', '2018-17', '2018-16', '2018-15', '2018-14', '2018-13', '2018-12', '2018-11']

list2 = [(27, 'Deutsche Bank', 'Opportunities email 2018-7'), (5, 'Deutsche Bank', 'Opportunities email 2018-2'), (4, 'Deutsche Bank', 'Opportunities email 2018-6'), (3, 'Deutsche Bank', 'Opportunities email 2018-10'), (3, 'Deutsche Bank', 'Opportunities email 2018-14'), (3, 'Deutsche Bank', 'Opportunities email 2017-50'), (3, 'Deutsche Bank', 'Opportunities email 2018-12'), (3, 'Deutsche Bank', 'Opportunities email 2018-4'), (2, 'Deutsche Bank', 'Opportunities email 2018-5'), (2, 'Deutsche Bank', 'Opportunities email 2018-3'), (2, 'Deutsche Bank', 'Opportunities email 2017-51'), (1, 'Deutsche Bank', 'Opportunities email 2018-13'), (1, 'Deutsche Bank', 'Opportunities email 2018-11')]

new_list = [0]*len(list1)

for x in list2:
    i = x[2].split(' ')[2]
    if i in list1:
        new_list[list1.index(i)] = x[0]

print(new_list) #[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 1, 3, 1]

Try initializing the new_list to be a list of 0 and then looking up the index on where to put it in the new_list using .index(value) with the new value, it's not the most optimized way of doing things but it should get the job done.


Why are you wanting to store it like this. It seems to me like you might want to map the values of new_list back to list1 as some sort of look up. If that's the case you might want to consider using a dictionary for key-value pairs look up, it's quicker and better, which is the first line to Kevin's answer.

Upvotes: 0

Goran
Goran

Reputation: 269

list1 = ['2018-24', '2018-23', '2018-22', '2018-21', '2018-20', '2018-19', '2018-18', '2018-17', '2018-16', '2018-15', '2018-14', '2018-13', '2018-12', '2018-11']
list2 = [(27, 'Deutsche Bank', 'Opportunities email 2018-7'), (5, 'Deutsche Bank', 'Opportunities email 2018-2'), (4, 'Deutsche Bank', 'Opportunities email 2018-6'), (3, 'Deutsche Bank', 'Opportunities email 2018-10'), (3, 'Deutsche Bank', 'Opportunities email 2018-14'), (3, 'Deutsche Bank', 'Opportunities email 2017-50'), (3, 'Deutsche Bank', 'Opportunities email 2018-12'), (3, 'Deutsche Bank', 'Opportunities email 2018-4'), (2, 'Deutsche Bank', 'Opportunities email 2018-5'), (2, 'Deutsche Bank', 'Opportunities email 2018-3'), (2, 'Deutsche Bank', 'Opportunities email 2017-51'), (1, 'Deutsche Bank', 'Opportunities email 2018-13'), (1, 'Deutsche Bank', 'Opportunities email 2018-11')]
new_list = []

for x in range(len(list1)):
    try:
        ii = list2[x][2].split(' ')[2]
        if ii not in list1:
            new_list.append(0)
        else:

            print('This is ii ' + ii)
            for y in list1:
                if ii in y:
                    new_list.append(list2[x][0])
    except IndexError:
        new_list.append(0)
print(new_list)
print(len(new_list))
print(len(list1))
print(len(list2))

Upvotes: 0

Related Questions