Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Using tuples or Re to replace 5 strings with one
#1
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)
Larz60+ wrote Aug-23-2017, 11:37 AM:
I added code tags for you this time. Please use in future posts.
see BBCODE
Quote
#2
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']
Quote
#3
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"
Quote
#4
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
Quote
#5
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.
Quote
#6
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]))
nilamo likes this post
Quote
#7
Thanks
nilamo likes this post
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Finding multiple strings between the two same strings Slither 1 263 Jun-05-2019, 09:02 PM
Last Post: Yoriz
  Search & Replace - Newlines Added After Replace dj99 3 647 Jul-22-2018, 01:42 PM
Last Post: buran
  lists, strings, and byte strings Skaperen 2 994 Mar-02-2018, 02:12 AM
Last Post: Skaperen
  search and replace first amount of strings instances with one thing and a second amou chickflick91 7 1,981 Sep-26-2017, 05:13 PM
Last Post: chickflick91
  replace specific words in strings Sama 3 1,068 Jul-23-2017, 01:51 PM
Last Post: ichabod801

Forum Jump:


Users browsing this thread: 1 Guest(s)