[Solved] User input integer list [duplicate]

Your best way of doing this, is probably going to be a list comprehension. user_input = raw_input(“Please enter five numbers separated by a single space only: “) input_numbers = [int(i) for i in user_input.split(‘ ‘) if i.isdigit()] This will split the user’s input at the spaces, and create an integer list. 1 solved User input … Read more

[Solved] Function to switch between two frames in tkinter

You shouldn’t put any logic in a lambda. Just create a normal function that has any logic you want, and call it from the button. It’s really no more complicated that that. class SomePage(…): def __init__(…): … button1 = tk.Button(self, text=”Back to Home”, command=lambda: self.maybe_switch_page(StartPage)) … def maybe_switch_page(self, destination_page): if …: self.controller.show_frame(destination_page) else: … If … Read more

[Solved] How do I optimize Python Code?

data = [[‘From’, ‘[email protected]’, ‘Fri’, ‘Jan’, ’14’, ’22:16:24′, ‘2012’], [‘From’, ‘[email protected]’, ‘Fri’, ‘Jan’, ’14’, ’23:16:24′, ‘2012’], [‘From’, ‘[email protected]’, ‘Fri’, ‘Jan’, ’14’, ’21:16:24′, ‘2012’], [‘From’, ‘[email protected]’, ‘Fri’, ‘Jan’, ’14’, ’22:02:24′, ‘2012’] ] hour_frequency_list = {} for temp in data: hour = temp[5].split(“:”)[0] if hour in hour_frequency_list: hour_frequency_list[hour] += 1 else: hour_frequency_list[hour] = 1 sorted_list = sorted(hour_frequency_list.items()) … Read more

[Solved] Interacting with a website and getting data using python

You can use BeautifulSoup, i.e.: import requests, traceback from bs4 import BeautifulSoup domains = [“duckduckgo.com”, “opensource.com”] for dom in domains: try: req = requests.get(f”https://fortiguard.com/webfilter?q={dom}&version=8″) if req.status_code == 200: soup = BeautifulSoup(req.text, ‘html.parser’) cat = soup.find(“meta”, property=”description”)[“content”].split(“:”)[1].strip() print(dom, cat) except: pass print(traceback.format_exc()) Output: duckduckgo.com Search Engines and Portals opensource.com Information Technology Demo 2 solved Interacting with … Read more

[Solved] Do string representations of dictionaries have order in Python 3.4?

Note: Python 3.6 introduces a new, order-preserving implementation of dict, which makes the following obsolete from 3.6 onwards. Here are three iterations of your example in three different Python 3.4 interpreter sessions: Python 3.4.1 (default, Aug 8 2014, 15:05:42) [GCC 4.8.2] on linux2 Type “help”, “copyright”, “credits” or “license” for more information. >>> d={} >>> … Read more

[Solved] Python – Division by zero

There are 3 ways to do this, the normal way I do it is like this. Note this will also output 5 if n is negative. I typically use this when averaging collections that might be empty, since negative lengths are impossible. result = 5/max(1, n) Will output 5 if n is 0 or negative. … Read more

[Solved] Finding and counting the frequency of known pairs of words in multiple files [closed]

When using combinations() you are getting all pairs, even the non-adjacent ones. You can create a function that will return the adjacent pairs. I’ve tried the following code and it worked, maybe it can give you some insight: import os import re from collections import Counter def pairs(text): ans = re.findall(r'[A-Za-z]+’, text) return (tuple(ans[i:i+2]) for … Read more

[Solved] Initialize variables when they’re not already declared in python

One solution is to use a defaultdict: from collections import defaultdict my_dict = defaultdict(lambda: []) my_dict[‘var1’].append(1) print(my_dict[‘var1’]) # prints ‘[1]’ This would not allow you to simply do print(var1), however, because it would still be undefined in your local (or global) namespace as a tagged value. It would only exist in the defaultdict instance as … Read more