jester112358
jester112358

Reputation: 465

Assign variable to python list position

points = '<td class="ismCol2">.*</td>'
points_pattern = re.compile(points)
re_weekly_points = re.findall(points_pattern, htmltext)[2]

The code works in this format, but I'd like to assign an argument/variable to re_weekly_points.

value = 3
re_weekly_points = re.findall(re_weekly_points_pattern, htmltext)["%s"] %(value)

This is the error I receive. What is the easiest way to bypass this issue?

list indices must be integers, not str

Upvotes: 0

Views: 1099

Answers (1)

GSalazar
GSalazar

Reputation: 166

Does it work for you to have:

value = 3 re_weekly_points = re.findall(re_weekly_points_pattern, htmltext)[value]

That way the indices will be integers unless value is not a integer.

To have something similar to lists but using strings as indices you might consider using a dictionary

Upvotes: 1

Related Questions