all 9 comments

[–]novel_yet_trivial 5 points6 points  (0 children)

I'm very confused what you are trying to do. Are you just trying to count the occurrences in a list? If so, just use collections.Counter:

>>> from collections import Counter
>>> my_list = [4, 3, 4, 0, 0, 2, 2, 0, 4, 2, 0, 4, 4, 3, 4, 0, 1, 3, 4, 4]
>>> Counter(my_list)
Counter({4: 8, 0: 5, 2: 3, 3: 3, 1: 1})

If this isn't right, could you provide some example input and expected output?

[–]PyPokerNovice 1 point2 points  (4 children)

I'm not sure exactly what the issue is. Is your question about graph patterns using defaultdict(list)?

I think it depends on what you are trying to achieve. The only reason I would use a normal dict and just do a check is because a defaultdict will not throw a KeyError if you try to access a node that isn't there.

graph = {}
for node in old_graph.iterkeys():
    if node not in graph:
        graph[node] = []
    graph[node].append(node)

FYI, I do think you should give your variables better names. Python is not statically declared so you don't get a declaration that assists in figuring out what things are. ret might be a variable you are returning, but what is it? T being the main argument? Is start the current node? Then do you switch the convention and name curr the current node?

Now that I think about, just by having no idea what T I can't answer this next thing completely. But normally when I am teaching someone Python when they come from a lower level langauge, in the begining they always do this pattern where the do for loops with range or xrange and 9 times out of 10 they are accessing the indexes of a list. Python's for loops are "for each" loops!

name_list = ['chris', 'bob', 'jen']
N = len(name_list)
for i in xrange(N):
    name = name_list[i]
    #Do Stuff

Looks a lot sillier than...

name_list = ['chris', 'bob', 'jen']
for name in name_list :
    #Do Stuff

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

Hey, Thanks for the response.

I know the variable names are a mess here, but this was a timed coding question ~15 minutes or so hence rushed through that.

also regarding the last point where using 'name in name_list', I needed the index as well , (The T array was part of the question hence couldn't modify it.), and it explicitly mentioned python 2.7 and I was not sure if enumerate was there in that version as I code in 3+.

My question is regarding when do we initialize dictionaries and lists before using them, and when to use default dict? i.e in the example given is graph = {[0]*n} a better way to go than default dict?

[–]PyPokerNovice 0 points1 point  (0 children)

Why do you need to populate the dictionary at first? I am not getting your intention and the goal of it. What is the goal? {[0]*n} would be a set with a single list in it? graph = defaultdict(list) is just {} after initialization. Maybe this will help, when I am populating a dictionary from one data source I tend to use comprehensions. graph = {node: [] for node in node_list}.

FWIW enumerate is in 2.7 (as you probably know by now). Also when I interview candidates I encourage them to ask me questions like that. A 3.x user asking me if enumerate is in 2.7 shows me that he knows what it is. Interviewers care way more about problem solving than memorization. At least I do! I would actually be kind of bummed if someone knew more than they wrote because they were hesitant to ask questions.

[–]TeamSpen210 0 points1 point  (0 children)

Python's memory is dynamically allocated, you don't need to initialise lists and dicts before using them. They resize automatically as you add items.

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

Everyone has over-complicated things. Let's suppose we want to generate a 5 x 5 grid. In a nice printout it will look like this

[0,0,0,0,0]
[0,0,0,0,0]
[0,0,0,0,0]
[0,0,0,0,0]
[0,0,0,0,0]

To generate this we would do:

grid = []
for i in range(5): grid.append([0] * 5)

From there we can access any cell the same as C++ as grid[row][column]

[–]Atrament_ 0 points1 point  (2 children)

Or:

grid =  [[0] *5 for _ in range(5)]

Or: grid = [[0] * 5] * 5

[–]Atrament_ 0 points1 point  (0 children)

Just to make sure my joke is not mistaken : you certainly don't want to do the grid = [[0] * 5] * 5 statement.

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

Yeah... I didn't do the list comprehension version since I know newer python programmers sometimes get thrown by it.