[Solved] How to search for each keyword from file1 in file2 and return the number of times it was found? [closed]

Well this is simple. Ill only do it for the First File, but to show how many times it comes up, you can count, but this is a one word per line test. import os file1 = open(“file1.txt”, “r”) # Make sure you added the keywords in beforehand file2 = open(“file2.txt”, “r”) #These will open … Read more

[Solved] how to check numbers in a list in python

Here is a cleaned-up version: from itertools import cycle BASE = ord(‘a’) – 1 def str_to_nums(s): “”” Convert a lowercase string to a list of integers in [1..26] “”” return [ord(ch) – BASE for ch in s] def nums_to_str(nums): “”” Convert a list of integers in [1..26] back to a lowercase string “”” return “”.join(chr(n … Read more

[Solved] Defining function with an argument, employs while loop and returns largest power of 2 that is less than or equal to number [closed]

A simple while loop works, just make sure you divide the number by 2, otherwise you’ll get the NEXT power of 2. def function(number): x = 1 while x <= number: x *= 2 return x / 2 solved Defining function with an argument, employs while loop and returns largest power of 2 that is … Read more

[Solved] I would like to total the amount of correct answers and display it at the end getting error local variable referenced

You can make count global: count = 0 def intro(start): if start == “yes” or start == “y”: print(“Lets begin.”) else: print(“Thanks for checking it out! Bye Bye!”) def question1(): global count count += 1 return count def question2(): global count count += 1 return count def main(): print(“Hello There! Welcome to an all new … Read more

[Solved] Python script to ping linux server

You can use urllib2 and socket module for the task. import socket from urllib2 import urlopen, URLError, HTTPError socket.setdefaulttimeout( 23 ) # timeout in seconds url=”http://google.com/” try : response = urlopen( url ) except HTTPError, e: print ‘The server couldn\’t fulfill the request. Reason:’, str(e.code) except URLError, e: print ‘We failed to reach a server. … Read more

[Solved] What does the get method do on dictionaries? [closed]

If the key argument is in switcher, the .get() method returns the value for the key. If the key is not in the dictionary, the method returns the optional “nothing”. def numbers_to_strings(argument): switcher = {0: “zero”, 1: “one”, 2: “two”} return switcher.get(argument, “nothing”) Calling the above function with a key that is in the dictionary: … Read more

[Solved] putting float numbers in lists [closed]

Just create lists: somelist = [p1, p2] You probably want to append those to another data structure initialized outside of your loop: somelargerlist = [] for presumed_loop_variable in presumed_loop_not_shown: p1 = … p2 = … somelist = [p1, p2] somelargerlist.append(somelist) 17 solved putting float numbers in lists [closed]

[Solved] How i can convert ISO time format to yyyy-mm-dd hh:mm:ss using python [closed]

Try this, >>> import datetime >>> datetime.datetime.now().strftime(‘%d-%m-%Y %H:%M:%S’) ’03-08-2019 12:43:16′ If you max_startdate is string, >>> max_startdate=”2019-08-03 01:08:58.155000″ >>> max_startdate.split(‘.’)[0] ‘2019-08-03 01:08:58’ 3 solved How i can convert ISO time format to yyyy-mm-dd hh:mm:ss using python [closed]

[Solved] Why are there so many formatting flavours in Python?

This is what evolution looks like. “We need string concatenation”. Sure. “hello” + ”’ ”’ + ‘world’ or, if you like, ‘ ‘.join([‘hello’, ‘world’]) “But “found ” + str(var) + ” matches” is kind of clumsy. I hear Lisp has princ…?” Oh okay, here: ‘found %i matches’ % var (… Time passes… ) “I hear … Read more

[Solved] evaluate a python string expression using dictionary values

At the end I found a solution to my problem: text = “‘my_home1’ in houses.split(‘,’) and ‘2018’ in iphone.split(‘,’) and 14 < mask” dict = {‘house’ : ‘my_home1,my_home2’, ‘iphone’ : ‘2015,2018’, ‘mask’ : ’15’ } expression = ‘false’ for key in dict.keys(): if isinstance(dict.get(key), str): text = re.sub(key, ‘\'{}\”.format(dict.get(key)), text) else: text = re.sub(key, dict.get(key), … Read more