I want to create a dictionary from list of strings. For example I have these list
AAAA
AAAA
AAAA
BBBB
BBBB
CCCC
CCCC
CCCC
....
Then I want to create a dictionary with numbering value from that, how to do that?
I explored some code but still have no idea
import os
path = "directoryA"
dirList = os.listdir(path)
with open("check.txt", "w") as a:
for path, subdirs, files in os.walk(path):
for filename in files:
# I have splitted the text and now I want to create dictionary
#from it
mylist = filename.split("_") # the text format is AAAA_0 and I split
#it so I can have list of 'AAAA' and '0'
k = mylist[0] #I only take 'AAAA' string after splitting
print(k) # here the output only give text output. From this I want to
# put into dictionary
This is the output after print(k) and these are not list
AAAA
AAAA
AAAA
BBBB
BBBB
CCCC
CCCC
CCCC
....
This is my expected result
myDic ={
'AAAA': 0,
'BBBB': 1,
'CCCC': 2,
'DDDD': 3,
# ... and so on
}