webcrew
webcrew

Reputation: 157

python list parsing example

I'd like to know how to parse (or split) and element of a list?

I have a list of lists (of string) such as:

resultList =  [['TWP-883 PASS'], ['TWP-1080 PASS'], ['TWP-1081 PASS']]

where:

resultList[0] = ['TWP-883 PASS']
resultList[1] = ['TWP-1080 PASS']

essentially, I need a variable for the two entries in each element of the list. For example:

issueId = 'TWP-883'
status = 'PASS'

What would allow for iterating through this list and parsing such as above?

Upvotes: 1

Views: 9467

Answers (5)

majid bhatti
majid bhatti

Reputation: 103

You can get the list of the strings by

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)]

to go through every issueID and corrosponding status you can use

for id,st in zip(issueId,status):
    print(id, " : ", st)

Upvotes: 0

jDo
jDo

Reputation: 4010

Note: I changed this answer to reflect an edit of the question. Specifically, I added a split() to separate the strings in the nested lists into two strings (issueId and status).


I would use list and dictionary comprehensions to turn your list of lists into a list of dictionaries with the keys issueId and 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]

Lookups can now be done in this way:

>>> result_dicts[0]["status"]
'PASS'
>>> result_dicts[0]["issueId"]
'TWP-883'
>>> result_dicts[1]
{'status': 'PASS', 'issueId': 'TWP-1080'}
>>> 

To declare variables for each value in each dictionary in the list and print them, use the code below:

for entry in result_dicts:
    issueId = entry["issueId"]
    status = entry["status"]
    print("The status of {0: <10} is {1}".format(issueId, status))

Output:

The status of TWP-883    is PASS
The status of TWP-1080   is PASS
The status of TWP-1081   is PASS

Upvotes: 1

C Panda
C Panda

Reputation: 3415

If you wish to do more transformations later, use a genexp,

g = (func(issueid, status) for issueid, status in resultList) # func returns non-None objects

If you just want to consume the iterable,

for issueid, status in resultList:
    # print(issueid, status)
    # whatever

Upvotes: 0

PM 2Ring
PM 2Ring

Reputation: 55489

You just need a simple for loop that exploits Python's tuple unpacking machinery.

for issueId, status in resultList:
    # Do stuff with issueId and status

Upvotes: 1

Bahrom
Bahrom

Reputation: 4862

Well that's as simple as:

# You can also assign as you iterate as suggested in the comments.
for issue, status in resultList:
    print issue, status

This outputs

TWP-883 PASS
TWP-1080  PASS
TWP-1081  PASS
TWP-1082  PASS
TWP-884  FAIL
TWP-885  PASS

Here's another example:

>>> x = [1, 2] # or (1, 2), or '12' works with collections
>>> y, z = x
>>> y
1
>>> z
2
>>> 

Incidentally, in Python 3.x, you can also do:

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]

Upvotes: 5

Related Questions