Python 如果键为';在列表中找不到?

Python 如果键为';在列表中找不到?,python,python-3.x,pandas,parsing,word-frequency,Python,Python 3.x,Pandas,Parsing,Word Frequency,我得到的结果是: for url in urls: uClient = ureq(url) page_html = uClient.read() uClient.close() soup = BeautifulSoup(page_html, "html.parser") text = (''.join(s.findAll(text=True))for s in soup.fin

我得到的结果是:

for url in urls:
            uClient = ureq(url)
            page_html = uClient.read()
            uClient.close()
            soup = BeautifulSoup(page_html, "html.parser")
            text = (''.join(s.findAll(text=True))for s in soup.findAll('p'))
            c = Counter((re.sub(r"[^a-zA-Z0-9 ]","",x)).strip(punctuation).lower() for y in text for x in y.split())
            for key in sorted(c.keys()):
                l.append([key, c[key]])

        d = collections.defaultdict(list)
        for k, v in l:
            d[k].append(v)

        print(d.items())
如果在列表中找不到键,我希望默认值为0。例如,如果Key:g在第一个列表中是1次,在第二个列表中是0次,在第三个列表中是3次,在第四个列表中是6次。它应该返回:“g”:[1,0,3,6]

编辑:

这是我完整代码中的注释行,用于显示未成功的试验:

([('', [3, 9, 4, 1]), ('1', [1, 2, 2]), ('1960', [1]), ('1974', [1]), ('1996', [1]), ('1997', [1]), ('1998', [1]), ('2001', [2]), ('2002', [1]), ...
我有一个名为“urls.txt”的文本文件,其中包含URL:

        #m = list(map(dict, map(zip, list_1, list_2)))    
        #matrix = pd.DataFrame.from_dict(d, orient='index')
        matrix = pd.DataFrame({ key:pd.Series(value) for key, value in d.items() })
我需要一个所有唯一字母数字的文档术语矩阵。比如说数据和科学:
一行应该是
[文件编号,术语“数据”,术语“科学”]

它应该看起来像:

https://en.wikipedia.org/wiki/Data_science
https://datajobs.com/what-is-data-science

我很接近,但不能用正确的方式。尝试了从列表到数据帧,从dict到数据帧,纯粹是通过数据帧,但没有任何效果。到处找,找不到类似的东西

我在回答我自己的问题,因为我可以找到一种方法,并在这里发布,以防有人需要帮助:

   data   science
1  96      65
2  105     22
3  0       16

用'l=[0,0,0,0]`来初始化你的
列表
对象,而不是附加use
l[i]=c[key]
。你能进一步解释一下吗?我将如何改变?你的问题和提供。删除
url
部分并循环3句文字。编辑代码。我现在举个例子。谢谢你的邀请suggestion@beta发布完整的代码与最简单的示例相反。。
import requests
from bs4 import BeautifulSoup
import collections
from string import punctuation
from urllib.request import urlopen as ureq
import re
import pandas as pd
import numpy as np
import operator
Q1= open ("Q1.txt", "w") 
def web_parsing(filename):
    with open (filename, "r") as df:
        urls = df.readlines()
        url_number = 0 
        url_count = []
        l = {} 
        d = []
        a =[]
        b = []
        e=[]
        for url in urls:
            uClient = ureq(url)
            page_html = uClient.read()
            uClient.close()
            soup = BeautifulSoup(page_html, "html.parser")
            text = (''.join(s.findAll(text=True))for s in soup.findAll('p'))
            c = Counter((re.sub(r"[^a-zA-Z0-9 ]","",x)).strip(punctuation).lower() for y in text for x in y.split())
            for key in c.keys():
                if key in a:
                    continue
                else:
                    a.append(key)
            #print(sorted(a))
            a = list(filter(None, a))
            #print(sorted(a))
            stopfile = open('stop_words.txt', 'r')
            stopwords = [line.split(',') for line in stopfile.readlines()]
            #print(stopwords)
            a = [item for item in a if item not in stopwords]
            #print(len(a))
            l = [list(([word, c[word]])) for word in a]
            l =sorted(l)
            flat_list = [item for sublist in l for item in sublist]
            d.extend(flat_list)
            b = {d[i]: d[i+1] for i in range(0, len(d), 2)}
            e.append(b)
        j=0
        for url in urls:
            j = j+1
        #print(j)
        result = {}
        for key in a:
            for i in range(0,j):
                if key in e[i]: result.setdefault(key, []).append(e[i][key])
                if key not in e[i]: result.setdefault(key, []).append(0)
            #print (result)
            #print (result)
        od = collections.OrderedDict(sorted(result.items()))
        #print(od)
        df1 = pd.DataFrame(od)
        df2 =df1.loc[:, ['data', 'companies', 'business', 'action', 'mining', 'science']]
        #return(df2)
        df1.to_csv(Q1, header=True)
        df2.to_csv(Q1,  header=True)        
        print(len(a))
        return(df1)