tags:

views:

521

answers:

8

Going through some example code sent to me and honestly, I have no idea what language this is

def uniqify(arr):
     b = {}
     for i in arr:
         b[i] = 1
     return b.keys()

Is it Python?

I am also curious what keys() does. It's obvious it returns an array but what does it do the array that calls the function? :P

EDIT: You guys are awesome!

+5  A: 

Type it in the python interpreter. If it runs, it is python.

artificialidiot
"it looks like Python, it runs like Python, it's Python"
Longpoke
@aptlynamedposter: if he doesn't know it's Python, maybe he doesn't have the Python interpreter
John Machin
Well, personally, I wouldn't try to randomly run code copy-pasted from the Net that I don't understand (to a certain extent). E.g. you wouldn't want to test whether `:(){:|::` is a valid Unix shell script (for those who don't know what it does - you _really_ wouldn't).
Pavel Minaev
@Pavel: OK, I'm curious. What will that script do?
JUST MY correct OPINION
This is why the fine manuals exist. If you aren't reading it and don't have an interpreter then you have no business writing code.Asking absolute newbie questions here instead of experimenting seems to be a good way to be a programmer, especially for the HR depts. of startups, right?I am trying to give sound advice to the guy, but spoonfeeding fares better for free karma.
artificialidiot
If I'm reading that right, you're making a function called `:` and inside that function calling it recursively, piping the output into itself, again recursively, putting it into the background. Then you call the function. Am I close?
JUST MY correct OPINION
@Pavel Minaev: This is why 1. usermode and 2. privileges exist.
Longpoke
@JUST, yes, that's correct... basically it's a fork bomb. It will spawn processes faster than you can kill them, easily overloading and taking down a system.
Michael Aaron Safyan
@Longpoke, privileges may prevent you from wiping out system files, but they sure as hell won't prevent you from wiping out your own files (or from rendering the system inoperable by spawning an insane number of processes), so Pavel's statement is quite reasonable.
Michael Aaron Safyan
@Michael, no... normal users aren't supposed to be able to take all the CPU. Meh, it's simple to setup a sandbox to test code in. If something can break out, it's not my fault, it's the O/S / software's fault.
Longpoke
@Longpoke, if the system's down, who's fault it is doesn't matter.
Michael Aaron Safyan
@Longpoke, and even though you won't be able to take up so much CPU that the kernel can't run (you would need to be root to give the process realtime priority), it probably would take so much CPU that you wouldn't be able to open a terminal and kill it.
Michael Aaron Safyan
@Michael: sounds like your O/S is configured wrong. BTW that code doesn't run for me in a bash shell...
Longpoke
+10  A: 

Yes, it's Python. b.keys returns a list of all the keys in the dictionary b. Each item in the sequence arr becomes a key in b whose value is 1. Note that lists and tuples are typically used where arrays would be used in other languages.

Also note that arr can be any iterable object (list, tuple, set, dict, collection), which is sort of the essence of duck typing in Python.

Longpoke
So this code would effectively get rid of all duplicates in "arr"?
bobber205
@bobber205: Yes it would, because keys in a dictionary are unique. If you do `b[1] = 1`, then `b[1] = 2`, `b[1]` will be two.
Longpoke
More precisely: it leaves `arr` alone, and returns a NEW list containing the unique values in `arr`.
John Machin
It would return a list of all unique elements in "arr".
jcao219
Although it's a bad way to make something unique, take the advice of Joe Kington's and John Machin's answers.
Longpoke
@Longpoke: "lists and tuples are typically used where arrays would be used in Python"?? Perhaps you mean "lists and tuples are used in Python where arrays would be used in other languages".
John Machin
@John Machin: hahaha my bad, fixed.
Longpoke
+2  A: 

Yes, those {} are the dictionary literal, they create an empty dictionary.

Then, it iterate the array receives as argument and create as key the value of each element in the array, and as value a 1 ( which is just a random value )

Later it returns the keys of the dictionary.

For a better understanding see the output:

$python
>>> def uniqify(arr):
...      b = {}
...      for i in arr:
...          b[i] = 1
...      return b.keys()
... 
>>> uniqify(["a","a","b", "c", "c", "a", "b", "c"])
['a', 'c', 'b']

Since the dictionary only accept one value as key, consecutive additions with the same key are discarded.

OscarRyz
+8  A: 

Yes, it's Python.

b is a dict (dictionary) which is a mapping of keys to values. b.keys() returns a list of keys.

However this code is rather old fashioned. set(arr) will return a set of the unique values in arr.

John Machin
+4  A: 

Yeah, this is python.

def uniqify(arr): #this line defines a python function with a parameter
     b = {} #declare a variable with type dictionary
     for i in arr: #loop the array and get all the elements 
         b[i] = 1 #set dict b[key,value] with key got from arr 
     return b.keys() #return all the keys, actually,just the arr
xiao
"actually just the arr" ???
John Machin
+14  A: 

As Longpoke explained, it's python, using a dict get the unique items.

However, it's bad python.

list(set(arr))

Does the same thing. No need to re-invent the wheel.

Joe Kington
+1  A: 

Trying it out in the Python Interactive Shell and calling the function with a list of letters (alphabets). Added comments preceded by # for your understanding.

>>> def uniqify(arr):
         # b is an empty dictionary
...      b = {}
         # for each item (indicated by the name i) in arr
...      for i in arr:
             # in the dictionary b, the key of value i is assigned a value 1
...          b[i] = 1
         # return all the keys in the dictionary b
...      return b.keys()
...

# arr is a list of alphabets/letters
>>> arr = ['a','b','c','d','e']

# call uniqify function passing the list arr as the argument.
>>> uniqify(arr)

# the result is a list of all keys (which is same as arr)

['a', 'c', 'b', 'e', 'd']
Technofreak
for increased international understandability, s/alphabets/letters/
John Machin
+3  A: 

It's ancient Python -- it would work fine in Python 1.5.2, final release February 2001 (and possibly earlier versions too). Still runs fine in the latest and greatest versions of course (both 2.7 and 3.1), but there are better ways these days (and there have been for years) -- many have already suggested sets, no doubt best, but even if you "need" to use dicts (e.g., you've made a bet;-),

def uniqify(arr):
  return dict.fromkeys(arr).keys()

will do exactly the same thing in a faster and much more compact way. dict.fromkeys means "make a dict with the following keys (and all identical values, by default None)" and its keys method works just like in your original example.

Alex Martelli
FWIW 1.5.2 latest release was April 1999, you must be thinking of 2.0.1
John Machin
@John, oops, you're right (I knew I should have checked it rather than relying on memory!!!), thanks,
Alex Martelli