0

I have to compare 2 lists of strings fetched from the database and from a CSV file respectively.

The data objects could vary in length.

The condition

if obj1 >= obj2 the perform the code. but since the data can vary in length I have to hard code every possibility according to the length of the objects, which is tiring and wrong.

According to the code if a obj1 length is greater than obj2 length then perform the program.

Can you please tell me a way where I can compare a string of length 8 and length 4 (example numbers) without hard coding it through if else. I believe this is achievable through while loop. Which is sadly my weak point. and this comparison goes until the first character is reached and when WHILE reaches 0 it breaks.

Meaning if I get an object of length 2 and the other object length is 3, How can I perform recursive compressing of the objects through while loop.

HELP would be greatly appreciated.

I hope you get the question.

def LCR():
    dest = Destination.objects.values_list('dest_num', flat=True)
# This method turn the fetched data into a list. Example
#['1233','4412','23135','123']
    rates = {}
    ratelist = {}



    csv_file = open(r'.\adoc.csv')



    data_set = csv_file.read().decode("UTF-8")
    io_string = io.StringIO(data_set)
    next(io_string)

    for column in csv.reader(io_string, delimiter=str(u",")):
        rates[column[0]] = column[1]
#Example data
#['8523654','754','4563']



    for desNum in dest:
        desNum = str(desNum)
        # print type(desNum)


        for venNum, venValue in rates.items():
            venlen = len(venNum)
            deslen = len(desNum)
            if venlen >= deslen:



                if desNum[:-1] == venNum[:-1] and desNum[:-2] == venNum[:-2] and desNum[:-3] == venNum[:-3]:
                    print (venNum)
                    print (desNum)
                    # print "Works well"
                    # print ('====================')

                    ratelist[venNum] = venValue




                elif desNum[:-1] == venNum[:-2] and desNum[:-2] == venNum[:-3] and desNum[:-3] == venNum[:-4]:

                    # ratelist[venNum] = venValue
                    print (venNum)
                    print (desNum)


                elif desNum[:-1] == desNum[:-3] and desNum[:-2] == venNum[:-4] and desNum[:-3] == venNum[:-5]:

                    # ratelist[venNum] = venValue
                    print (venNum)
                    print (desNum)



                elif desNum[:-1] == venNum[:-4] and desNum[:-2] == venNum[:-5]:

                    # ratelist[venNum] = venValue
                    print (venNum)
                    print (desNum)


                elif desNum[:-1] == venNum[:-5] and desNum[:-2] == venNum[:-6]:
                    # ratelist[venNum] = venValue
                    print (venNum)
                    print (desNum)
            else:
                print "LENGTH ERROR"
    print ratelist
8
  • Looks like you just want to know if venNum starts with desNum? Commented Jul 25, 2019 at 7:24
  • starts from the end side yes. But since the length could be different I can't use -5 or -3 etc like that. Or can I? Commented Jul 25, 2019 at 7:28
  • You can just do if venNum.startswith(desNum) rather than all of those nested if statements Commented Jul 25, 2019 at 7:30
  • I have to make sure the sequence matches. if [:-1] == [:-1] and [:-2] == [:-2] and [:-3]==[:-3] . . . . . Something like that Commented Jul 25, 2019 at 7:32
  • venNum.startswith(desNum) is exactly doing that. Commented Jul 25, 2019 at 7:43

1 Answer 1

1

Since I couldn't understand what exactly is the question, I guess you want to check one of these conditions.

If you would like to know if string1 contains string2 you could do:

if string2 in string1:
    # Do what you want to do

string2 in string1 will return True if string1 contains string2.

Or

if string1.find(string2) != -1:
    # Do what you want to do

string1.find(string2) will return the index string2 occurs in string1. If string2 couldn't be found, it returns -1.

If you would like to know if string1 starts or ends with string2 you could do:

if string1.startswith(string2):
    # Do what you want to do
if string1.endswith(string2):
    # Do what you want to do

So in your case string1 would be equal to the venNum and string2 would be equal to the desNum. You could implement it like this:

if venNum in desNum:
    # Do what you want to do
if venNum.find(desNum) != -1:
    # Do what you want to do
if venNum.startswith(desNum):
    # Do what you want to do
if venNum.endswith(desNum):
    # Do what you want to do
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.