简体   繁体   中英

How can I get unique values from a list when I use loop and condition

I wanted to create a table where Item Code is main row and Challan No. Is sub row. For every item Item Code is only one and Challan No. is only one. But same Challan No. are allowed for different Item Code.

item_table = [('I-101', 'Wall Tiles'), ('I-102', 'Floor Tiles'), ('I-103', 'Wall Tiles')]
in_table = [('I-101', 'C-1415', 100.0), ('I-102', 'C-1469', 110.0), ('I-103', 'C-1455', 120.0), ('I-101', 'C-1897', 130.0), ('I-101', 'C-1415', 140.0), ('I-102', 'C-1415', 150.0), ('I-103', 'C-1897', 160.0)]
out_table = [('I-101', 'C-1415', -100.0), ('I-103', 'C-1455', -500.0)]
for elem in item_table:
    print(elem[0])
    for bill in in_table:
        if elem[0] == bill[0]:
            print(bill[1])

This is my problem

这是我的问题

this is my main design

这是我的主要设计

Make a set of all the challan numbers that have been printed, so you don't print duplicates.

item_table = [('I-101', 'Wall Tiles'), ('I-102', 'Floor Tiles'), ('I-103', 'Wall Tiles')]
in_table = [('I-101', 'C-1415', 100.0), ('I-102', 'C-1469', 110.0), ('I-103', 'C-1455', 120.0), ('I-101', 'C-1897', 130.0), ('I-101', 'C-1415', 140.0), ('I-102', 'C-1415', 150.0), ('I-103', 'C-1897', 160.0)]
out_table = [('I-101', 'C-1415', -100.0), ('I-103', 'C-1455', -500.0)]
for elem in item_table:
    printed = set()
    print(elem[0])
    for bill in in_table:
        if elem[0] == bill[0] and bill[1] not in printed:
            print(bill[1])
            printed.add(bill[1])

DEMO

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM