'list' object has no attribute 'find'
Solution 1
You could use str.split
to deal with strings. First split each element string with '('
, with maxsplit being 1:
In [48]: dic=dict(e[:-1].split('(', 1) for e in entities) #using [:-1] to filter out ')'
...: print dic
...:
{'#5= IFCAPPLICATION': "#1,'2014','Autodesk Revit 2014 (ENU)','Revit')", '#1= IFCORGANIZATION': "$,'Autodesk Revit 2014 (ENU)',$,$,$)"}
then split each value in the dict with ','
:
In [55]: dic={k: dic[k][:-1].split(',') for k in dic}
...: print dic
{'#5= IFCAPPLICATION': ['#1', "'2014'", "'Autodesk Revit 2014 (ENU)'", "'Revit'"], '#1= IFCORGANIZATION': ['$', "'Autodesk Revit 2014 (ENU)'", '$', '$', '$']}
Note that the key-value pairs in a dict is unordered, as you may see '#1= IFCORGANIZATION'
is not showing in the first place.
Solution 2
If you want to know if a value is in a list you can use in
, like this:
>>> my_list = ["one", "two", "three"]
>>> "two" in my_list
True
>>>
If you need to get the position of the value in the list you must use index
:
>>> my_list.index("two")
1
>>>
Note that the first element of the list has the 0 index.
Solution 3
Here you go:
>>> import re
>>> import ast
>>> entities = ["#1= IFCORGANIZATION('$','Autodesk Revit 2014 (ENU)','$','$','$');", "#5= IFCAPPLICATION('#1','2014','Autodesk Revit 2014 (ENU)','Revit');"]
>>> entities = [a.strip(';') for a in entities]
>>> pattern = re.compile(r'\((.*)\)')
>>> dic = {}
>>> for a in entities:
... s = re.search(pattern, a)
... dic[a[:a.index(s.group(0))]] = list(ast.literal_eval(s.group(0)))
>>> dic
{'#5= IFCAPPLICATION': ['#1', '2014', 'Autodesk Revit 2014 (ENU)', 'Revit'], '#1= IFCORGANIZATION': ['$', 'Autodesk Revit 2014 (ENU)', '$', '$', '$']}
This regex r'\((.*)\)'
looks for elements in (
and )
and converts them to a list. It makes the sub string appearing before the brackets as the key and the list as the value.
JoaoSa
Updated on July 09, 2022Comments
-
JoaoSa almost 2 years
I know this is a basic question, but I'm new to python and can't figure out how to solve it.
I have a list like the next example:
entities = ["#1= IFCORGANIZATION($,'Autodesk Revit 2014 (ENU)',$,$,$)";, "#5= IFCAPPLICATION(#1,'2014','Autodesk Revit 2014 (ENU)','Revit');"]
My problem is how to add the information from the list
"entities"
to a dictionary in the following format:dic = {'#1= IFCORGANIZATION' : ['$','Autodesk Revit 2014 (ENU)','$','$','$'], '#5= IFCAPPLICATION' : ['#1','2014','Autodesk Revit 2014 (ENU)','Revit']
I tried to do this using
"find"
but I'm getting the following error:'list' object has no attribute 'find'
,and I don't know how to do this without find method.
-
JoaoSa about 10 yearsTrimax: Thank you for the support. The problem is that the data in the list is always diferent and consequently I can't search for a specific word. I need to divide each string in the list, in the format presented before, so that I can manipulate the information and then use it.
-
Trimax about 10 yearsFor search pattern in strings is more useful the Regular Expressions module
re
docs.python.org/3.3/library/re.html and you can extract them by groups in a list. -
cox about 10 years@Trimax your right, but a list comprehension will be more appropiate. If the so don't know how to iterate over a list, you think it will be ok with re ?
-
Trimax about 10 yearswith re.findall() you get a list with all mathes in the string and your input seems has a pattern, the data in the list is always diferent but it isn't ramdom.
-
JoaoSa about 10 yearszhangxaochen: Many thanks for your support. I tried your code and it really does what I need. I dont need the dictionary to be in a specific order so that's not a problem. Thank you for the explation too. Regards!
-
JoaoSa about 10 yearsshaktimaan: Thank you for your quick support. I didn't know those modules, so thank you for introducing me to them. I didn't manage to get your code to work because in my "entities" variable I don't have the ' along with the $, i.e. '$'. But I think with a few ajustments it will wotk fine! Regards.
-
JoaoSa about 10 years@Trimax In my particular case I don't mind if the dictionary isn't in a correct order. But if i need so, I will use your sugestion. Thank you.
-
shaktimaan about 10 yearsWithout the quotes it will get a little complicated to store them as a list
-
JoaoSa about 10 years@ shaktimaan: The zhangxaochen answer doesn't use any modules but it works just fine! Thank you.