[Solved] How to replace 4 backslashes

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 don’t … Read more

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

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 the … Read more

[Solved] python name error: NameError: name ‘Jewels’ is not defined

You can’t fill a list in this way. Because you start with an empty list, Jewels = [], your attempt to assign to Jewels[0] will result in an IndexError. You can instead use the list.append method jewels = [] for i in range(total_jewels): jewels.append(raw_input(‘Please Enter approx price for Jewel #{}: ‘.format(i))) or a list comprehension … Read more

[Solved] Printing list shows single quote mark

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, for … Read more

[Solved] Python sort multi dimensional dict

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)

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) solved Need to change … Read more

[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