8 Replies - 731 Views - Last Post: 04 February 2020 - 02:11 PM Rate Topic: -----

#1 2Dabz   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 100
  • Joined: 10-September 17

Replacing values in two different size list

Posted 31 January 2020 - 11:13 AM

I have the list f3 = ['a', 'a', 'b', 'b', 'c', 'c', 'a', 'b', 'b']

I put the list in order of frequency f1 = ['b', 'a', 'c']

Now f1 converts to f2 = ['e', 't', 'a']

So now I want to take the converted values from f2 and swap them back on f3 and wind up with

f4 = ['t', 't', 'e', 'e', 'a', 'a', 't', 'e', 'e']

I can't figure out how to produce f4. I've tried if and for statements but can't come up with anything

that works. Any thoughts? Thanks.

Is This A Good Question/Topic? 1
  • +

Replies To: Replacing values in two different size list

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15503
  • View blog
  • Posts: 62,075
  • Joined: 12-June 08

Re: Replacing values in two different size list

Posted 31 January 2020 - 01:22 PM

If I am following, you could just a 'replace' to search for some index-variable tracked value in f1 with the same index value in f2.
Was This Post Helpful? 1
  • +
  • -

#3 DK3250   User is offline

  • Pythonian
  • member icon

Reputation: 566
  • View blog
  • Posts: 1,804
  • Joined: 27-December 13

Re: Replacing values in two different size list

Posted 31 January 2020 - 02:16 PM

Let me hint you:
You can use two nested loops, each using the enumerate function.
My working code is only 4 lines, so difficult to hint more without actually disclose finished code.

This post has been edited by DK3250: 31 January 2020 - 02:18 PM

Was This Post Helpful? 1
  • +
  • -

#4 2Dabz   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 100
  • Joined: 10-September 17

Re: Replacing values in two different size list

Posted 31 January 2020 - 09:00 PM

I've been playing around with it but no luck yet.
Was This Post Helpful? 0
  • +
  • -

#5 DK3250   User is offline

  • Pythonian
  • member icon

Reputation: 566
  • View blog
  • Posts: 1,804
  • Joined: 27-December 13

Re: Replacing values in two different size list

Posted 01 February 2020 - 01:18 AM

ok, it's such a small code, let's go through it.
First using only lists:
f3 = ['a', 'a', 'b', 'b', 'c', 'c', 'a', 'b', 'b']
f1 = ['b', 'a', 'c']
f2 = ['e', 't', 'a']


f4 = []  # make an empty list
for item in f3:  # iterate through f3
    for n1, letter in enumerate(f1):  # iterate through f1 - and get index number
        if item == letter:
            f4.append(f2[n1])  # add to the result list using the f1-index in combination with f2
print(f4)


Next, using a dictionary - this is more efficient and probably also more pythonic:
f3 = ['a', 'a', 'b', 'b', 'c', 'c', 'a', 'b', 'b']
f1 = ['b', 'a', 'c']
f2 = ['e', 't', 'a']

d = dict(zip(f1, f2))  # make a dict linking f1 and f2
f4 = []  # make an empty list
for item in f3:  # iterate through f3
    f4.append(d[item])  # add to result list using the dict
print(f4)


I hope you get it - feel free to ask again.
Was This Post Helpful? 2
  • +
  • -

#6 2Dabz   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 100
  • Joined: 10-September 17

Re: Replacing values in two different size list

Posted 02 February 2020 - 11:26 AM

Your code works. Thanks. I am still having a problem implementing in on mine. I get a KeyError: 'b' from the line f4.append(d[items]).

Here is how I am generating the list that contains 'b' in f1.

f1 = [key for key, _ in frequency.most_common()]



This gives me the fist f1 = ['b', 'a', 'c'].

I'm thinking this isn't making a normal list?
Was This Post Helpful? 0
  • +
  • -

#7 2Dabz   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 100
  • Joined: 10-September 17

Re: Replacing values in two different size list

Posted 02 February 2020 - 11:51 AM

Figured it out. When I was creating f2 I was doing something that forced the KeyError. Switched up the way the list is generated and it works now. Thanks again.
Was This Post Helpful? 0
  • +
  • -

#8 DK3250   User is offline

  • Pythonian
  • member icon

Reputation: 566
  • View blog
  • Posts: 1,804
  • Joined: 27-December 13

Re: Replacing values in two different size list

Posted 02 February 2020 - 01:22 PM

Glad I could help.
But assistance without conveying new understanding is really not helpful.
So, just for my peace of mind: Are you sure you understand the solution in detail?
Was This Post Helpful? 1
  • +
  • -

#9 2Dabz   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 100
  • Joined: 10-September 17

Re: Replacing values in two different size list

Posted 04 February 2020 - 02:11 PM

Yes. Your documentation is good. I knew what I wanted to do just not the code to get there. The zip and dict functions made it really easy. Link the values in two list. Then run through original list and replace matching value with the linked value.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1