Carol M
Carol M

Reputation: 131

Replace values in Python dict

I have 2 files, The first only has 2 columns

A   2
B   5
C   6

And the second has the letters as a first column.

A  cat
B  dog
C  house

I want to replace the letters in the second file with the numbers that correspond to them in the first file so I would get.

2  cat
5  dog
6  house

I created a dict from the first and read the second. I tried a few things but none worked. I can't seem to replace the values.

import csv
with open('filea.txt','rU') as f:
    reader = csv.reader(f, delimiter="\t")
    for i in reader:
        print i[0]  #reads only first column
        a_data = (i[0])


dictList = []
with open('file2.txt', 'r') as d:
        for line in d:
            elements = line.rstrip().split("\t")[0:]
            dictList.append(dict(zip(elements[::1], elements[0::1])))

for key, value in dictList.items():
            if value == "A":
                    dictList[key] = "cat"

Upvotes: 3

Views: 13185

Answers (4)

Alexander
Alexander

Reputation: 109526

The issue appears to be on your last lines:

for key, value in dictList.items():
    if value == "A":
        dictList[key] = "cat"

This should be:

for key, value in dictList.items():
    if key in a_data:
        dictList[a_data[key]] = dictList[key]
        del dictList[key]

d1 = {'A': 2, 'B': 5, 'C': 6}
d2 = {'A': 'cat', 'B': 'dog', 'C': 'house', 'D': 'car'}

for key, value in d2.items():
    if key in d1:
        d2[d1[key]] = d2[key]
        del d2[key]

>>> d2
{2: 'cat', 5: 'dog', 6: 'house', 'D': 'car'}

Notice that this method allows for items in the second dictionary which don't have a key from the first dictionary.

Wrapped up in a conditional dictionary comprehension format:

>>> {d1[k] if  k in d1 else k: d2[k] for k in d2}
{2: 'cat', 5: 'dog', 6: 'house', 'D': 'car'}

I believe this code will get you your desired result:

with open('filea.txt', 'rU') as f:
    reader = csv.reader(f, delimiter="\t")
    d1 = {}
    for line in reader:
        if line[1] != "":
            d1[line[0]] = int(line[1])

with open('fileb.txt', 'rU') as f:
    reader = csv.reader(f, delimiter="\t")
    reader.next()  # Skip header row.
    d2 = {}
    for line in reader:
        d2[line[0]] = [float(i) for i in line[1:]]

d3 = {d1[k] if k in d1 else k: d2[k] for k in d2}

Upvotes: 3

zenofsahil
zenofsahil

Reputation: 1753

import re

d1 = dict()
with open('filea.txt', 'r') as fl:
    for f in fl:
        key, val = re.findall('\w+', f)
        d1[key] = val

d2 = dict()
with open('file2.txt', 'r') as fl:
    for f in fl:
        key, val = re.findall('\w+', f)
        d2[key] = val

with open('file3.txt', 'wb') as f:
    for k, v in d1.items():
        f.write("{a}\t{b}\n".format(a=v, b=d2[k]))

Upvotes: 1

Tamas Hegedus
Tamas Hegedus

Reputation: 29906

If the two dictionaries are called a and b, you can construct a new dictionary this way:

composed_dict = {a[k]:b[k] for k in a}

This will take all the keys in a, and read the corresponding values from a and b to construct a new dictionary.

Regarding your code:

  • The variable a_data has no purpose. You read the first file, pront the first column, and do nothing else with the data in it
  • zip(elements[::1], elements[0::1]) will just construct pairs like [1,2,3] -> [(1,1),(2,2),(3,3)], I think that's not what you want
  • After all you have a list of dictionaries, and at the last line you just put strings in that list. I think that is not intentional.

Upvotes: 1

Kevin
Kevin

Reputation: 8207

You could use dictionary comprehension:

d1 = {'A':2,'B':5,'C':6}
d2 = {'A':'cat','B':'dog','C':'house'}

In [23]:  {d1[k]:d2[k] for k in d1.keys()}
Out[23]:  {2: 'cat', 5: 'dog', 6: 'house'}

Upvotes: 3

Related Questions