You are close. Try:
dict.fromkeys(my_csv_dict.keys(),[])
This will initialize a dictionary with the same keys that you parsed from your CSV file, and each one will map to an empty list (to which, I assume, you will append your suspect row values).
Try this. (There are several subtler changes here that are all necessary, like how you can't initialize unsure_rows before you start reading the CSV.)
unsure_rows = None
for row in csv_reader:
# if row['Start Time'] != 'None':
try:
if before_date > strptime(row['Start Time'], '%Y-%m-%d %H:%M:%S') > after_date:
continue
except ValueError:
if not unsure_rows:
# Initialize the unsure rows dictionary
unsure_rows = dict.fromkeys(csv_reader.fieldnames,[])
for key in unsure_rows:
unsure_rows[key].append(row[key])
Answer from cheeken on Stack OverflowYou are close. Try:
dict.fromkeys(my_csv_dict.keys(),[])
This will initialize a dictionary with the same keys that you parsed from your CSV file, and each one will map to an empty list (to which, I assume, you will append your suspect row values).
Try this. (There are several subtler changes here that are all necessary, like how you can't initialize unsure_rows before you start reading the CSV.)
unsure_rows = None
for row in csv_reader:
# if row['Start Time'] != 'None':
try:
if before_date > strptime(row['Start Time'], '%Y-%m-%d %H:%M:%S') > after_date:
continue
except ValueError:
if not unsure_rows:
# Initialize the unsure rows dictionary
unsure_rows = dict.fromkeys(csv_reader.fieldnames,[])
for key in unsure_rows:
unsure_rows[key].append(row[key])
Have you tried:
newd = dict.fromkeys(origdict)
? If that doesn't work for you then please add more details about the error you are getting.
python - Copying a key/value from one dictionary into another - Stack Overflow
How to copy one key pair value from one dictionary to another in python - Stack Overflow
Python copy specific key/value from one dictionary to another - Stack Overflow
python - Moving a single key value pair from one dictionary to another - Stack Overflow
you want to use the dict.update method:
d1 = {'UID': 'A12B4', 'name': 'John', 'email': 'hi@example.com'}
d2 = {'UID': 'A12B4', 'other_thing': 'cats'}
d1.update(d2)
Outputs:
{'email': 'hi@example.com', 'other_thing': 'cats', 'UID': 'A12B4', 'name': 'John'}
From the Docs:
Update the dictionary with the key/value pairs from other, overwriting existing keys. Return None.
If you want to join dictionaries, there's a great built-in function you can call, called update.
Specifically:
test = {'A': 1}
test.update({'B': 2})
test
>>> {'A':1, 'B':2}
You can try this:
new_dict = {x:mydict[x] for x in mydict if x in ('color','fruit')}
the easiest way is this:
new_dict["color"] = mydict["color"]
however, if you had a larger list of items you need to pass to a new dict, you could use the following:
items = ["color", "fruit"]
for item in items:
new_dict[item] = mydict[item]
products = {"Pencil": 1, "Notebook": 2, "Backpack": 3, "Pens": 2, "Markers": 5, "Whiteboard": 30}
cart = {}
def addToCart():
try:
productToAdd = input("What would you like to add? ")
cart[productToAdd]=products[productToAdd]
except KeyError:
print("No such product")
Note that python is case sensitive 'pencil' and 'Pencil' are not the same. If you are sure the products are meant to be Capital-letter first you can use
productToAdd = input("What would you like to add? ").title()
Assuming that user will choose items from that list(particularly from keys) then:
products = {"Pencil": 1, "Notebook": 2, "Backpack": 3, "Pens": 2, "Markers": 5,
"Whiteboard": 30}
cart = {}
def addToCart():
productToAdd = input("What would you like to add? ")
if not productToAdd.title() in products: # for removing caps confusion
print('No such item')
else: cart[productToAdd] = products[productToAdd]
>>> addToCart()
What would you like to add? Pencil
>>> cart
{'Pencil': 1}
Question's in the subject. The below is complete waffle.
--
Started learning Python recently. Im a digital compositor by trade (using nuke) Python seems to be the next logical step.
Let me start by saying I know nothing. And I'd prefer being pointed in the right direction rather than 'here's the answer'.
Below is my shitty, hacked to death code to try and make something similar to blackjack/21.
http://pastebin.com/RbkqJ0bc
Edit --
Turns out I was massively over complicating the whole thing. Lists seem to do what I want them to do.
http://pastebin.com/vhre3RdU
Using dict comprehension:
>>> d = {'a':'aaa','b':'bbb','c':'ccc'}
>>> newdict = {key:d[key] for key in ['a', 'b']}
>>> newdict
{'a': 'aaa', 'b': 'bbb'}
Side note: Don't use dict as a variable name. It shadows builtin dict function.
Avoiding the dict comprehension which is only available in newer version of Python (2.7+) you can also do:
d = {'a':'aaa','b':'bbb','c':'ccc'}
dd = dict((k, d[k]) for k in ("a", "b"))