Python合并列表(Python merge lists)
我想从网站上获得数字的总和。 我这样做:
lst = list() url = raw_input('Enter - ') html = urllib.urlopen(url).read() soup = BeautifulSoup(html) tags = soup('span') for tag in tags: lst.append(map(int,tag.contents)) print lst
但如果我没有记错的话,我现在每个号码都有一个子列表。 因此,
sum(lst)
不起作用。 我该如何合并子列表或将数字放入单个列表中以开始? 谢谢!I want to get the sum of numbers from a website. I do this:
lst = list() url = raw_input('Enter - ') html = urllib.urlopen(url).read() soup = BeautifulSoup(html) tags = soup('span') for tag in tags: lst.append(map(int,tag.contents)) print lst
But if I am not mistaken, I now have a sublist for every number. Thus,
sum(lst)
doesn't work. How can I either merge the sublists or put the numbers in a single list to start with? Thanks!
原文:https://stackoverflow.com/questions/34711458
更新时间:2023-01-14 20:01