Python Forum

Full Version: Using tuples or Re to replace 5 strings with one
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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)
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:
Output:
fruit[0]: Apple, fruit[2]: Banana fruit: ['Apple', 'Orange', 'Banana', 'Mandarin'] newfruit: ['Apple', 'Orange', 'Banana', 'Mandarin']
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"
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
Yes, tuples would be great. I am trying this now

(Aug-23-2017, 11:55 AM)Larz60+ Wrote: [ -> ]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


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:
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


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.
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]))
Thanks