0

New to programming. I'm trying to create a program that has a list o list (As a data base) the first element being an invoice, the second the value of that invoice and the third, the margin of income. Given an invoice (input), it will search within the list o lists(data base).

I currently have:

Data_base= [["f1",2000,.24],["f2",150000,.32],["f3",345000,.32]]
invoice = input("Enter invoice: ")

if invoice in data_base :
    print ("Invoice is valid")

else :
    print("Invoice does not exist, enter a valid document: ")

When I run the program and I enter a valid element within the list, it does not recognize as an element within the list.

What am I doing wrong?

What is the code to be given the index if the element is within the list of lists?

idjaw
  • 25,487
  • 7
  • 64
  • 83
smera
  • 15
  • 4
  • You are dealing with a list of lists. So, your condition `if invoice in data_base` will not suffice. You will have to iterate over your list, and then use that condition – idjaw Sep 27 '16 at 00:40

4 Answers4

1

in keyword in Python is used to check the membership of an element in a iterator. That is each element of the iterator is checked with the operand and returns True if the operand exists in the iterator.

The in keyword is specifically looking for invoice in data_base which is a list. Now, it is a different story that each element of this list is another list.

The line if invoice in data_base : does the following, Say the user input was "f1" now "f1" is compared to the list ["f1",2000,.24] that is

"f1" == ["f1",2000,.24] which is False, you are assuming this should return True but it does not, since string "f1" is not equal to the list ["f1",2000,.24].

then

"f1" == ["f2",150000,.32] which is False

and finally

"f1" == ["f3",345000,.32] which is also False.

You are essentially comparing the invoice(user input) to the whole element instead of just the first element of the element.

This program should give you the correct output.

Data_base= [["f1",2000,.24],["f2",150000,.32],["f3",345000,.32]]
invoices_db = [ele[0] for ele in Data_base]
invoice = input("Enter invoice: ")
if invoice in invoices_db :
    print ("Invoice is valid")
else :
    print("Invoice does not exist, enter a valid document: ")

For more details refer the answer on Use and meaning of "in" in an if statement in python

Community
  • 1
  • 1
Sreejith Menon
  • 1,057
  • 1
  • 18
  • 27
1

If you wish to search by invoice number, a better data structure to use is the dictionary. Use the invoice number as the key, and the invoice value and margin as values. Example:

invoices = {'f1': [2000, .24],
            'f2': [150000, .32],
            'f3': [345000, .32]}

invoice = input("Enter invoice: ")
if invoice in invoices:
    value, margin = invoices[invoice]
    print('Invoice {} is valid. Value {}, margin {}'.format(invoice, value, margin))
else:
    print("Invoice does not exist, enter a valid document: ")

Now when you say invoice in invoices Python will search for the invoice number in the dictionary's keys. Compared to searching in a list this is a very efficient operation (O(1) vs. O(n)).

mhawke
  • 84,695
  • 9
  • 117
  • 138
0

Here's the solution I found:

data_base = [["f1",2000,.24],["f2",150000,.32],["f3",345000,.32]]
invoice = input("Enter invoice: ")

found = False

for data in data_base:
    if invoice == data[0]:
        found = True
        break

result = "Invoice is valid" if found else "Invoice does not exist, enter a valid document: "

print(result)

First you should iterate in all data_base elements and then check if the first element is the invoice you're looking for.

Arthur Gouveia
  • 734
  • 4
  • 12
0
Data_base= [["f1",2000,.24],["f2",150000,.32],["f3",345000,.32]]

invoice = 2000

result = [i for i in Data_base if i[1] == invoice]

for item in result:

    print item
Howardyan
  • 667
  • 1
  • 6
  • 15
  • invoice is the input value and I just set to a fix number. and use invoice to filter the data from data_base. result = [i for i in Data_base if i[1] == invoice] – Howardyan Sep 28 '16 at 03:04