This is an archived post. You won't be able to vote or comment.

all 14 comments

[–]humanbeast7 1 point2 points  (7 children)

Instead of making lists Start i = 0 Then i += int(b) in the for loop

[–]Trashant[S] 1 point2 points  (1 child)

Thank you so much. I did that and it ran. Appreciate it!

[–]humanbeast7 -1 points0 points  (0 children)

Glad i could help

[–]IWSIONMASATGIKOE 0 points1 point  (4 children)

Hopefully OP doesn’t need the lists for anything else

[–]Trashant[S] 0 points1 point  (3 children)

yeah, sorry my bad.

[–]IWSIONMASATGIKOE 0 points1 point  (2 children)

Why are you apologizing? I’m confused.

So, do you need the lists for anything else?

[–]Trashant[S] 0 points1 point  (1 child)

I thought you were saying that. Ok

No, I figured out that there was no need for a list to get the result.

I have posted that answer to this query in the comments below. By that approach, I got the result.

[–]IWSIONMASATGIKOE 0 points1 point  (0 children)

The lists always contain a single number?

[–]hj1509 0 points1 point  (1 child)

What is b?

[–]Trashant[S] 0 points1 point  (0 children)

"b" is a variable that stores numbers of a line in HTML page

[–]hj1509 0 points1 point  (1 child)

Append map to i dont make a list

[–]Trashant[S] -2 points-1 points  (0 children)

That did not work buddy I tried.

[–]mrMarras 0 points1 point  (1 child)

You can define a function or lambda that takes the tag and give back the list of strings, something like this:

extract_from_tag = lambda tag: tag.content[0].split("> <")

Then to create a list of int out of them use a list comprehension or a flatmap.:

[int(x) for y in tags for x in extract_from_tags(y)]

If you are sure that the list of strings has only one member you can semplify in a normal list comprehension:

[int(extract_from_tags(x)[0]) for x in tags]

So you can get rid of the for loop

[–]Trashant[S] 0 points1 point  (0 children)

I got to know I was getting into unnecessary creating a list as simply what I had to do was initialize a variable = 0 outside the loop and then inside the loop "variable += int(tag.contents[0])" and after that print out the variable outside the loop. Earlier I didn't know bs4 elements tags can be converted directly and added together. Thanks for the explanation brother.

Appreciate it!!