[Solved] How to replace 4 backslashes

[ad_1] You don’t need a regex, look for the apostrophe following the backslashes and replace the whole pattern with just an apostrophe: In [1]: s = “It was quick and great ! And it\\\\’s customized” In [2]: s.replace(r”\\'”, “‘”) Out[2]: “It was quick and great ! And it’s customized” Based on your repr output you … Read more

[Solved] PYTHON codes with a lesson from a book [closed]

[ad_1] Briefly: cities is instantiated as a dictionary, and some key/value are inserted here. Both key and values are string for CA -> San Francisco, MI -> Detroit, etc. etc. a function named find_city is defined, it takes two input parameters (themap and state); to the cities dictionary is added another key/value, where key is … Read more

[Solved] Printing list shows single quote mark

[ad_1] Here’s one utility function that worked for me: def printList(L): # print list of objects using string representation if (len(L) == 0): print “[]” return s = “[” for i in range (len(L)-1): s += str(L[i]) + “, ” s += str(L[i+1]) + “]” print s This works ok for list of any object, … Read more

[Solved] Python sort multi dimensional dict

[ad_1] First, you should avoid using reserved words (such as input) as variables (now input is redefined and no longer calls the function input()). Also, a dictionary cannot be sorted. If you don’t need the keys, you can transform the dictionary into a list, and then sort it. The code would be like this: input_dict … Read more

[Solved] Need to change letters to other letters (Python 2.7)

[ad_1] something like this? orig = ‘hello’ # table = {‘h’: ‘L’, ‘e’: ‘O’, ‘l’: ‘A’, ‘o’: ‘W’ } # table_tr = dict( (ord(a), ord(b)) for a,b in table.items() ) table_tr = str.maketrans(‘helo’, ‘LOAW’) res = orig.translate(table_tr) print(res) (this is for python3; for python2 you need to import string and use string.maketrans) [ad_2] solved Need … Read more

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

[ad_1] 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 … Read more