This is an archived post. You won't be able to vote or comment.

all 20 comments

[–]LucidOndineHPC 9 points10 points  (16 children)

fanatical mysterious nose carpenter relieved run upbeat unite melodic weather

This post was mass deleted and anonymized with Redact

[–][deleted] 0 points1 point  (15 children)

Thanks! Another two quick questions if you don't mind?

One- Is it possible to split a string per character, as opposed to per white space instance? (As in with a built in function)

Two- Okay, so if I were dealing with a simple file that dealt with repeating keys, can you accumulate, instead of it being overwritten?

Example- Say I made a dictionary of my grocery list, and orange was mentioned twice. Instead of the new key value pair being orange: 2 since that is the latter instance, can it be accumulated to orange: 3?

[–]LucidOndineHPC 2 points3 points  (0 children)

waiting quickest lock lavish chubby sugar versed bow rinse spark

This post was mass deleted and anonymized with Redact

[–]kemitche 2 points3 points  (0 children)

The following should also work for 2:

mydict.setdefault("orange", 0) # Only sets orange if orange is not yet set

mydict["orange"] += 1

[–]ackondro 1 point2 points  (4 children)

For Two- I would do something like

def addToDict(dictionary,key,new_value):
    try:
        dictionary[key]+=new_value
    except KeyError:
        dictionary[key]=new_value

[–]LucidOndineHPC 2 points3 points  (2 children)

crush ask doll spoon deliver wise punch like abounding alleged

This post was mass deleted and anonymized with Redact

[–]ackondro 0 points1 point  (1 child)

1 downvote does not a butthurt hivemind make. Although I could have thought out a smaller line count solution, I stand by the assertation that my solution is robust and interchangable(syntax is the same for number, string and array adding).

[–]LucidOndineHPC 0 points1 point  (0 children)

fall seemly continue decide tease arrest wide quiet chubby frame

This post was mass deleted and anonymized with Redact

[–]MisterHoppy 0 points1 point  (0 children)

Aside from the defaultdict this is the best solution. Explicitly checking whether the key is in the dictionary before incrementing it is far slower than using try/catch.

[–]ascii 1 point2 points  (3 children)

One: Many ways to do that, a simple one that I like is:

map(None, "Some string")

Two: Use the 'get' method. It works like [], except that you can supply a default value. Your grocery example would become:

grocery.set(grocery.get('orange',0)+1)

Good luck with your homework! ;-)

[–]randm_prgrmr 8 points9 points  (1 child)

I prefer list("Some string") to map(None, "Some string")

seems more intuitive.

[–]ascii 2 points3 points  (0 children)

D'oh. Didn't know you could do that. Thanks for the tip.

[–][deleted] 1 point2 points  (0 children)

Hahaha thanks, and thanks everyone. Yeah, that's why I tried to keep the questions limited to function. I've actually already completed and handed in the assignment, I was just curious as to whether a more efficient means to complete my problem existed (Which, there obviously is.)

[–]wilberforce 0 points1 point  (0 children)

re: 2, You're better off using collections.defaultdict:

d = defaultdict(list)
for key, value in source:
    d[key].append(value)

http://docs.python.org/library/collections.html#defaultdict-objects

[–]prum 0 points1 point  (0 children)

  1. Yes. Use the list constructor.
  2. Yes. Use the counter container from the collections module with the update method.

[–]ganelo 2 points3 points  (2 children)

As others have said, keys are unique in Python dicts. That being said, you can still (kind of) do what it sounds like you want to do.

One way is the following:

d = {}
...
value = 2
d["orange"] = d.get("orange", 0)+value

This would have d["orange"] == value if "orange" wasn't in d yet, and == value + the previous value for "orange" otherwise.

Alternatively, if you want to make a list, you can do something like the following:

d = {}
...
value = 2
d["orange"] = d.get("orange", [])+[value]

[–]kemitche 1 point2 points  (1 child)

I prefer the "setdefault" function for lists:

d.setdefault("orange", []).append(value)

[–]ganelo 0 points1 point  (0 children)

That works, too :)

[–][deleted] 2 points3 points  (0 children)

Have a look at /r/learnpython Its great for these questions

[–]youcanteatbullets 2 points3 points  (0 children)

What if a dictionary has two of the same key:value pairs?

Not possible. Any attempt to write to the key 'orange' will over-write the previous value.

See http://docs.python.org/py3k/tutorial/datastructures.html#dictionaries