2

Hey everyone this code is working fine just one thing to deal with. It overwrites the multiple entries against a key. I need to avoid the overwriting and to save all those entries. Can you help me in this please?

#!/usr/bin/python

import sys
import fileinput

#trys to create dictionary from african country list
dic = {}

for line in sys.stdin:
    lst = line.split('|')
    links = lst[4].split()
    new=links[0] + ' ' + links[len(links)-1]
    dic[new]=lst[4] # WARNING: overwrites multiple entriess at the moment! :)

for newline in fileinput.input('somefile.txt'):
    asn = newline.split()
    new = asn[0] + ' ' + asn[1]
    if new in dic:
            print "found from " + asn[0] + " to " + asn[1]
            print dic[new]

Note: Sys.stdin takes a string in the following format; 1.0.0.0/24|US|158.93.182.221|GB|7018 1221 3359 3412 2543 1873

2
  • 2
    You shouldn't use list as a variable name since it shadows the builtin function list(). Also you're miss ing a colon in the if statement, which BTW should probably look like this: if new in dic:. At least, that's the pythonic way. Commented Aug 29, 2011 at 21:24
  • What do you expect dic[dic[new]] to do, and why? (Hint: what does dic[new] do?) Commented Aug 30, 2011 at 4:43

2 Answers 2

5

You've got a number of problems with your code. The simplest way to do what you describe is to use a defaultdict, which gets rid of the explicit if and has_key (which you should replace by new in dic anyway):

#trys to create dictionary from african country list
from collections import defaultdict

dic = defaultdict(list)   # automatically creates lists to append to in the dict

for line in sys.stdin:
    mylist = line.split('|')    # call it mylist instead of list
    links = mylist[4].split()
    new = links[0] + ' ' + links[-1]   # can just use -1 to reference last item
    dic[new].append(mylist[4])         # append the item to the list in the dict
                                # automatically creates an empty list if needed

See eryksun's comment on Gerrat's answer if you're on an old version of Python without defaultdict.

Sign up to request clarification or add additional context in comments.

Comments

1

There is no method called appendlist. use append:

dic[dic[new]].append(list[4])

Also, it's inadvisable to use list as a variable name.
It is a builtin in python.

Also, this entire section of code:

    if ( dic.has_key(new))
        dic[dic[new]].appendlist(list[4])
    else:
       dic[dic[new]] = [list[4]] 

should instead probably be:

    if new in dic:  # this is the preferrable way to test this
        dic[new].append(list[4])
    else:
       dic[new] = [list[4]] 

13 Comments

Hi, I have just corrected the mistakes I had made. But still code: dic[dic[new]] = [list[4]] is not working and gives error "'type' subject is not subscriptable".
@Sohaib - as both @Gerrat and I said in our answers, dic[dic[new]] is wrong, however, that shouldn't give a trying to subscript a type object (not "subject"!) error. You must have accidentally typed dict instead of dic, as dict is a type object and dic is not.
@Gerrat: I have tried your preferred way to test it and encountered an error "'str' object has no attribute 'append'". I am actually trying to create a dictionary which will contain multiple values against a key, each value is a string. I would then read content of a new file and on comparing keys in dictionary I'll print those values in of key.
@Sohaib: it sounds like you've used: dic[new] = lst[4], not dic[new] = [lst[4]] as you've posted...is the code above exactly what you're running?
@Gerrat: yes, as it is in code, I have used it in the same way.
|

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.