简体   繁体   中英

How to check number exist or not in python for loop

I have an excel sheet with this format:

from    amount  to

314 $470.21 275

12  $1,788.98   149

316 $2,949.53   417

5   $2,193.48   454

198 $1,402.76   371

82  $1,212.14   420

222 $1,167.72   396

and render all csv data with for loop:

import csv

def csv_data(path):
    with open(path) as csv_file:
        readCSV = csv.DictReader(csv_file)

        for line in readCSV:
            col_from = line['from']
            col_to = line['to']
            col_ammount = line['amount']
            print(col_from, col_amount, col_to)

csv_data('transactions.csv')

For example in "from column" i found value 314 now how can i check in all values of "to column" that this value is exist or not.actually i want to calculate current balance of all account numbers from and to.

How can add if condition properly in for loop ?

I give you are looking something like this..

for line in readCSV:
    if line['form']:
        #do something if "from" exist
    else:
        #do something if "from" do not exit
    if line['to']:
        # do something if "to" exist
    else:
        # do something if "to" do not exit
    if line['amount']:
        #do something if "amount" exist
    else:
        #do something if "amount" do not exist

I think your main problem is that you overwrite the col_from , col_to and col_amount list over and over again. Printing the value from within the loop may confuse you into thinking all your values would be stored in that lists. Try and call print(col_from, col_amount, col_to) outside the for-loop and you will only get the last element. A better way would be to create empty lists and append the items.

Secondly I think you should mind the data format which is int and float, but the csv.DictReader will return strings for all the elements in your matrix.

Then, checking if values of col_from also appear in col_to is easily done with list comprehension. You can create a list that contains "True" for all lines in which your condition applies and "False" if they do not (which is the case for all your sample data).

A suggestion that is close to your solution would be:

import csv

def csv_data(path):
    with open(path) as csv_file:
        readCSV = csv.DictReader(csv_file, delimiter=";", quoting=csv.QUOTE_NONE) # supposed that your delimiter is ";", otherwise use , or \tab

        col_from, col_to, col_amount = [], [], [] # initialize empty lists

        for line in readCSV: # append entries to your lists
            col_from.append(int(line['from']))
            col_to.append(int(line['to']))
            col_amount.append(float(line['amount']))

        bool_list = [True if col_from[i] in col_to else False for i in range(len(col_from))] # A list with Trues and Falses
        return bool_list

print csv_data('transactions.csv')
# out: [False, False, False, False, False, False, False]

Assuming you are successfully able to store values from the 'to' and 'from' columns in a list, you can perform an intersection operation on the 2 lists to get all the common values that exist in both the list.

col_from = [314, 12, 316, 5, 198, 82, 222]
col_to = [275, 149, 417, 454, 371, 420, 396]
intersection = list(set(col_from) & set(col_to))

Now intersection should contain all the common values from both the list, which in this case will be an empty list. You can also search for specific values by modifying it a bit

list(set([314]) & set(col_to))

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