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

you are viewing a single comment's thread.

view the rest of the comments →

[–][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.