Mac app with Core Data:
I have 3 entities:
A <-->> B <<--> C, C has a BOOLEAN atribute.
Now I want do display, in a 2 Column Table, the name of A (1st column) and a boolean value (2nd column) which based on the boolean values from C.
For example:
An Object from type A (called a) owns 3 Objects from Type B (called b1, b2, b3).
Each of these 3 Objects owns 1 Object from Type C (called c1, c2, c3).
If the boolean Attribute of one of these Objects (c1, c2, c3) is TRUE, the boolean in the 2nd column have to be true.
I have tried something like that (with a custom ArrayController Class):
[NSTableColumn bind:#"value" toObject:self withKeyPath:#"arrangedObjects.b.c.#sum.boolValue" options:nil];
The ArrayController self is in Entity Name mode with Entity Name: A.
Are you sure #sum should be after c? There are 3 objects of type b for each a right? so, "b" is the collection in your case.
From the KVC Guide:
Simple Collection Operators
Simple collection operators operate on the properties to the right of
the operator in either an array or set.
And:
#sum
The #sum operator returns the sum of the values of the property
specified by the key path to the right of the operator. Each number is
converted to a double, the sum of the values is computed, and the
total is wrapped as an instance of NSNumber and returned.
Have you tried:
#"arrangedObjects.b.#sum.c.boolValue"
Related
I have this array in Octave :
dwnSuccess(1,1)
ans =
{
[1,1] =
scalar structure containing the fields:
site = FRED
interval = d
aard = logDir log/
dwnGrootte = log/
time = 737861.64028
and I would like to formulate conditions to find cells containing e.g. logDir in the field 'aard'.
I don't find the correct syntax. Someone knows where to find or has an example with combinations of conditions. Thanks
Assuming that you need to keep a cell array of scalar structs (instead of a struct array which makes more sense if each struct has a defined set of fieldnames), then you need to iterate the cell array to get that field and then use logical indexing to create a new cell array with the structs of interest. Like so:
aards = cellfun (#getfield, cs, {"aard"}, "UniformOutput", false);
m = strcmp(aards, "logDir"); # this must match the whole string
filter_cs2 = cs(m);
If you are interested on finding whether a string is somewhere in that field, then it's just a bit more complex:
m = ! cellfun ("isempty", strfind (aards, "logDir"));
If I understood correctly your question, then suppose you have the following cell array:
a = cell();
a{1} = struct('a', 1, 'b', 'dwn', 'c', 2);
a{2} = struct('a', 2, 'b', 'notdwn', 'c', 3);
a{3} = struct('a', 3, 'b', 'dwn', 'c', 4);
a{4} = struct('a', 4, 'b', 'dwn', 'c', 5);
I think the easiest thing to do would be to first convert it to a struct array. You can do so easily via 'sequence generator' syntax, i.e.
s = [a{:}]; % collect all cell elements as a sequence, then wrap into an array
If you are in charge of this code, then I would instead just create a struct array instead of a cell array from the very beginning.
Once you have that, you can again use a 'sequence generator' syntax on the struct array, with an appropriate function that tests for equality. In your case, you could do something like this:
strcmp( {s.b}, 'dwn' )
% ans = 1 0 1 1
s.b accesses the field 'b' in each element of the struct array, returning it as a comma separated list. Wrapping this in braces causes this sequence to become a cell array. You then pass this resulting cell array of strings into strcmp, to compare each element with the string 'dwn'.
Depending on what you want to do next, you can use that logical array as an index to your struct array to isolate only the structs that contain that value etc.
Obviously this is a quick way of doing it, if you're comfortable with generating sequences in this way. If not, the general idea stands and you're welcome to iterate using traditional for loops etc.
I am trying to iterate over rows in a Pandas Dataframe using the itertuples()-method, which works quite fine for my case. Now i want to check if a specific value ('x') is in a specific tuple. I used the count() method for that, as i need to use the number of occurences of x later.
The weird part is, for some Tuples that works just fine (i.e. in my case (namedtuple[7].count('x')) + (namedtuple[8].count('x')) ), but for some (i.e. namedtuple[9].count('x')) i get an AttributeError: 'int' object has no attribute 'count'
Would appreciate your help very much!
Apparently, some columns of your DataFrame are of object type (actually a string)
and some of them are of int type (more generally - numbers).
To count occurrences of x in each row, you should:
Apply a function to each row which:
checks whether the type of the current element is str,
if it is, return count('x'),
if not, return 0 (don't attempt to look for x in a number).
So far this function returns a Series, with a number of x in each column
(separately), so to compute the total for the whole row, this Series should
be summed.
Example of working code:
Test DataFrame:
C1 C2 C3
0 axxv bxy 10
1 vx cy 20
2 vv vx 30
Code:
for ind, row in df.iterrows():
print(ind, row.apply(lambda it:
it.count('x') if type(it).__name__ == 'str' else 0).sum())
(in my opinion, iterrows is more convenient here).
The result is:
0 3
1 1
2 1
So as you can see, it is possible to count occurrences of x,
even when some columns are not strings.
I have a class that has 3 properties: Name, ID, and ParentID.
My data:
Name ID ParentID
Event A 1 1
Event B 2 1
Event C 3 1
Event D 4 2
I have everything in a List and was trying to use the OrderBy or perhaps the Sort methods. Not sure which would be better.
I need the data in the list to be ordered so that an event has it's child as the next item in the list. Any help on this would be greatly appreciated, I am doing this in VB by the way. Thanks!!
You can sort the list like this
list.Sort(Function(x, y) 2 * x.ParentID.CompareTo(y.ParentID) + _
x.ChildID.CompareTo(y.ChildID))
Explanation: I am using a lambda expression here. You can think of it as a kind of inline declaration of a function. CompareTo returns either -1, 0 or +1. A negative number means x is less than y, 0 both are equal and +1 means x is greater than y. By multiplying the first comparison by two, its sign takes precedence over the second comparison. The second has only an effect, if the first one returns 0.
The advantage of using the lists Sort method over LINQ is that the list is sorted in-place. With LINQ you would have to create a new list.
I have 3 NSArrays with:
item: amount
A: 1
B: 2
C: 3
A: 2
E: 1
F: 6
C: 5
D: 1
F: 3
After "combining" these into one, I need:
A: 3
B: 2
C: 8
D: 1
E: 1
F: 9
Do I first combine all the arrays into one and then sum and remove the duplicates?
You could use an NSCountedSet. I'm not clear on the structure of the data in your arrays, but by assuming that your B: 2 means that you have two B's in the array, then something like this would work:
NSCountedSet *set = [NSCountedSet setWithCapacity:[array1 count]+[array2 count]+[array3 count]];
[set addObjectsFromArray:array1];
[set addObjectsFromArray:array2];
[set addObjectsFromArray:array3];
// Test it out!
NSUInteger countForC = [set countForObject:objC];
// countForC == 8
Instead of using a NSArray you could try using a NSMutableDictionary where the key is inherent in the objects structure. That will allow you to iterate through each of your arrays of letters and counts then query for the value with the key, get the value and add to the value, then continue processing.
One possibility would be to use:
Use predicates to extract like sets of data (by item) into separate arrays. See Collection predicates guide
Key Value Coding to sum the value field of each of the resulting arrays (by item). See KVO collection operators.
Pop the results in whatever structure you like (NSArray or NSDictionary).
There may be performance considerations to explore. Alternatively, iterate the array, pulling out matching items in a separate NSDictionary (keyed on item) and summing as you go.
I have a situation where I need to sort arrays and preserve the current key - value pairs.
For example, this array:
(0) = 4
(1) = 3
(2) = 1
(3) = 2
Needs to sort like this
(2) = 1
(3) = 2
(1) = 3
(0) = 4
Retaining the original keys. Array.Sort(myArray) sorts into the right sequence but doesn't keep the indexes. I need a variant that does.
edit
Using the links, this seems close to what I want. Do I just need to remove the extra brackets to convert this to vb.net?
myList.Sort((firstPair,nextPair) =>
{
return firstPair.Value.CompareTo(nextPair.Value);
}
);
(also would I intergrate this as a function or something else?)
In an array, the order is determined by the indexes (what you call "keys"). Thus, there cannot be an array like this:
(2) = 1
(3) = 2
(1) = 3
(0) = 4
What you need is a data structure that has keys, values and an order (which is independent from the keys). You can use a List(Of KeyValuePair) or (if you use .net 4) List(Of Tuple(Of Integer, Integer)) for this; a few examples are shown in the link provided by Ken in the comment (which I will repeat here for convenience):
How do you sort a C# dictionary by value?
EDIT: Another option would be to use LINQ to automatically create a sorted IEnumerable(Of Tuple(Of Integer, Integer)):
Dim a() As Integer = {4, 3, 1, 2} ' This is your array
Dim tuples = a.Select(Function(value, key) New Tuple(Of Integer, Integer)(key, value))
Dim sorted = tuples.OrderBy(Function(t) t.Item2)
(untested, don't have Visual Studio available right now)
Since you are using .net 2.0 (since you said that you are using Visual Studio 2005 in one of the comments), using an OrderedDictionary might be an option for you, if every array value appears only once. Since OrderedDictionaries are ordered by the key, you could add your array entries to such a dictionary, using
the array index as the dictionary value and
the array value as the dictionary key (which will be used to order the dictionary).
What you are looking for is storing it as a Dictionary < int,int > and sort by the dictionary by Value.
I think the VB .Net synatx for dictionary is Dictionary( of Int, Int)