[Solved] Count total lines with open in Python

You are increasing your total line count incrementally with each processed line. Read in the full file into a list, then process the list. While processing the list, increase a counter thats “the current lines nr” and pass it to the output function as well. The len() of your list is the total line count … Read more

[Solved] Extract text with conditions in Python

Try with this: \d+\s*((?:Apple|Banana|Orange|Pineapple)s?\b[\s\S]*?)(?=$|\d+\s*(?:Apple|Banana|Orange|Pineapple)s?\b) See: Regex demo The code: import re regex = r”\d+\s*((?:Apple|Banana|Orange|Pineapple)s?\b[\s\S]*?)(?=$|\d+\s*(?:Apple|Banana|Orange|Pineapple)s?\b)” test_str = “I have 2 apples in my bag and apples are great food toeat. you shud eat apples daily. it is very good for health. 3 bananas are also good. it reduces fat.” matches = re.findall(regex, test_str, re.MULTILINE | re.IGNORECASE) … Read more

[Solved] Coding a function in python wihch count the number of occurances [closed]

Use the collections module to use the Counter function. Such as: import collections def carCompt(*args): return collections.Counter(“”.join(map(str.upper, args))) This will return {‘A’:5, ‘D’:1, ‘E’:3, ‘H’:2, ‘L’:1, ‘N’:1, ‘O’:1, ‘P’:2, ‘R’:2, ‘S’:1, ‘X’:1} If you want it to be case sensitive then leave it like: import collections def carCompt(*args): return collections.Counter(“”.join(args)) which will return {‘a’: 4, … Read more

[Solved] merge list of nested list to a single list which has lakhs of data python [duplicate]

from itertools import chain l = [[‘password’, ‘_rev’, ‘_id’, ‘username’], [‘password’, ‘_rev’, ‘_id’, ‘username’, ‘name’], [‘password’, ‘_rev’, ‘_id’, ‘username’],[‘password’, ‘_rev’, ‘_id’, ‘username’,’country’]] list(set(chain(*l))) Output – [‘username’, ‘_rev’, ‘_id’, ‘name’, ‘password’, ‘country’] solved merge list of nested list to a single list which has lakhs of data python [duplicate]