Given the following list of lists which has a length of 2:
cLists = [[(1.3, "30__[1.353, '12', 'weekly']_[1.35, '20', 'daily']"), (1.35, '0_Retrace_50_M')], [(1.34, "32__[1.34, '11', 'weekly']_[1.34, '12', 'daily']"), (1.3459, '1_Ret_50_W')]]
len(cLists)
2
I can use a list comprehension on list index 1 cLists[1]
as follows:
[level_description[1] for level_description in cLists[1]
if any(k in level_description[1] for k in ['daily','weekly','monthly'])]
However I am struggling to iterate over both elements of the list cLists[0]
and cLists[1]
How do I iterate over both elements of this list?
Assuming you want a nested list of results to match the input, you can just nest your list comprehensions:
[[ld[1] for ld in cList if any(k in ld[1] for k in ['daily','weekly','monthly'])] for cList in cLists]
Output:
[
["30__[1.353, '12', 'weekly']_[1.35, '20', 'daily']"],
["32__[1.34, '11', 'weekly']_[1.34, '12', 'daily']"]
]
If you don't want a nested list, you just need to add a for
expression over the cLists
values:
[ld[1] for cList in cLists for ld in cList if any(k in ld[1] for k in ['daily','weekly','monthly'])]
Output:
[
"30__[1.353, '12', 'weekly']_[1.35, '20', 'daily']",
"32__[1.34, '11', 'weekly']_[1.34, '12', 'daily']"
]
Not sure if this is what your looking or not...
aa = []
for i in cLists:
aa.append([level_description[1] for level_description in i
if any(k in level_description[1] for k in ['daily','weekly','monthly'])])
aa then being a list of matches.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.