Reputation: 261
I have a data in list as follows
['And user clicks on the link "Statement and letter preferences" -> set([0, 2])',
'And user waits for 10 seconds -> set([0, 2])',
'Then page is successfully launched -> set([0, 1, 2])',
'@TestRun -> set([0, 1, 2])',
'And user set text "#Surname" on textbox name "surname" -> set([0, 1, 2])',
'And user click on "menu open user preferences" label -> set([0, 2])']
In these data I have set([0,2]) , now I want all the satements that occurs in 0,1,2 in different list ? how can we do this in python
Expected output is
list_0 i.e which contains all statements that has 0 in set(0,2)
list_0
[And user clicks on the link "Statement and letter preferences
And user waits for 10 seconds
Then page is successfully launched
'@TestRun
And user set text "#Surname" on textbox name "surname
And user click on "menu open user preferences" label]
list_1
[ Then page is successfully launched
'@TestRun
And user set text "#Surname" on textbox name "surname]
list_2
[And user clicks on the link "Statement and letter preferences
And user waits for 10 seconds
Then page is successfully launched
'@TestRun
And user set text "#Surname" on textbox name "surname
And user click on "menu open user preferences" label]
Upvotes: 2
Views: 150
Reputation: 402713
I'd recommend appending strings to a dictionary of lists. You'll understand why.
First, here's a high level approach to solving this problem -
from collections import defaultdict
import re
d = defaultdict(list)
for i in data:
x, y = i.split('->')
for z in map(int, re.findall('\d+', y)):
d[z].append(x.strip()) # for performance, move the `strip` call outside the loop
print(d)
{
"0": [
"And user clicks on the link \"Statement and letter preferences\"",
"And user waits for 10 seconds",
"Then page is successfully launched",
"@TestRun",
"And user set text \"#Surname\" on textbox name \"surname\"",
"And user click on \"menu open user preferences\" label"
],
"1": [
"Then page is successfully launched",
"@TestRun",
"And user set text \"#Surname\" on textbox name \"surname\""
],
"2": [
"And user clicks on the link \"Statement and letter preferences\"",
"And user waits for 10 seconds",
"Then page is successfully launched",
"@TestRun",
"And user set text \"#Surname\" on textbox name \"surname\"",
"And user click on \"menu open user preferences\" label"
]
}
You can find all strings related to ID i
by querying d[i]
. This is much cleaner than initialising separate lists.
Upvotes: 2