Using tuples or Re to replace 5 strings with one - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Using tuples or Re to replace 5 strings with one (/thread-4527.html) |
Using tuples or Re to replace 5 strings with one - sem - Aug-23-2017 Hi, I am trying to simplify a list of fruits in one file, by generalizing them to "fruit". However, I can only using regular tuples and re, replace one by one. Here I would need to replace 4 different words with one, as the example shows below. But it doesn't work. Any ideas? Thanks fruit = ("Apple", "Orange", "Banana", "Mandarin") fruit_trans = {} fruit_trans = [("fruit")] trans = string.maketrans("Apple", "Orange", "Banana", "Mandarin") my_string = my_string.translate(trans) RE: Using tuples or Re to replace 5 strings with one - Larz60+ - Aug-23-2017 The best way to save lists and/or dictionaries is as a json file. in your code, this would be done as follows: import json fruit = ["Apple", "Orange", "Banana", "Mandarin"] # this can be accessed by index: print('fruit[0]: {}, fruit[2]: {}'.format(fruit[0], fruit[2])) print('fruit: {}'.format(fruit)) # To save to file named fruit.dat: with open('fruit.dat', 'w') as fout: json.dump(fruit, fout) # To load back in: with open('fruit.dat') as fin: newfruit = json.load(fin) print('newfruit: {}'.format(newfruit))output:
RE: Using tuples or Re to replace 5 strings with one - sem - Aug-23-2017 Thanks, but I think this is rather different than what I imagined. The script searches for strings in a file, what would be good to have here is that if the script finds ONLY the combination of "apples" "orange" and "banana" it will automatically change all the three strings in the file into "fruit" RE: Using tuples or Re to replace 5 strings with one - Larz60+ - Aug-23-2017 Sorry, yu said you needed tuples: slight modification: import json fruit = ("Apple", "Orange", "Banana", "Mandarin") fruitl = list(fruit) # this can be accessed by index: print('fruitl[0]: {}, fruitl[2]: {}'.format(fruitl[0], fruitl[2])) print('fruitl: {}'.format(fruitl)) # To save to file named fruit.dat: with open('fruit.dat', 'w') as fout: json.dump(fruitl, fout) # To load back in: with open('fruit.dat') as fin: newfruit = json.load(fin) print('newfruit: {}'.format(newfruit))could you give an example (what the results would actually look line) of inputs and outputs RE: Using tuples or Re to replace 5 strings with one - sem - Aug-23-2017 Yes, tuples would be great. I am trying this now (Aug-23-2017, 11:55 AM)Larz60+ Wrote: Sorry, yu said you needed tuples: Thanks Larz. This is what I did : Rock = ['Hematite', 'Bentonite', 'Phylite', 'Magnetite'] Rock1 = list(Rock) print('Rock1[0]: {}, ROck1[4]: {}'.format(Rock1[0], Rock1[2], Rock1[3], Rock1[4])) print('Rock1: {}'.format(Rock1)) and I thought that all files that are scanned by this part of the script are subjected to a complete removal of the four words listed in Rock, and replaced by "Rock". However I get IndexError: list index out of range (Aug-23-2017, 11:55 AM)Larz60+ Wrote: Sorry, yu said you needed tuples: PS: Used new_trans = {} new_trans[("bentonite", "magnetite")] = "Brittle_Rock" mc = [] however, it words only for TWO words. I would like to do the same for 5 words. RE: Using tuples or Re to replace 5 strings with one - Larz60+ - Aug-23-2017 each item that comes after the format statement must have a corresponding '{}' in the print body. thus, your statement: print('Rock1[0]: {}, ROck1[4]: {}'.format(Rock1[0], Rock1[2], Rock1[3], Rock1[4]))there is no slot for Rock[3] and Rock[4] doesn't exist! Remember this is zero based, so indexes are 0,1,2,3 You didn't use Rock[1] which is the second item in the list See what I'm talking about? it would have to be something like: print('Rock1: {}, Rock2: {} Rock3: {} Rock4: {} '.format(Rock[0], Rock[1], Rock[2], Rock1[3])) RE: Using tuples or Re to replace 5 strings with one - sem - Aug-24-2017 Thanks |