Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/329.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vue.js/6.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 - Fatal编程技术网

Python 查找字典的多个匹配项?

Python 查找字典的多个匹配项?,python,Python,我试图用最简单的方法来计算x字典的所有出现次数,但没有结果。 我不能导入任何库,也不能使用除:for var in、if x、if not、if is以外的其他语句 脚本的结果应如下所示: 例如: @编辑 已更正的文本正常,不使用任何库或函数: >>> l = [1,2,1,'a',4,2,3,3,5,'a',2] >>> pos = {} >>> for i, n in enumerate(l): ... pos.setdefaul

我试图用最简单的方法来计算x字典的所有出现次数,但没有结果。 我不能导入任何库,也不能使用除:for var in、if x、if not、if is以外的其他语句

脚本的结果应如下所示: 例如:

@编辑
已更正的文本

正常,不使用任何库或函数:

>>> l = [1,2,1,'a',4,2,3,3,5,'a',2]
>>> pos = {}
>>> for i, n in enumerate(l):
...     pos.setdefault(n, []).append(i)
... 
>>> pos
{'a': [3, 9], 1: [0, 2], 2: [1, 5, 10], 3: [6, 7], 4: [4], 5: [8]}
>>> for k, v in pos.items():
...     print "%s = %s" % (k, v)
... 
a = [3, 9]
1 = [0, 2]
2 = [1, 5, 10]
3 = [6, 7]
4 = [4]
5 = [8]
lis = [1,2,1,'a',4,2,3,3,5,'a',2]
dic={}
index=0
for item in lis:
    if item in dic:
        dic[item]+=[index]
        index+=1
    else:
        dic[item]=[index] 
        index+=1
print dic   
输出:

{'a': [3, 9], 1: [0, 2], 2: [1, 5, 10], 3: [6, 7], 4: [4], 5: [8]}

好的,不使用任何库或函数:

lis = [1,2,1,'a',4,2,3,3,5,'a',2]
dic={}
index=0
for item in lis:
    if item in dic:
        dic[item]+=[index]
        index+=1
    else:
        dic[item]=[index] 
        index+=1
print dic   
输出:

{'a': [3, 9], 1: [0, 2], 2: [1, 5, 10], 3: [6, 7], 4: [4], 5: [8]}

另外,请澄清您的问题-什么是CICLE?我的意思是“如果陈述”和“对于陈述”同样,请澄清您的问题-什么是CICLE?我的意思是“如果陈述”和“对于陈述”为什么在这两种情况下都使用2x索引+=1@只在“if”中编辑外接程序,在“else”中删除枚举比手动计算
索引
更方便。我尝试使用str.index(),但它只返回第一个索引,而忽略了所有其他索引。。。请给我看一个enumerate的例子好吗?@user1768615请看我这个问题的答案()。@AlexeyKachayev我知道
enumerate()
的作用,我只是想展示一个不使用任何函数或库的解决方案。为什么在这两种情况下都使用2x index+=1@只在“if”中编辑外接程序,在“else”中删除枚举比手动计算
索引
更方便。我尝试使用str.index(),但它只返回第一个索引,而忽略了所有其他索引。。。请给我看一个enumerate的例子好吗?@user1768615查看我对这个问题的答案()。@AlexeyKachayev我知道
enumerate()
的作用,我只是想展示一个不使用任何函数或库的解决方案。这个问题的解决方法很好。你能解释一下我使用2x变量“i,n”的原因吗?@user1768615
enumerate
将返回带元组的迭代器
(位置,元素)
[(0,1),(1,2),(2,1),…])
i,n
语法只是一个多重赋值:位置到
i
,元素到
n
。使用range()代替enumerate不是更简单吗?@user1768615肯定不是<代码>范围将创建单独的列表,并为此分配内存
enumerate
iterator是专门为这种情况创建的,所以我不认为使用它有任何缺点。非常感谢,但非常感谢=)pfff,我不能给出2个正确答案=/这个问题的解决方法很好。你能解释一下我使用2x变量“i,n”的原因吗?@user1768615
enumerate
将返回带元组的迭代器
(位置,元素)
[(0,1),(1,2),(2,1),…])
i,n
语法只是一个多重赋值:位置到
i
,元素到
n
。使用range()代替enumerate不是更简单吗?@user1768615肯定不是<代码>范围将创建单独的列表,并为此分配内存
enumerate
iterator是专门为这种情况创建的,所以我不认为使用它有任何缺点。非常感谢,但非常=)pfff,我不能给出2个正确的答案=/