case « dictionary « Python Data Type Q&A

Home
Python Data Type Q&A
1.array
2.date
3.dictionary
4.Format
5.integer
6.List
7.numpy
8.regex
9.string
10.tuple
Python Data Type Q&A » dictionary » case 

1. Choosing between different switch-case replacements in Python - dictionary or if-elif-else?    stackoverflow.com

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 ...

2. Elegant, pythonic solution for forcing all keys and values to lower case in nested dictionaries of Unicode strings?    stackoverflow.com

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 ...

3. How to generate a list of 150 cases initialised with a dict in Python?    stackoverflow.com

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 ...

4. Case insensitive dictionary    stackoverflow.com

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):

  ...

5. Python dictionary instead of switch/case    stackoverflow.com

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: ...

6. How do I make this sorting case insensitive?    stackoverflow.com

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.

7. Case insensitive dictionary search with Python    stackoverflow.com

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 ...

8. "case sequence" in python implemented with dictionaries    stackoverflow.com

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 ...

9. Use cases for the 'setdefault' dict method    stackoverflow.com

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:

  1. What is setdefault still useful for, today in Python 2.6/2.7?
  2. What ...

10. In simple cases, is it better to except & recover or to avoid the exception?    stackoverflow.com

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:
  1. Being cautious, we always check whether ...

11. Using 'get' method of a dictionary to select case. Why does it keep selecting the wrong thing?    stackoverflow.com

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 ...

12. Python: Performance of Nested Dictionaries in this case    stackoverflow.com

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 ...

13. how to run block of code inside python case statement    stackoverflow.com

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' ...

14. How to iterate over case insensitive sorted dictionary items?    stackoverflow.com

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

16. hide handlers from main() scope with dictionary switch-case?    python-forum.org

#!/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, ...

17. Dictionary Case Sensitive    python-forum.org

#! 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()

java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.