Are there dictionary comprehensions in Python? (Problem with function returning dict)
2014-01-20 10:14
579 查看
I know about list comprehensions, what about dictionary comprehensions?
Expected Output:
Code (I'm a beginner):
What is the problem with this code? Why do I get this
python dictionary python-2.x
edit: As agf pointed out in comments and the other answer, there is a dictionary comprehension for Python 2.7 or newer.
There is no need to convert
a list or sort it before turning it into a set since strings are iterable:
There is no dictionary comprehension with for Python 2.6 and below, which could be why you are seeing the syntax error. The alternative is to create a list of key-value tuples using a comprehension or generator and passing that into the
If you're on Python 2.7 or newer:
works fine. You don't need to sort the list before you set it. You also don't need to turn the word into a list. Also, you're on a new enough Python to use
If you're on an older version of Python, you can't use
you need to use a generator expression with the
This still requires you to iterate over
so try something like:
Expected Output:
>>> countChar('google') {'e': 1, 'g': 2, 'l': 1, 'o': 2} >>> countLetters('apple') {'a': 1, 'e': 1, 'l': 1, 'p': 2} >>> countLetters('') {}
Code (I'm a beginner):
def countChar(word): l = [] #get a list from word for c in word: l.append(c) sortedList = sorted(l) uniqueSet = set(sortedList) return {item:word.count(item) for item in uniqueSet }
What is the problem with this code? Why do I get this
SyntaxError?
return { item:word.count(item) for item in uniqueSet } ^ SyntaxError: invalid syntax
python dictionary python-2.x
edit: As agf pointed out in comments and the other answer, there is a dictionary comprehension for Python 2.7 or newer.
def countChar(word): return dict((item, word.count(item)) for item in set(word)) >>> countChar('google') {'e': 1, 'g': 2, 'o': 2, 'l': 1} >>> countChar('apple') {'a': 1, 'p': 2, 'e': 1, 'l': 1}
There is no need to convert
wordto
a list or sort it before turning it into a set since strings are iterable:
>>> set('google') set(['e', 'o', 'g', 'l'])
There is no dictionary comprehension with for Python 2.6 and below, which could be why you are seeing the syntax error. The alternative is to create a list of key-value tuples using a comprehension or generator and passing that into the
dict()built-in.
If you're on Python 2.7 or newer:
{item: word.count(item) for item in set(word)}
works fine. You don't need to sort the list before you set it. You also don't need to turn the word into a list. Also, you're on a new enough Python to use
collections.Counter(word)instead.
If you're on an older version of Python, you can't use
dictcomprehensions,
you need to use a generator expression with the
dictconstructor:
dict((item, word.count(item)) for item in set(word))
This still requires you to iterate over
word
len(set(word))times,
so try something like:
from collections import defaultdict def Counter(iterable): frequencies = defaultdict(int) for item in iterable: frequencies[item] += 1 return frequencies
相关文章推荐
- Python学习笔记--大纲
- python one-line list comprehension: if-else variants
- 【python】初识python的问题
- Python安装时import matplotlib.pyplot as plt报错
- 初试PyOpenGL一 (Python+OpenGL)
- Python-Project Euler 48
- Python的包管理工具Pip
- python中getattr函数使用方法 getattr实现工厂模式
- python逐行读取文件内容的三种方法
- Python 用 os.walk 遍历目录
- python总结链接
- Python中的内置的模块 和第三方的模块
- python中的generator高级用法 -- send
- Python中第三方的库(library)、模块(module),包(package)的安装方法
- python操作excel
- 如何在windows下的Python开发工具IDLE里安装其他模块?
- Python tricks(1) -- 动态定义一个新变量
- Python-Project Euler 46
- python学习小结
- Python学习系列五:模块