简体   繁体   中英

Is there a way to make a dictionary from a single list where the key and value for the dict are taken from a specific index

After creating a list from a couple of text files I get the following:

['host : myhost.ro', 'port : 12222', 'service_name : SRVNM', 'user : MGA', 'password : Test']

Is there a way to make 2 lists like this:

lst1 =['host', 'port', 'service_name', 'user', 'password']
lst2 =['myhost.ro', '12222', 'SRVNM', 'MGA', 'Test']

Or to make a dictionary from the original list, this is my end goal, a dict like this:

Dict['host'] = 'myhost.ro'

You can use a dict comprehension:

lst = ['host : myhost.ro', 'port : 12222', 'service_name : SRVNM', 'user : MGA', 'password : Test']

dct = {key: value for item in lst for key, value in [item.split(" : ")]}
print(dct)

Which yields

{'host': 'myhost.ro', 'port': '12222', 'service_name': 'SRVNM', 'user': 'MGA', 'password': 'Test'}

You could do this with some combination of str.strip , str.split , map , zip , comprehension generators:

# ...to obtain the `dict`
dict(map(str.strip, x.split(':')) for x in ll)
# {'host': 'myhost.ro', 'port': '12222', 'service_name': 'SRVNM', 'user': 'MGA', 'password': 'Test'}
# ...to obtain the "parallel" sequences (`tuples`)
l1, l2 = zip(*(map(str.strip, x.split(':')) for x in ll))
l1
# ('host', 'port', 'service_name', 'user', 'password')
l2
# ('myhost.ro', '12222', 'SRVNM', 'MGA', 'Test')

Assuming the input to be:

ll = ['host : myhost.ro', 'port : 12222', 'service_name : SRVNM', 'user : MGA', 'password : Test']

Also, it looks like the dict() seems a better option in this use case.

try this:

data = ['host : myhost.ro', 'port : 12222', 'service_name : SRVNM', 'user : MGA', 'password : Test']

data_dict = dict([tuple(e.split(" : ")) for e in data])
print(data_dict)
original = ['host : myhost.ro', 'port : 12222', 'service_name : SRVNM', 'user : MGA', 'password : Test']

b = dict([a.split(' : ') for a in original])

You can split your list into two lists that you want like this:

myList = ['host : myhost.ro', 'port : 12222', 'service_name : SRVNM', 'user : MGA', 'password : Test']
list1 = []
list2 = []

for i in myList:
    element1 = i.split(":")[0]
    element2 = i.split(":")[1]
    list1.append(element1)
    list2.append(element2)
print(list1)
print(list2)

Output:

['host ', 'port ', 'service_name ', 'user ', 'password ']                                                             
[' myhost.ro', ' 12222', ' SRVNM', ' MGA', ' Test'] 

Or Your Final desired Dictionary will be achieved by:

newDict = {}

for i in myList:
    key = i.split(":")[0].strip()
    value = i.split(":")[1].strip()
    newDict[key] = value
print(newDict)

Output:

{'user': 'MGA', 'port': '12222', 'service_name': 'SRVNM', 'password': 'Test', 'host': 'myhost.ro'}

Take advantage of string method split and list \ dict comprehensions.

To get your dict:

data = ['host : myhost.ro', 'port : 12222', 'service_name : SRVNM', 'user : MGA', 'password : Test']
{entry.split(' : ')[0]:entry.split(' : ')[1] for entry in data}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM