Python “列表”对象没有属性“查找”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23201351/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
'list' object has no attribute 'find'
提问by JoaoSa
I know this is a basic question, but I'm new to python and can't figure out how to solve it.
我知道这是一个基本问题,但我是 python 新手,无法弄清楚如何解决它。
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:
我的问题是如何将列表中的信息"entities"
按以下格式添加到字典中:
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'
,
我试图用这个做的"find"
,但我发现了以下错误:
'list' object has no attribute 'find'
,
and I don't know how to do this without find method.
如果没有 find 方法,我不知道如何做到这一点。
采纳答案by zhangxaochen
You could use str.split
to deal with strings. First split each element string with '('
, with maxsplit being 1:
你可以str.split
用来处理字符串。首先用 分割每个元素字符串'('
,maxsplit 为 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.
请注意,dict 中的键值对是无序的,正如您可能看到的,一开始'#1= IFCORGANIZATION'
并没有显示。
回答by Trimax
If you want to know if a value is in a list you can use in
, like this:
如果您想知道某个值是否在列表中,您可以使用in
,如下所示:
>>> 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
:
如果您需要获取列表中值的位置,则必须使用index
:
>>> my_list.index("two")
1
>>>
Note that the first element of the list has the 0 index.
请注意,列表的第一个元素的索引为 0。
回答by shaktimaan
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.
此正则表达式r'\((.*)\)'
在(
和 中查找元素)
并将它们转换为列表。它使出现在括号前的子字符串作为键,将列表作为值。