Sana ullah
Sana ullah

Reputation: 21

two list integer combine together in python

I am trying to get integers value from a list of specific functions in a two different list and then try to store both list integer with combination of 2nd list integer. let suppose we have two list,

list1 = ['A(0)','B(1)','C(3)','Z(4)','Z(7)','Z(2)', 'X(3)','X(2)',...]
list2 = ['A(0)','B(1)','C(3)','Z(7)','Z(3)','Z(5)', 'X(11)','X(4)',...]

now only the integer of Z from list1 and list2 will extract and store like this sequence,

       Z1 = A(4,7)
       Z1 = A(7,3)
       Z2 = B(2,5)

first element of list1 and 2nd element of list2 in a sequence.

here is my code which i tried,

    for line in list1:
      if 'OUTPUT' in line:
        print(line.split('Z(')[1].split(')')[0].strip())
   for line in list2:
     if 'OUTPUT' in line:
        print(line.split('Z(')[1].split(')')[0].strip())

here is output

4 7 7 3 2 5

but still i didnt get value like,

    Z1 = A(4,7)
    Z1 = A(7,3)
    Z2 = B(2,5)

Upvotes: 0

Views: 54

Answers (1)

felipe
felipe

Reputation: 8025

def format_list(lst):

    new = []
    for sub in lst:
        open_p = sub.index("(")
        close_p = sub.index(")")

        letter = sub[:open_p]
        number = sub[open_p + 1 : close_p]

        new.append((letter, number))

    return new

list1 = ["A(0)", "B(1)", "C(3)", "Z(4)", "Z(7)", "Z(2)", "X(3)", "X(2)"]
list2 = ["A(0)", "B(1)", "C(3)", "Z(7)", "Z(3)", "Z(5)", "X(11)", "X(4)"]

lst1 = format_list(list1)
lst2 = format_list(list2)

The above code will format the lists as so:

lst1 = [('A', '0'), ('B', '1'), ('C', '3'), ('Z', '4'), ('Z', '7'), ('Z', '2'), ('X', '3'), ('X', '2')]

lst2 = [('A', '0'), ('B', '1'), ('C', '3'), ('Z', '7'), ('Z', '3'), ('Z', '5'), ('X', '11'), ('X', '4')]

From there, you'll be able to use filter() to find the places in which the numbers differentiate:

different_obj = list(filter(lambda x: x[0][1] != x[1][1], zip(lst1, lst2)))
print(different_obj)

Or if you rather, you don't need to use filter:

different_obj = []
for x, y in zip(lst1, lst2):
    if x[1] != y[1]:
        different_obj.append((x, y))

outputs:

[(('Z', '4'), ('Z', '7')),
 (('Z', '7'), ('Z', '3')),
 (('Z', '2'), ('Z', '5')),
 (('X', '3'), ('X', '11')),
 (('X', '2'), ('X', '4'))]

From there you should be able to organize different_obj to your goal.

Upvotes: 1

Related Questions