[Solved] main program while loop

No, you do not need a main loop if you use the cmd.Cmd class included with Python: #! /usr/bin/env python3 import cmd class App(cmd.Cmd): prompt=”Type in a command: ” def do_a(self, arg): print(‘I am doing whatever action “a” is.’) def do_b(self, arg): print(‘I am doing whatever action “b” is.’) if __name__ == ‘__main__’: App().cmdloop() The … Read more

[Solved] How to store missing date(15 min interval) points from csv into new file (15 minutes interval) -python 3.5

try this: In [16]: df.ix[df.groupby(df[‘datetime’].dt.date)[‘production’].transform(‘nunique’) < 44 * 4 * 24, ‘datetime’].dt.date.unique() Out[16]: array([datetime.date(2015, 12, 7)], dtype=object) this will give you all rows for the “problematic” days: df[df.groupby(df[‘datetime’].dt.date)[‘production’].transform(‘nunique’) < 44 * 4 * 24] PS there is a good reason why people asked you for a good reproducible sample data sets – with the one … Read more

[Solved] write python code in single line

Since pythons list comprehensions are turing complete and require no line breaks, any program can be written as a python oneliner. If you enforce arbitrary restrictions (like “order of the statements” – what does that even mean? Execution order? First apperarance in sourcecode?), then the answer is: you can eliminate some linebreaks, but not all. … Read more

[Solved] Create email with the firsts letters of the name [closed]

Here’s some “python magic” name = [‘Elon Reeve Musk’] f”{”.join(filter(str.isupper, name[0]))}@company.com”.lower() >>> [email protected] Whether this is better than your method is debatable. Most of the time a few lines of easy to read code is better than a one line hack. My recommendation would be name = [‘Elon Reeve Musk’] initials=””.join(word[0] for word in name[0].split()) … Read more

[Solved] find number of 1 and 0 combinations in two columns

Assuming you have a pandas dataframe, one option is to use pandas.crosstab to return another dataframe: import pandas as pd df = pd.read_csv(‘file.csv’) res = pd.crosstab(df[‘X’], df[‘Y’]) print(res) Y 0 1 X 0 3 7 1 1 3 A collections.Counter solution is also possible if a dictionary result is required: res = Counter(zip(df[‘X’].values, df[‘Y’].values)) 4 … Read more

[Solved] Remove some duplicates from list in python

this is where itertools.groupby may come in handy: from itertools import groupby a = [“a”, “a”, “b”, “b”, “a”, “a”, “c”, “c”] res = [key for key, _group in groupby(a)] print(res) # [‘a’, ‘b’, ‘a’, ‘c’] this is a version where you could ‘scale’ down the unique keys (but are guaranteed to have at leas … Read more

[Solved] Can’t deal with some complicated laid-out content from a webpage

You can take advantage of CSS selector span[id$=lblResultsRaceName], which finds all spans that’s id ends with lblResultsRaceName and ‘td > span’, which finds all spans that have direct parent <td>: This code snippet will go through all racing result and prints all races: import requests from bs4 import BeautifulSoup url = “https://www.thedogs.com.au/Racing/Results.aspx?SearchDate=3-Jun-2018” def get_info(session,link): session.headers[‘User-Agent’] … Read more