Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么';python字典不能像我期望的那样工作吗?_Python_Python 3.x_Dictionary_Counting - Fatal编程技术网

为什么';python字典不能像我期望的那样工作吗?

为什么';python字典不能像我期望的那样工作吗?,python,python-3.x,dictionary,counting,Python,Python 3.x,Dictionary,Counting,我需要将n个文本文件传递给程序并获得输出: #!/usr/bin/env python3.7 import sys def main(): list_placeholder = [0 for _ in range(1, len(sys.argv))] dict_file_content = {} for arg_file in range(1, len(sys.argv)): content = '' with open(sys.arg

我需要将n个文本文件传递给程序并获得输出:

#!/usr/bin/env python3.7
import sys

def main():
    list_placeholder = [0 for _ in range(1, len(sys.argv))]
    dict_file_content = {}

    for arg_file in range(1, len(sys.argv)):
        content = ''
        with open(sys.argv[arg_file]) as f:
            for line in f:
                content += line
            dict_file_content[arg_file] = content

    dict_symbols = {}

    for key in dict_file_content:
        content = dict_file_content[key]
        for i in content:
            if dict_symbols.get(i) == None:
                dict_symbols[i] = list_placeholder

    for key in dict_file_content:
        content = dict_file_content[key]
        for key_symbol in dict_symbols:
            dict_symbols[key_symbol][key-1] = content.count(key_symbol)
    print(dict_symbols)

if __name__ == "__main__":
    main()

其中,字典键是唯一字符,列表是该字符在文本中出现的次数,列表[0]=第一个文本中出现的次数,列表[1]=第二个文本中出现的次数,等等。

您的
dict\u符号
引用了相同的列表。您需要为每个条目提供自己的列表:

{'A': [12, 3], 'm': [8, 15], 'a': [19, 23], 'z': [7, 6], 'i': [5, 4] , ...}

请注意
列表\u占位符[:]
复制一份。

您的输入和输出是什么?欢迎使用堆栈溢出。请阅读这些资料。计数.py文本1.1.1.1.1.1.1.1.1.1.1.1.1.2、'm’:[0、2]、'A’:[0、0、2]、'0、0、0、5.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.3请请请请请请请请请请请请阅读.请请请请请请请阅读.计数.计算计算.中点点点.1.1.1.1.1.3.1.1.1.1.1.1.1.1.1.1.3.3.2.3.3.3.3.3.1.1.1.1.1.1.1.3.1.1.1.1.2.3.3.3.3.3.3.3.3.3.3.3.3.1.3请请请请请请请"s:[0,2],":[0,2],"s:[0,2],"o:[0,2],"k:[0,2]“c’:[0,2,”c’:[0,2],“f’:[0,2],“G’:[0,2],“u’:[0,2],“p’:[0,2],“p’:[0,2],“c’:[0,2],“f’:[0,0,2],“G’:[0,2],“u’:[0,2,”u’:[0,2,”0,2],“p’:[0,2],“p’,[0,0,2],[0,0,2],“p’,“p’,,[0,0,2],,[0,0,2],,,,,,“\p’,[0,0,2],,,,,,,,,,,,,“\\\\\n’,,,,,[0,[0,2,[0,2,[0,2,,,,“0”:[0,2],顺便说一句,将该注释移动到问题中,以便可以很好地格式化它。您可以使用
.items()在字典中进行迭代
,而不是迭代键并获取每个值。不要将
==
None
进行比较,而是使用
is
。看起来您试图将文件的所有内容都转换成字符串。如果是这样,只需执行
f.read()
。我也不明白为什么要从1开始第一个
范围()。
    for key in dict_file_content:
        content = dict_file_content[key]
        for i in content:
            if dict_symbols.get(i) == None:
                dict_symbols[i] = list_placeholder[:]