Python Forum
Combining Lambda's not working
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Combining Lambda's not working
#1
Hello everyone, Need help with this issue. What I am seeking to do is three things.

1. convert strings that start and end with numbers to num2words
2. convert only if ends with number to num2words
3. All other numbers convert to num2words.

However the first one is currently stuck on the first item. I am trying to combine lambda instead of using multiple if statements. The following is what I have so far.

for tx in txt:
    md=re.search('(?<=[0-9].  )(.*)(?=.txt)',tx) #Filter between the numbers and the extension txt
    #print('MD',md[0])
    
    with open(tx, 'r+', encoding="cp1251", errors='ignore') as f: #Open text file
        Lines = f.readlines()
        text = f.read() #Read Text File
        for line in Lines:
            mda=re.search(md[0],line)
            
            if re.search(md[0],line):
                cap=re.search(md[0],line)
                chp=cap[0].strip()
                
  
                if chp.startswith(('1','2','3','4','5')) and  chp.endswith(('1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20','21','22','23','24','25','26',
                                                                            '27','28','29','30','31','32','33','34','35','36','37','38','39','40','41','42','43','44','45','46','47','48','49','50','51',
                                                                            '52','53','54','55','56','57','58','59','60','61','62','63','64','65','66','67','68','69','70','71','72','73','74','75','76',
                                                                            '77','78','79','80','81','82','83','84','85','86','87','88','89','90','91','92','93','94','95','96','97','98','99','100','101',
                                                                            '102','103','104','105','106','107','108','109','110','111','112','113','114','115','116','117','118','119','120','121','122','123',
                                                                            '124','125','126','127','128','129','130','131','132','133','134','135','136','137','138','139','140','141','142','143','144','145',
                                                                            '146','147','148','149','150')): #Check if starts and ends with numbers
                    
                    ordr= re.sub(r"^[^\d]*(\d+)",  lambda x:  num2words(int(x.group(1)),to='ordinal') +' ', line) #Convert to ordinal
                    chpt2= re.sub(r"(\d+)(?!.*\d)",  lambda a: ' Chapter ' + num2words(int(a.group(0))) +' ', line) #Convert and add Chapter

                    comblam = lambda q: ordr(q) + chpt2(q) #Combine lambda
                    print('Lets see ',comblam(2)) #Print Results
However when ran I get error message of

    comlam = lambda q: ordr(q) + chpt2(q) #Combine lambda
TypeError: 'str' object is not callable


How can I solve this issue? Thanks
Reply
#2
You are not combining lambda expressions. Neither ordr or chpt2 are lambda expressions or functions. ordr is whatever was returned by:
 ordr= re.sub(r"^[^\d]*(\d+)",  lambda x:  num2words(int(x.group(1)),to='ordinal') +' ', line)
Which is going to be a list object, so ordr(q) is treating a list like it is a function and trying to call it.

Did you mean to do this?
comblam = lambda q: ordr[q] + chpt2[q]
If so, your topic suddenly makes more sense. A lambda expression that combines things.
Reply
#3
(Apr-01-2022, 05:21 PM)deanhystad Wrote: You are not combining lambda expressions. Neither ordr or chpt2 are lambda expressions or functions. ordr is whatever was returned by:
 ordr= re.sub(r"^[^\d]*(\d+)",  lambda x:  num2words(int(x.group(1)),to='ordinal') +' ', line)
Which is going to be a list object, so ordr(q) is treating a list like it is a function and trying to call it.

Did you mean to do this?
comblam = lambda q: ordr[q] + chpt2[q]
If so, your topic suddenly makes more sense. A lambda expression that combines things.

Ahh oh ok got it. Thanks for the info. Sorry about the late reply. Just had a chance to get back to it. How might I go about updating as list one has one entry and list two has another. How can I go about synching both list 1 and 2 so both entries shown on the same line? Thanks
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020