python 列表解析示例
python list parsing example
我想知道如何解析(或拆分)列表的元素?
我有一个(字符串的)列表列表,例如:
resultList = [['TWP-883 PASS'], ['TWP-1080 PASS'], ['TWP-1081 PASS']]
其中:
resultList[0] = ['TWP-883 PASS']
resultList[1] = ['TWP-1080 PASS']
本质上,我需要一个变量来表示列表中每个元素中的两个条目。例如:
issueId = 'TWP-883'
status = 'PASS'
什么允许遍历此列表并像上面那样进行解析?
好吧,就这么简单:
# You can also assign as you iterate as suggested in the comments.
for issue, status in resultList:
print issue, status
这输出
TWP-883 PASS
TWP-1080 PASS
TWP-1081 PASS
TWP-1082 PASS
TWP-884 FAIL
TWP-885 PASS
这是另一个例子:
>>> x = [1, 2] # or (1, 2), or '12' works with collections
>>> y, z = x
>>> y
1
>>> z
2
>>>
顺带一提,在Python3.x中,您还可以:
In [1]: x = [1, 2, 3, 4]
In [2]: y, z, *rest = x
In [3]: y
Out[3]: 1
In [4]: z
Out[4]: 2
In [5]: rest
Out[5]: [3, 4]
您只需要一个简单的 for
循环来利用 Python 的元组解包机制。
for issueId, status in resultList:
# Do stuff with issueId and status
如果你以后想做更多的转换,使用genexp,
g = (func(issueid, status) for issueid, status in resultList) # func returns non-None objects
如果你只想使用可迭代对象,
for issueid, status in resultList:
# print(issueid, status)
# whatever
注意:我更改了这个答案以反映对问题的编辑。具体来说,我添加了一个 split()
将嵌套列表中的字符串分隔为两个字符串(issueId
和 status
)。
我会使用列表和字典理解将您的列表列表转换为具有键 issueId
和 status
:
的字典列表
resultList = [['TWP-883 PASS'], ['TWP-1080 PASS'], ['TWP-1081 PASS']]
result_dicts = [{("issueId","status")[x[0]]:x[1] for x in enumerate(lst[0].split())} for lst in resultList]
现在可以通过以下方式进行查找:
>>> result_dicts[0]["status"]
'PASS'
>>> result_dicts[0]["issueId"]
'TWP-883'
>>> result_dicts[1]
{'status': 'PASS', 'issueId': 'TWP-1080'}
>>>
要为列表中每个字典中的每个值声明变量并打印它们,请使用以下代码:
for entry in result_dicts:
issueId = entry["issueId"]
status = entry["status"]
print("The status of {0: <10} is {1}".format(issueId, status))
输出:
The status of TWP-883 is PASS
The status of TWP-1080 is PASS
The status of TWP-1081 is PASS
您可以通过
获取字符串列表
issueId = [y for x in resultList for (i,y) in enumerate(x.split()) if(i%2==0)]
status = [y for x in resultList for (i,y) in enumerate(x.split()) if(i%2==1)]
查看每个问题 ID 和您可以使用的相应状态
for id,st in zip(issueId,status):
print(id, " : ", st)
我想知道如何解析(或拆分)列表的元素?
我有一个(字符串的)列表列表,例如:
resultList = [['TWP-883 PASS'], ['TWP-1080 PASS'], ['TWP-1081 PASS']]
其中:
resultList[0] = ['TWP-883 PASS']
resultList[1] = ['TWP-1080 PASS']
本质上,我需要一个变量来表示列表中每个元素中的两个条目。例如:
issueId = 'TWP-883'
status = 'PASS'
什么允许遍历此列表并像上面那样进行解析?
好吧,就这么简单:
# You can also assign as you iterate as suggested in the comments.
for issue, status in resultList:
print issue, status
这输出
TWP-883 PASS
TWP-1080 PASS
TWP-1081 PASS
TWP-1082 PASS
TWP-884 FAIL
TWP-885 PASS
这是另一个例子:
>>> x = [1, 2] # or (1, 2), or '12' works with collections
>>> y, z = x
>>> y
1
>>> z
2
>>>
顺带一提,在Python3.x中,您还可以:
In [1]: x = [1, 2, 3, 4]
In [2]: y, z, *rest = x
In [3]: y
Out[3]: 1
In [4]: z
Out[4]: 2
In [5]: rest
Out[5]: [3, 4]
您只需要一个简单的 for
循环来利用 Python 的元组解包机制。
for issueId, status in resultList:
# Do stuff with issueId and status
如果你以后想做更多的转换,使用genexp,
g = (func(issueid, status) for issueid, status in resultList) # func returns non-None objects
如果你只想使用可迭代对象,
for issueid, status in resultList:
# print(issueid, status)
# whatever
注意:我更改了这个答案以反映对问题的编辑。具体来说,我添加了一个 split()
将嵌套列表中的字符串分隔为两个字符串(issueId
和 status
)。
我会使用列表和字典理解将您的列表列表转换为具有键 issueId
和 status
:
resultList = [['TWP-883 PASS'], ['TWP-1080 PASS'], ['TWP-1081 PASS']]
result_dicts = [{("issueId","status")[x[0]]:x[1] for x in enumerate(lst[0].split())} for lst in resultList]
现在可以通过以下方式进行查找:
>>> result_dicts[0]["status"]
'PASS'
>>> result_dicts[0]["issueId"]
'TWP-883'
>>> result_dicts[1]
{'status': 'PASS', 'issueId': 'TWP-1080'}
>>>
要为列表中每个字典中的每个值声明变量并打印它们,请使用以下代码:
for entry in result_dicts:
issueId = entry["issueId"]
status = entry["status"]
print("The status of {0: <10} is {1}".format(issueId, status))
输出:
The status of TWP-883 is PASS
The status of TWP-1080 is PASS
The status of TWP-1081 is PASS
您可以通过
获取字符串列表issueId = [y for x in resultList for (i,y) in enumerate(x.split()) if(i%2==0)]
status = [y for x in resultList for (i,y) in enumerate(x.split()) if(i%2==1)]
查看每个问题 ID 和您可以使用的相应状态
for id,st in zip(issueId,status):
print(id, " : ", st)