Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Associating two item in a list

Tags:

I am comparing two lists for common strings and my code currently works to output items in common from two lists.

list1

['5', 'orange', '20', 'apple', '50', 'blender'] 

list2

['25', 'blender', '20', 'pear', '40', 'spatula'] 

Here is my code so far:

for item1 in list1[1::2]:     for item2 in list2[1::2]:         if item1 == item2:             print(item1) 

This code would return blender. What I want to do now is to also print the number before the blender in each list to obtain an output similar to:

blender, 50, 25 

I have tried to add two new lines to the for loop but did not have the desired output:

for item1 in list1[1::2]:     for item2 in list2[1::2]:         for num1 in list1[0::2]:             for num2 in list2[0::2]:                if item1 == item2:                    print(item1, num1, num2) 

I know now that making for loops is not the answer. Also trying to call item1[-1] does not work. I am new to Python and need some help with this!

Thank you

like image 893
stevesy Avatar asked Sep 18 '18 15:09

stevesy


People also ask

How do I add two elements to a list?

Using + operator The + operator does a straight forward job of joining the lists together. We just apply the operator between the name of the lists and the final result is stored in the bigger list. The sequence of the elements in the lists are preserved.

How do you join two elements in a list Python?

Note: The join() method provides a flexible way to create strings from iterable objects. It joins each element of an iterable (such as list, string, and tuple) by a string separator (the string on which the join() method is called) and returns the concatenated string.

How do I assign a list of values from one list to another?

When you copying a list to another list using = sign, both the list refer to the same list object in the memory. So modifying one list, other automatically changes as they both refer to same object. To copy a list use slice operator or copy module.


2 Answers

You can do it in two ways, either stay with lists (Which is more messy):

list1 = ['5', 'orange', '20', 'apple', '50', 'blender'] list2 = ['25', 'blender', '20', 'pear', '40', 'spatula'] for item1 in list1[1::2]:   for item2 in list2[1::2]:     if item1 == item2:        item_to_print = item1        print(item1, ",", end="")        for index in range(len(list1)):            if list1[index] == item1:                print(list1[index - 1], ",", end="")        for index in range(len(list2)):            if list2[index] == item1:                print(list2[index - 1]) 

or the better way (in my opinion) with dictionary:

dict1 = {'apple': '20', 'blender': '50', 'orange': '5'} dict2 = {'blender': '25', 'pear': '20', 'spatula': '40'} for item in dict1:    if item in dict2:        print(item, ",", dict1[item], ",", dict2[item]) 

Both will output:

>> blender , 50 , 25 
like image 153
MercyDude Avatar answered Oct 22 '22 05:10

MercyDude


You're approaching this problem with the wrong Data Structure. You shouldn't keep this data in lists if you're doing lookups between the two. Instead, it would be much easier to use a dictionary here.

Setup

You can use zip to create these two dictionaries:

a = ['5', 'orange', '20', 'apple', '50', 'blender'] b = ['25', 'blender', '20', 'pear', '40', 'spatula']  dct_a = dict(zip(a[1::2], a[::2])) dct_b = dict(zip(b[1::2], b[::2])) 

This will leave you with these two dictionaries:

{'orange': '5', 'apple': '20', 'blender': '50'} {'blender': '25', 'pear': '20', 'spatula': '40'} 

This makes every part of your problem easier to solve. For example, to find common keys:

common = dct_a.keys() & dct_b.keys() # {'blender'} 

And to find all the values matching each common key:

[(k, dct_a[k], dct_b[k]) for k in common] 

Output:

[('blender', '50', '25')] 
like image 39
user3483203 Avatar answered Oct 22 '22 04:10

user3483203