I recently read the questions that recommend against using switch-case statements in languages that do support it. As far as Python goes, I've seen a number of switch case replacements, such ... |
I'm curious how the Python Ninjas around here would do the following, elegantly and pythonically:
I've got a data structure that's a dict from unicode strings to dicts from unicode strings to ... |
I would like to create a list like this
list = []
for i in range(150):
list.append({'open': False, 'serve': False})
But is there a better way in Python to do ... |
I'd like my dictionary to be case insensitive.
I have this example code:
text = "practice changing the color"
words = {'color': 'colour',
'practice': 'practise'}
def replace(words,text):
...
|
I've recently learned that python doesn't have the switch/case statement. I've been reading about using dictionaries in its stead, like this for example:
values = {
value1: ...
|
def sortProfiles(p):
return sorted(p, key=itemgetter('first_name'))
I have a list with dictionaries. This function allows me to sort them by their first_name. However, it's case-sensitive.
|
I can use map to implement the case insensitive list search with Python.
a = ['xyz', 'wMa', 'Pma'];
b = map(string.lower, a)
if 'Xyz'.lower() in b:
print 'yes'
How can I do ... |
|
i have implemented a function:
def postback(i,user,tval):
"""functie ce posteaza raspunsul bazei de date;stringul din mesaj tb sa fie mai mic de 140 de caractere"""
result ... |
The addition of collections.defaultdict in Python 2.5 greatly reduced the need for dict's setdefault method. This question is for our collective education:
- What is
setdefault still useful for, today in Python 2.6/2.7?
- What ...
|
Consider:
categories = {'foo':[4], 'mer':[2, 9, 0]}
key = 'bar'
value = 5
We could safely append to a list stored in a dictionary in either of the following ways:
- Being cautious, we always check whether ...
|
I'm using a dictionary's get() method to choose a case based on a variable key value. Problem is, it keeps choosing the wrong case for the key. I'm not sure if ... |
I plan to build dictionary with keys of all alphabets. When user input a sentence or paragraph, each word gets checked, and depending on starting letter, it will be added to ... |
I'm new to python and I really like concept of using dictionaries instead of switch/case statements, but there is one problem I can't figure out
Let's say we have a 'pythonic case' ... |
Example:
>>> d = {'answer':1, 'Question':2}
>>> for i, j in sorted(d.items()): print i
Question
answer
I would like case insensitive list:
answer
Question
and I believe it can be done in simple Pytonic way
|
|
#!/usr/bin/python import sys if len(sys.argv) != 2: sys.exit() def handle_one(): return 'one' def handle_two(): return 'two' def handle_three(): return 'three' def handle_default(): return 'unknown' cases = { '1': handle_one, '2': handle_two, ... |
#! usr/bin/env python # phonebook.py # Listings (Dictionary) listings = {'Adam': '444-444-4440', 'Dave': '333-222-0203'} # Define Function def phonebook(): who = raw_input('Whose number would you like? ') if listings.has_key(who): return listings[who] print phonebook() |