3

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"

4 Answers 4

3

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}
Sign up to request clarification or add additional context in comments.

6 Comments

I got AttributeError: 'list' object has no attribute 'items'
That is because dictList is indeed a list and not a dictionary. Could you post a small sample of data from both?
I just edited to include code to read that file as a dictionary (d1 in my example above).
|
3

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'}

Comments

1

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.

5 Comments

That I think was one of my main problems. You see fileA has 100 columns so I wanted to make sure dict would just go over the first and not waste time with the others. zip(elements[::1], elements[0::1]) gives me was supposed to take column 0 and 1 from the second file and construct it as A:2, so I would have a dict. I think I may have got things all confused. I'll start again. Thanks
@CarolM and what would you like to use the first column of the first file for?
That's the file where I need to replace the letters for numbers in the first column.
@CarolM Ok now we know which file contains what. And what's your output? a dictionary? a new file? overwrite the original file?
The plan is to overwrite the original file.
1
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]))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.