[Solved] Swap lines in a script by iteration over lines [closed]
Using perl from command line, perl -ne ‘push @r, $_; print(@r[2,1,0,3]), @r=() if @r==4 or eof’ file solved Swap lines in a script by iteration over lines [closed]
Using perl from command line, perl -ne ‘push @r, $_; print(@r[2,1,0,3]), @r=() if @r==4 or eof’ file solved Swap lines in a script by iteration over lines [closed]
You can use itertools.product for this. It returns a generator of sequences of fixed length. As you order your strings first by length and then lexicographically, you may use something like this import itertools for l in range(1, 5): for seq in itertools.product(“abc”, repeat=l): print(“”.join(seq)) 3 solved Find All Possible Fixed Size String Python
Don’t use a new OneHotEncoder on test_data, use the first one, and only use transform() on it. Do this: test_data = onehotencoder_1.transform(test_data).toarray() Never use fit() (or fit_transform()) on testing data. The different number of columns are entirely possible because it may happen that test data dont contain some categories which are present in train data. … Read more
def days_in_month(month): for m, nblist in month_days: if month==m: return nblist else: return [] days_in_month(‘May’) Out[20]: [31] days_in_month(‘Mady’) Out[21]: [] solved Using the List Feature in Python
Try to loop until your check evaluates True: import time interval = 0.2 # nr of seconds while True: stop_looping = myAlertCheck() if stop_looping: break time.sleep(interval) The sleep gives you CPU time for other tasks. EDIT Ok, I’m not sure what exactly your question is. First I thought you wanted to know how to let … Read more
This would be a complete implementation in Haskell import Data.List.Split (chunksOf) import Data.List import Data.Char (digitToInt) main :: IO () main = do digits <- map digitToInt <$> readFile “pifile.txt” let summator = foldl1′ (zipWith (+)) . chunksOf 4 print $ summator digits I will update this with some explanation later this day. Update @Comments … Read more
Use dateutil.parser.parse() parse the date string into a timezone aware datetime object, then use strftime() to get the format you want. For better explanation goto solved How to convert datetime string to another format [duplicate]
if you want to write it in a sorted manner you can do this. from collections import Counter wordlist = open(‘so.py’, ‘r’).read().split() word_counts = Counter(wordlist) write_file = open(‘wordfreq.txt’, ‘w’) for w, c in sorted(word_counts.iteritems(), key=lambda x: x[1], reverse=True): write_file.write(‘{w}, {c}\n’.format(w=w, c=c)) solved Words, sorted by frequency, in a book (.txt file)
Python: print (“{0:5s} {1:7s} {2:9s} {3:6s} {4:25s} {5:s}”.format(‘Rank’, ‘Points’, ‘Comments’, ‘Hours’, ‘Sub’, ‘Link’)) Ruby: puts “%-5s %-7s %-9s %-6s %-25s %-5s” % [‘Rank’, ‘Points’, ‘Comments’, ‘Hours’, ‘Sub’, ‘Link’] Alternatively: puts sprintf(“%-5s %-7s %-9s %-6s %-25s %-5s”, *[‘Rank’, ‘Points’, ‘Comments’, ‘Hours’, ‘Sub’, ‘Link’]) 1 solved What would the ruby equivalent be to this python script?
def count(word, letter): count = 0 for l in word: if l == letter: count = count + 1 return count word = raw_input(‘Enter a string:’) letter = raw_input(‘Enter a character:’) print count(word, letter) solved function with arguments in python [closed]
That’ll do the trick import re s=””text” “some”” res = re.subn(‘”([^”]*)”‘, ‘<em>\\1</em>’, s)[0] solved grep with python to match string inside quotes in html files [closed]
This is less of a Python question and more of an HTML question. You’ll still need to write most of the page in HTML and then only ‘deliver’ the page to the user using Python, with Bottlepy, as you’ve mentioned . You’ll need to learn about HTML Forms and Inputs to get anything done effectively. … Read more
There is an incredible scraping library for Python called BeautifulSoup which will make your life much easier: http://www.crummy.com/software/BeautifulSoup/ BeautifulSoup allows you to select by html tags and/or html attributes such via a css class name. It also handles bad html docs really well but you need to read the docs on how it works. It’s … Read more
You have an extra line of spaces on the top Try this- import math import os import random import re import sys # Complete the staircase function below. def staircase(n): for x in range(1,n+1): print((n-x)*” “+”#”*x) if __name__ == ‘__main__’: n = int(input()) staircase(n) solved Any idea why hackerrank doesn’t accept my code even if … Read more
This should help you: lst = [‘apple’,’banana’,’cherry’] strings = input().split(‘,’) for x in strings: lst.remove(x) print(lst) Output: >>> apple,banana [‘cherry’] solved How to remove multiple input items from a list [closed]