Home > front end >  Split links inside a list of values of a Python dictionary
Split links inside a list of values of a Python dictionary

Time:11-14

I have a dictionary like this:

dict = {
    key1: <http://www.link1.org/abc/f><http://www.anotherlink.com/ght/y2>,
    key2: <http://www.link1.org/abc/f><http://www.anotherOneLink.en/ttta/6jk>,
    key3: <http://www.somenewlink.xxw/o192/ggh><http://www.link4.com/jklu/wepdo9>,
    key4: <http://www.linkkk33.com/fgkjc><http://www.linknew2.com/poii/334hsj>,
    ...
       }

Goal to achieve:

I want to separate the 2 links inside each value of the dictionary, and then count how many times each first value occurs in the entire dictionary. Something like this:

new_dict = {
    key1: [<http://www.link1.org/abc/f>,  <http://www.anotherlink.com/ght/y2>],
    key2: [<http://www.link1.org/abc/f>,  <http://www.anotherOneLink.en/ttta/6jk>],
    key3: [<http://www.somenewlink.xxw/o192/ggh>,  <http://www.link4.com/jklu/wepdo9>],
    key4: [<http://www.linkkk33.com/fgkjc>,  <http://www.linknew2.com/poii/334hsj>],
    ...
       }

first_value_count = {
          <http://www.link1.org/abc/f> : 2,
          <http://www.somenewlink.xxw/o192/ggh> : 1,
          <http://www.linkkk33.com/fgkjc> : 1,
          ....             
              }

My Code:

To split the values I've tried this, but it doesn't work:

new_dict = {k: v[0].split(">") for k, v in dict.items()}

To count the value occurrences in my dictionary:

from collections import Counter
all_dictionary_values = []
for v[0] in new_dict.values():
   all_dictionary_values.append(x)

count = Counter(all_dictionary_values)

I have a very big dictionary (1M keys), is this the fastest way to count all value occurrences in my dictionary?

CodePudding user response:

I tried your code but it didn't work on my side, so I changed it as follows :

dict = {
    'key1' : "<http://www.link1.org/abc/f><http://www.anotherlink.com/ght/y2>",
    'key2': "<http://www.link1.org/abc/f><http://www.anotherOneLink.en/ttta/6jk>",
    'key3' : "<http://www.somenewlink.xxw/o192/ggh><http://www.link4.com/jklu/wepdo9>",
    'key4': "<http://www.linkkk33.com/fgkjc><http://www.linknew2.com/poii/334hsj>",
       }
new_dict = {k: v.split("><") for k, v in dict.items()}
new_dict

Output

{'key1': ['<http://www.link1.org/abc/f', 'http://www.anotherlink.com/ght/y2>'],
 'key2': ['<http://www.link1.org/abc/f',
  'http://www.anotherOneLink.en/ttta/6jk>'],
 'key3': ['<http://www.somenewlink.xxw/o192/ggh',
  'http://www.link4.com/jklu/wepdo9>'],
 'key4': ['<http://www.linkkk33.com/fgkjc',
  'http://www.linknew2.com/poii/334hsj>']}

And we add the counter here :

from collections import Counter
all_dictionary_values = []
for v in new_dict.values():
       all_dictionary_values.append(v[0] ">")

count = Counter(all_dictionary_values)
count

Ouput

Counter({'<http://www.link1.org/abc/f': 2,
         '<http://www.somenewlink.xxw/o192/ggh': 1,
         '<http://www.linkkk33.com/fgkjc': 1})
  • Related