1

I have an array

ar= [2, 3, 45 , 5556, 6, 'empty', 4]

I'd like to normalize this array in order to plot it later.

0:2
1:3
2:45
3: 5556
4: 6
5: 0  # not "empty" anymore
6: 4

newAr = {if (ar[i] != 'empty') ar[i] : i for i in range(len(ar))
         else: ar[i] = 0 }

I tried this way, How can I normilize this array with condition of converting every 'empty' element with zero.

UPDATE The main goal is to normalize this array. Axe X: for index, Axe Y for the value.

what if we have duplicates, how can we make sure that we have the right references.

 ar= [2, 3, 45, 4 , 5556, 6, 'empty', 4]

  0:2
    1:3
    2:45
    3: 5556
    4: 6
    5: 0  # not "empty" anymore
    6: 4

2 Answers2

4

This works:

ar = [2, 3, 45, 5556, 6, 'empty', 4]
new_ar = [0 if x == 'empty' else x for x in ar]

yields:

[2, 3, 45, 5556, 6, 0, 4]

I used the Python ternary operator at the front of the list comprehension, instead of after.

Edit: If you need a set, as per comment, then simply use {} instead of [] in your comprehension:

new_ar = {0 if x == 'empty' else x for x in ar}

This will automatically ensure unique values only.

Community
  • 1
  • 1
supermitch
  • 2,022
  • 4
  • 21
  • 26
  • Thanks! How can we hash them in case we have duplicates, for example (we have 4 two times, the main goal is to plot these data. Axe X: is for the array index and the Axe Y for ar[i]) I shouldn't have duplicates . – user3047512 Nov 29 '13 at 00:44
  • Just turn your list into a set: `set(new_ar)` will remove duplicates. I'll add that to my answer. http://docs.python.org/2/library/stdtypes.html#set – supermitch Nov 29 '13 at 00:45
  • In this case I need to get the new_ar as a list: Does this work new_ar = {0 if x == 'empty' else x for x in list(set(ar))} – user3047512 Nov 29 '13 at 00:49
  • 1
    Turning a `list` into a `set` would kill the ordering. Since @user3047512 wants to plot this data, I'm leaning more towards `dict`s or such. Still, the question about handling conflicts remains – inspectorG4dget Nov 29 '13 at 00:50
  • @user3047512: `list(set(ar))` is pointless in the expression that you've written. The more pressing concern is preserving the ordering of data, so that the plot stays true to the original indices – inspectorG4dget Nov 29 '13 at 00:51
  • 1
    You could use `OrderedDict` and simply write out the long form of the comprehension, if you wanted to preserve order. There's no *need* for it to be a one-liner. http://docs.python.org/2/library/collections.html#collections.OrderedDict – supermitch Nov 29 '13 at 00:52
  • I though about Dictionaries, hashing will make values unique and can get them easily. I am not familiar with using Dictionaries/hashing on python. Can you give a hint – user3047512 Nov 29 '13 at 00:53
  • The ordering is not important. – user3047512 Nov 29 '13 at 00:54
  • @user3047512: I can't imagine a plot with ordering being unimportant. In either case, check out the edit in my post. I've figured out what I needed to know from your edited post and updated my answer appropriately – inspectorG4dget Nov 29 '13 at 00:57
  • @user3047512 if ordering is unimportant, the set comprehension will work fine, see last line. – supermitch Nov 29 '13 at 00:59
1
In [91]: ar = [2, 3, 45, 5556, 6, 'empty', 4]

In [92]: [i if not isinstance(i, str) else 0 for i in ar]
Out[92]: [2, 3, 45, 5556, 6, 0, 4]

OR

In [93]: [i if i!='empty' else 0 for i in ar]
Out[93]: [2, 3, 45, 5556, 6, 0, 4]

Based on your updated post, this should handle the appropriate removal of duplicates:

In [105]: d = {n if n!='empty' else 0:i for i,n in enumerate(ar)}

In [106]: newList = [None]*len(d)

In [107]: for n,i in d.iteritems(): newList[i] = n

In [108]: newList
Out[108]: [2, 3, 45, 5556, 6, 0, 4]
inspectorG4dget
  • 97,394
  • 22
  • 128
  • 222