What is this syntax in Python - arrays

if there is a syntax like this in python
what exactly is it declaring defining?
I tried looking in in the python website did not seen anything like this
children = cell["CHILDREN"]

It is accessing the item in dictionary cell with key "CHILDREN" and assigning it to the variable children.
More useful references:
http://www.tutorialspoint.com/python/python_dictionary.htm
http://learnpythonthehardway.org/book/ex39.html
http://www.python-course.eu/dictionaries.php

That line refers to dictionaries, also known as associative arrays. The line of code in question assigns the value in the dictionary that associates to the key CHILDREN to the variable children.
Read more about this in a great related Stack Overflow question.

cell is a dictionary. A dictionary is an unordered set of key: value pairs. The element cell["CHILDREN"] is retrieved and stored in the variable children. More on dictionaries here:
https://docs.python.org/2/tutorial/datastructures.html#dictionaries
https://pythonspot.com/python-dictionaries/

Related

Empty Array Vs Dictionary

I am just learning Swift and found the concept of Empty Array and Empty Dictionary. I could not dig it down with the simple examples, when to choose it and what are the benefits? Your help would be appreciated.
let emptyArray = [String]()
let emptyDictionary = [String: Float]()
If the constant can be empty then why to use array?
There is nothing to "dig down". An empty thing is just a thing that is empty. Emptiness has no effect on the thingness of a thing. For example:
An empty honey jar is a jar with no honey in it.
An empty bank account is a bank account with no money in it.
An empty string is a string with no characters in it.
An empty array is an array with no elements in it.
An empty dictionary is a dictionary with no key-value pairs in it.
The emptiness of a string does not make the string less of a string; it just happens to be empty. The emptiness of a bank account does not make the bank account less of a bank account. You do not say: how do I choose between between an empty bank account and an empty string? So you do not say: how do I choose between an empty array and an empty dictionary.
So if you don't understand the difference between an array and a dictionary, that is one thing. But the use of "empty" in your question adds nothing whatever to the mix.
It just depends on what you're trying to do. These variables don't just exist in isolation. If they did, they would be useless and you should remove them.
Are you calling an API that requires an array? Well then you would pass it an array.
Are you calling an API that requires a dictionary? Well then you would pass it a dictionary.
Dictionary is an unordered structure of data. Data is accessed by a key-value.
Array is an ordered structure of data. Data is accessed by location.
dict["aKey"] = CGFloat(1)
array.append("Thursday")
The dictionary grows by adding keys and assigning value.
The array grows by appending value to the end.

change value in hash using an array of keys in ruby

i was wondering if it is possible to access an value of a hash with an array of keys as described in the post ruby use array tvalues to index nested hash of hash. My aim is not just to access this value but to change this value.
As I understood
keys.inject(hash, :fetch)
returns the value of the hash-value determined by the key-array and not it's reference. How can I accomplish to modify this value?
I know it's bad style to modify an object instead of making a copy and working with immutables but in severel cases it seems much more comfortable to do it the short way.
Thanks a lot.
Use all but the last key to get the most deeply nested Hash, then assign normally using the last key.
keys[0...-1].inject(hash, :fetch)[keys.last] = value
Ruby doesn't have references so you can't reassign the value directly. Instead you have to reassign the object pointer, which means going up one level of nesting.

Pass current object first Array as3

Is it possible to pass an array whilst placing the currentTarget object first in the array?
I've searched online and only found reference to index, indexOf etc but no resources on how to do this anywhere.
I can send the array no problem but it's always as first loaded, through selecting a different object it must be possible to ammend the array or splice the new object to the beginning but would really be grateful of any assistance as to how to achieve this.
Not 100% sure what you're asking, but you can use the Array::unShift(...args) method to insert an object at the beginning of an array.
Edit:
From your comment below, I think I get what you're saying. You can just swap the item you're looking for with the one at the beginning:
var index = array.indexOf(event.currentTarget);
array[index] = array[0];
array[0] = event.currentTarget;
or something similar to that anyways.

How to derive values from an array using the index value

I am trying to use an array in NetLogo to store my images and call the values using their index. Looks like I am getting stuck with common manner of accessing the array's value via arrayName[0].
How do I do that in NetLogo? googling doesn't seem to have the answer.
My array:
let imgArray ["easy1.png" "easy2.png" "easy3.png" "easy4.png" "easy5.png"]
I am trying to fix the image in the following manner:
clear-drawing import-drawing imgArray[1]
You're looking for item:
item 1 imgArray
Note that it's zero-indexed, so the first item is item 0 imgArray, though first imgArray is more idiomatic.
Also, arrays in NetLogo are called lists.

In Perl, how can I make a deep copy of an array? [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
What's the best way to make a deep copy of a data structure in Perl?
In my code i do:
#data_new=#data;
and then I change #data.
The problem is that #data_new always changes as well.
It's like #data_new is just a reference to what's in #data.
How do i make a copy of an array which is not a reference but a new copy of all the values?
#data is a two dimensional array, by the way.
See perlfaq4's "How do I print out or copy a recursive data structure". That is, use the dclone method from Storable.
use Storable qw(dclone);
#data_new = #{ dclone(\#data) }
The code you have will copy the contents of the list to a new list. However, if you are storing references in the list (and you have to in order to create a two-dimensional array in Perl), the references are copied, not the objects the references point to. So when you manipulate one of these referenced objects through one list, it appears as though the other list is changing, when in fact both lists just contain identical references.
You will have to make a "deep copy" of the list if you want to duplicate all referenced objects too. See this question for some ways to accomplish this.
Given your case of a two-dimensional array, this should work:
#data_new = map { [#$_] } #data;

Resources