Skip to content
Advertisement

appending string to a list from an other list

well, I have 2 lists (names and bilananu2017) names contains the names of companies and bilananu2017 contain a pdf of each company there is some missing links the problem is that names length is 80 and bilananu2017 length is 75 i want both lists to be the same length so I can make a data frame. I have this idea of adding a string “null” in bilananu2017 for each missing link basically ill compare each company name with all the link if there a link that contains the campany name then append the link in a new list if not append(“null”) so at the end ill have a new list with the length of names where there is null for each missing link i tried this code

for n in names:
    t=False
    for b in bilananu2017:
        if n in b:
            bilananu2017_2.append(b)
            t=True
    if t==False:
        bilananu2017_2.append("Null")

it does the work but for somehow bilananu2017 length is now 89 ??? its supposed to be 80 so i tried this code:

for n in names:
    t=False
    if any(n in bil for bil in bilananu2017):
        bilananu2017_2.append(bil)
        t=True
    if t==False:
        bilananu2017_2.append("Null")

now I got an error that says bil is not defined well that’s obvious but how can I fix it ?

that’s what i have in names

['ab', 'adwya', 'aetec', 'al', 'alkim', 'ams', 'artes', 'assad', 'ast', 'atb', 'atl', 'tjari', 'tjl', 'maghrebia', 'bl', 'bh', 'ml', 'biat', 'bna', 'bt', 'bte', 'salim', 'cc', 'cell', 'creal', 'cil', 'scb', 'city', 'dh', 'lstr', 'nakl', 'sokna', 'ecycl', 'gif', 'hl', 'icf', 'lndor', 'mag', 'mip', 'mnp', 'mpbs', 'nbl', 'oth', 'office', 'pltu', 'pgh', 'sah', 'sam', 'smd', 'servi', 'sfbt', 'siame', 'simpa', 'sipha', 'sits', 'somoc', 'sopat', 'sotem', 'sotet', 'stpap', 'stpil', 'mgr', 'sotuv', 'spdit', 'star', 'stb', 'steq', 'stip', 'tgh', 'tlnet', 'tpr', 'tinv', 'tre', 'tair', 'tls', 'uadh', 'ubci', 'uib', 'unimed', 'wifack']

in bilananu2017 its 75 links so ill put 4 so you can have an idea:

http://www.bvmt.com.tn/sites/default/files/societes/ab/etats-financiers/ab-etats-financiers-annuels-31-12-2017.pdf',
 'http://www.bvmt.com.tn/sites/default/files/societes/adwya/etats-financiers/adwya-etats-financiers-annuels-individuels-31-12-2017.pdf',
 'http://www.bvmt.com.tn/sites/default/files/societes/aetec/etats-financiers/aetec-etats-financiers-annuels-31-12-2017.pdf', 
'http://www.bvmt.com.tn/sites/default/files/societes/alkim/etats-financiers/alkim-etats-financiers-annuels-individuels-31-12-2017.

for example: the first name in names is “ab” if we try to look in the link for that name we will find that the first link contains “ab”

http://www.bvmt.com.tn/sites/default/files/societes/ab/etats-financiers/ab-etats-financiers-annuels-31-12-2017.pdf

here /societes/ab

Advertisement

Answer

Try this it will work fine:

import re

names=['ab','bbb','ccc','adwya'] #Dummy Data
bilananu2017=['http://www.bvmt.com.tn/sites/default/files/societes/ab/etats-financiers/ab-etats-financiers-annuels-31-12-2017.pdf', 'http://www.bvmt.com.tn/sites/default/files/societes/adwya/etats-financiers/adwya-etats-financiers-annuels-individuels-31-12-2017.pdf']
pattern = re.compile("http://www.bvmt.com.tn/sites/default/files/societes/(.*?)/")
toBeAdded = []
for i in range(len(names)):
    status=False
    for bil in bilananu2017:
        x=re.search(pattern, bil)
        if x:
            if names[i] == x.group(1):
                status = True
                break
    if(status==False):
        toBeAdded.append(i)

for i in toBeAdded:
    bilananu2017.insert(i,"Null")

print(bilananu2017)

Output

['http://www.bvmt.com.tn/sites/default/files/societes/ab/etats-financiers/ab-etats-financiers-annuels-31-12-2017.pdf', 'Null', 'Null', 'http://www.bvmt.com.tn/sites/default/files/societes/adwya/etats-financiers/adwya-etats-financiers-annuels-individuels-31-12-2017.pdf']
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement