[Solved] how to find the words that are likely to follow the single word in large string in python .?

You can use a regex to look for words that follow the word python, for example >>> import re >>> re.findall(r’Python (\w+)’, s) [‘is’, ‘has’, ‘features’, ‘interpreters’, ‘code’, ‘is’, ‘Software’] Since this list may contain duplicates, you could create a set if you want a collection of unique words >>> set(re.findall(r’Python (\w+)’, s)) {‘Software’, ‘is’, … Read more

[Solved] How to merge two list of list in python

You may achieve this using itertool.chain() with list comprehension expression as: >>> a=[[1,2,3],[4,5,6]] >>> b=[[5,8,9],[2,7,10]] # v join the matched sub-lists # v v your condition >>> [i + list(chain(*[j[1:] for j in b if i[1]==j[0]])) for i in a] [[1, 2, 3, 7, 10], [4, 5, 6, 8, 9]] solved How to merge two … Read more

[Solved] Got stuck in python code which made me confused

How about something like this? import re class context: grammar = r’and|or|#|\$|:|@|\w+’ subs = [ (r’\$(\w+)’, “context(‘\\1′)”), (r’!#(\w+)’, “intents(‘\\1′).isNot_()”), (r’#(\w+)’, “intents(‘\\1′).is_()”), (r’@(\w+):(\w+)’, “entities(‘\\1’).is_(‘\\2′)”), (r’@(\w+)’, “entities(‘\\1’).any()”) ] def __init__(self, val): self.val = val def parse(self): parsed = self.val for sub in self.subs: parsed = re.sub(*sub, parsed) return parsed >>> print(context(‘$foo\n#foo\n!#foo\n@foo\n@foo:bar’).parse()) context(‘foo’) intents(‘foo’).is_() intents(‘foo’).isNot_() entities(‘foo’).any() entities(‘foo’).is_(‘bar’) 1 … Read more

[Solved] Compute the ratio of the sum of cube of the first ‘n’ natural numbers to the sum of square of first ‘n’ natural numbers

from functools import reduce import ast,sys input_int = int(sys.stdin.read()) num_list = [x for x in range(1, input_int+1)] print(reduce(lambda x, y : x + y ** 3, num_list) / reduce(lambda x, y : x + y ** ,num_list)) 1 solved Compute the ratio of the sum of cube of the first ‘n’ natural numbers to the … Read more

[Solved] How to filter Django queryset by non field values

I was able to solve the problem by pre processing the data to store the person’s timezone. Then using pytz I do this. from django.utils import timezone import pytz valid_timezones = [] for tz in list_of_timezones: local_time = now().astimezone(pytz.timezone(tz)) if 19 < local_time.hour < 20: valid_timezones.append(tz) reminders = Person.objects.filter(timezone__in=valid_timezones) solved How to filter Django queryset … Read more

[Solved] I’m not getting the else output

input returns a string, and 0 != “0” Either parse your strings immediately parx = int(input(“Write your parX: “)) pary = int(input(“Write your parY: “)) Or check against strings while pary != “0” and parx != “0”: Note too that you should be using some error checking. If the user enters a non-number, your program … Read more

[Solved] Python How to get the last word from a sentence in python? [closed]

You have to pass “/” as a separator parameter to the split function: split(“/”). The split function by default splits by whitespace ” “. “Hello World”.split() => [“Hello”, “World”] “Hello/World”.split() => [“Hello/World”] “Hello/World”.split(“/”) => [“Hello”, “World”] Change your code to: print(sentence.split(“/”)[-1]) 3 solved Python How to get the last word from a sentence in python? … Read more