user11606914
user11606914

Reputation:

How to convert a list to a dict?

I am using subprocess to print the output of ls.

output = subprocess.getoutput("ssh -i key.pem [email protected] ls -l --time-style=long-iso /opt/databases | awk -F' ' '{print $6 $8}'")
lines = output.splitlines()
print(lines)
format = '%Y-%m-%d'
for line in lines:
   if line != '':
      date = datetime.strptime(line, format)

And when I print lines am getting a large list in the following format:

['', '2019-04-25friendship_graph_43458', '2019-07-18friendship_graph_45359', '2019-09-03friendship_graph_46553', '2019-10-02friendship_graph_46878']

I am trying to convert the above output to a dict with the dates in '%Y-%m-%d' format. So output would be something like:

{ '2019-04-25' : 'friendship_graph_43458',
  '2019-07-18': 'friendship_graph_45359',
  '2019-09-03': 'friendship_graph_46553' }

and so on, but not quite sure how to do so.

Upvotes: 1

Views: 84

Answers (4)

Jab
Jab

Reputation: 27485

Technically if you don't want to use re if all dates are formatted the same then they will all be 10 characters long thus just slice the strings to make the dict in a comprehension:

data = ['', '2019-04-25friendship_graph_43458', '2019-07-18friendship_graph_45359', '2019-09-03friendship_graph_46553', '2019-10-02friendship_graph_46878']

output = {s[:10]: s[10:] for s in data if len(s) > 10}

{'2019-04-25': 'friendship_graph_43458', '2019-07-18': 'friendship_graph_45359', '2019-09-03': 'friendship_graph_46553', '2019-10-02': 'friendship_graph_46878'}

Upvotes: 3

wwii
wwii

Reputation: 23753

For lines that start with the date; use slices to separate the key from the value.

>>> s = '2019-04-25friendship_graph_43458'
>>> d = {}
>>> d[s[:10]] = s[10:]
>>> d
{'2019-04-25': 'friendship_graph_43458'}
>>>

Upvotes: 2

Jacob Lee
Jacob Lee

Reputation: 4670

You could use a regular expression for each item in the list. For example:

(\d{4}-\d{2}-\d{2})(.*)

Then, you can just iterate through each item in the list and use the regular expression to the get the string in its two parts.

>>> import re
>>> regex = re.compile(r"(\d{4}-\d{2}-\d{2})(.*)")
>>> items = ['', '2019-04-25friendship_graph_43458', '2019-07-18friendship_graph_45359', '2019-09-03friendship_graph_46553', '2019-10-02friendship_graph_46878']
>>> items_dict = {}
>>> for i in items:
        match = regex.search(i)
        if match is None:
            continue
        items_dict[match.group(1)] = match.group(2)

    
>>> items_dict
{'2019-04-25': 'friendship_graph_43458', '2019-07-18': 'friendship_graph_45359', '2019-09-03': 'friendship_graph_46553', '2019-10-02': 'friendship_graph_46878'}

Upvotes: 2

Timur Shtatland
Timur Shtatland

Reputation: 12347

Use re.findall and dictionary comprehension:

import re
lst = ['', '2019-04-25friendship_graph_43458', '2019-07-18friendship_graph_45359', '2019-09-03friendship_graph_46553', '2019-10-02friendship_graph_46878']
dct = {k: v for s in lst for k, v in re.findall(r'(\d\d\d\d-\d\d-\d\d)(.*)', s) }
print(dct)
# {'2019-04-25': 'friendship_graph_43458', '2019-07-18': 'friendship_graph_45359', '2019-09-03': 'friendship_graph_46553', '2019-10-02': 'friendship_graph_46878'}

Upvotes: 1

Related Questions