1 year ago

#360321

test-img

Suhani Jain

Python String Comparison in a Dictionary

I have an excel file out of which I have extracted various data which looks something as follows:

  1. List with unique languages: lang=['EN','HI']
  2. List of unique numbers in string format: nums=['101','102','103','104','105']
  3. A python dictionary containing keys made by concatenating above 2 lists and values taken from another column in excel which looks as follows: {'101EN':'-abc', '102EN':'--df-n', '103EN':'gvh', '104EN':'----erfj-', '105EN':'---WEW', '101HI':'---aFDbc', '102HI':'-dfesdn', '103HI':'gfdvh-', '104HI':'----erffj' '105HI':'--WEW' }

So basically, I need to compare indentation(no. of hiphen in the starting) for same number with different language.

For eg: 101EN and 101HI do not have same number of hiphens in the starting and hence it should return false. Where as, 103EN and 103HI should return true as no of hiphens is equal, (in this case 0) and return a final value=pass if no errors found or value=fail even if one error is found.

Would appreciate immediate help on this!

Below is the code I have tried, both J loops working fine but not able to get the correct comparison for count1 and count2 variable:

`import pandas as pd`

`passcount=0`
`failcount=0`
`count1=1 #for language1`
`count2=1 #for language2`

`nums=df1['HTS'].unique().tolist()`

`values=df1['Description'].tolist()`

`lang=df1['LANGUAGE'].unique().tolist()`
`lang = [item for item in lang if not(pd.isnull(item)) == True]`

`keys=[]`

`for index,row in df1.iterrows():`
`    keys.append(str(row[1])+str(row[6]))#making keys for dictionary from excel columns`
`dict = {k: v for k, v in zip(keys, values)}`

`if len(lang)>1:`
`    for i in hts:`
`    #1st j loop to count hiphens for EN language code for the number i`
`    for j in range(len(str(dict.get('i' + lang[0]))) - 1):`
`        if str(dict.get('i' + lang[0]))[j] == str(dict.get('i' + lang[0]))[j + 1]:`
`#storing the value(no. of hiphens) in a temporary count1 variable for 1st language`
`            count1 = count1 + 1`
`        else:`
`            break`
`#2nd j loop to count number of hiphens for HI language code for the same number i`
`    for j in range(len(str(dict.get('i' + lang[1]))) - 1):``
`        if str(dict.get('i' + lang[1]))[j] == str(dict.get('i' + lang[1]))[j + 1]:`
`#storing the value(no. of hiphens) in a temporary count2 variable for 2nd language`
`           count2 = count2 + 1`
`       else:`
`           break`
`#Compare indentation`
`    if count1!=count2:`
`        failcount=failcount+1`
`else:`
`    print('Only one language code found')`
`print('failed: ',failcount)`

python

pandas

dataframe

string-comparison

0 Answers

Your Answer

Accepted video resources