Related
I have a random array of numbers 1 to five occurring in ramdom sometimes [1,1,1,1,2,2] etc. I am tasked with finding the value with highest occurrence all the the time regardless. I achieved that in javascript like below using a library called ramda here . After reading the documentation, i went with a solution like below.
// filter out duplication in array that way you can get the uniq represented numbers
const uniqueItems = R.uniq(params);
// use the unique numbers as keys and create a new array of object
const mappedItemsWithRepresentations = map((a) => ({ color: a, rep: params.filter(b => b === a).length }), uniqueItems);
// and then finally, select the item with highest rep and return it key
const maxRepItem = mappedItemsWithRepresentations.reduce((acc, curr) => acc.rep > curr.rep ? acc : curr, []);
return maxRepItem.key; // gives me the correct value i need
However, reading through more in the documentation and going through the example here, i realised there is a way i can combine the logic above and simply with ramda. I tried numerous attempt possible and the closest i could get are below.
const getMaxRep = curry(pipe(uniq, map((a) => ({ color: a, rep: filter(b => b === a).length })), pipe(max(pathEq("rep")), tap(console.log))));
console.log("Max Rep here", getMaxRep(params));
I also tried utilising the reduced feature here, all to no avail. Please how do i arrange achieve that ? Any help will be appreciated.
Ramda has R.countBy to get the number of occurrences. You can convert the resulting object of country to pairs [value, count], and then reduce it to find the pair with the highest count:
const { pipe, countBy, identity, toPairs, reduce, maxBy, last, head } = R
const fn = pipe(
countBy(identity), // count the occurrences
toPairs, // convert to pairs of [value, count]
reduce(maxBy(last), [0, 0]), // reduce to find the maximum occurrence
head, // get the actual value
Number, // convert back to an number
)
const arr = [1,1,1,1,2,2]
const result = fn(arr)
console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.27.0/ramda.js"></script>
A slight variation on this idea that collects values with the same count to an array. This will handle cases in which the frequency of several items is identical:
const { pipe, countBy, identity, toPairs, invert, reduce, maxBy, last, head, map } = R
const fn = pipe(
countBy(identity), // count the occurrences
invert, // combine all values with the same count
toPairs, // convert to pairs of [value, count]
reduce(maxBy(head), [0, 0]), // reduce to find the maximum occurrence
last, // get the actual values
map(Number), // convert back to numbers
)
const arr = [1,1,1,1,2,2,3,3,3,3]
const result = fn(arr)
console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.27.0/ramda.js"></script>
nice use case, try this:
const maxReduce = reduce(maxBy(last), [0,0])
const getMaxRep = pipe(countBy(identity), toPairs, maxReduce, head)
console.log(getMaxRep([1,1,1,1,2,2]))
countBy is a really nice start, sadly Ramda don't support reduce for object but we can convert to an array of arrays using toPairs function and finish the work.
It's not entirely clear to me what it is you're asking for.
But it might be something like this:
const maxRep = pipe (
countBy (identity),
toPairs,
map (zipObj(['color', 'rep'])),
reduce (maxBy (prop ('rep')), {rep: -Infinity}),
)
const params = [1, 2, 3, 4, 2, 3, 5, 2, 3, 2, 1, 1, 4, 5, 5, 3, 2, 5, 1, 5, 2]
console .log (
maxRep (params)
)
<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.27.0/ramda.js"></script>
<script> const {pipe, countBy, identity, toPairs, map, zipObj, reduce, maxBy, prop} = R </script>
We start with a list of values drawn from {1, 2, 3, 4, 5}, occuring in some random, multiply-occuring order.
With countBy(identity) we change the original list into something like
{"1": 4, "2": 6, "3": 4, "4": 2, "5": 5}
with the counts associated with each entry.
toPairs formats that as an array like
[["1", 4], ["2", 6], ["3", 4], ["4", 2], ["5", 5]]
(You could also use Object.entries here.)
Then by calling map (zipObj (['color', 'rep'])), we turn this into
[{"color": "1", "rep": 4}, {"color": "2", "rep": 6}, ...]
Finally, we reduce the result, using maxBy (prop ('rep')), which chooses the one with the maximum rep value. For the initial value to the max call, we create a dummy object, {rep: -Infinity} that will compare less than any in your list.
If you wanted to also keep that final intermediate structure, you could rename that function to makeReps, dropping off the last function in the pipeline, and then making a new maxRep out of it.
Then you could call
const reps = makeResps (params)
const maxVal = maxRep (reps)
and use both.
But all this presupposes that the value with color and rep properties is what you need. If you just need the count then the other solutions already here handle that fine.
messy_list = ["a", 2, 3, 1, False, [1, 2, 3]]
for items in messy_list:
if items.isdigit() == 0:
messy_list.remove(items)
Please help me to do so?
You can do something like this (not in-place):
lst = [item for item in messy_list if isinstance(item, int) and not isinstance(item, bool)]
Can anyone explain this? Why are all elements of yeartonametolist affected when I just change one?
yeartonametolist = dict.fromkeys(range(2007,2017),{})
yeartonametolist[2007]["a"] = 1
yeartonametolist[2008]["b"] = 2
print yeartonametolist
Output:
{
2016: {'a': 1, 'b': 2},
2007: {'a': 1, 'b': 2},
2008: {'a': 1, 'b': 2},
2009: {'a': 1, 'b': 2},
2010: {'a': 1, 'b': 2},
2011: {'a': 1, 'b': 2},
2012: {'a': 1, 'b': 2},
2013: {'a': 1, 'b': 2},
2014: {'a': 1, 'b': 2},
2015: {'a': 1, 'b': 2}
}
It looks like they have same memory address, so modifying one modifies them all, and this is because they are a only reference.
Here is a better explanation:
Your first line creates your object with integers keys and empty object as a value for all of them.
yeartonametolist = dict.fromkeys(range(2007,2017),{})
Now at this point, if you use the id() function like this
id(yeartonametolist[2016]) #4294024972L
id(yeartonametolist[2015]) #4294024972L
It is the same id, so if you do
yeartonametolist[2007]["a"] = 1
yeartonametolist[2008]["b"] = 2
you are changing the same object.
You can see it as well if you change the {} to object()
test = dict.fromkeys(range(2007,2017), object() )
print test
Output:
{
2016: <object object at 0xfff964d8>,
2007: <object object at 0xfff964d8>,
2008: <object object at 0xfff964d8>,
2009: <object object at 0xfff964d8>,
2010: <object object at 0xfff964d8>,
2011: <object object at 0xfff964d8>,
2012: <object object at 0xfff964d8>,
2013: <object object at 0xfff964d8>,
2014: <object object at 0xfff964d8>,
2015: <object object at 0xfff964d8>
}
The value for each key points to the same memory address.
I have a set of datapoints such as (THIS IS AN EXAMPLE)
val=4; (*this value is calculated before in the program, so it not known a priori*)
x={0,1,2,3};
data=Table[0, {val}];
data[[1]] = {1,5,6,8};
data[[2]] = {9,7,1,3};
data[[3]] = {3,4,5,6};
data[[4]] = {2,2,4,6};
Now I can plot each of these data with ListPlot as
ListPlot[Transpose[{x,data[[1]]}]]
and if I want to plot more than one I can do
ListPlot[{Transpose[{x, data[[1]]}], Transpose[{x, data[[2]]}]}]
but how can I plot all of them in one code single line, by considering that val is calculated before in the program?
Is there a way to do something like
For[i = 1, i < val + 1, i++, ListPlot[Transpose[{x,data[i]}]]......]
having a single graph with all x-y curves?
Indeed I would like a static picture of
Manipulate[ListPlot[Transpose[{x, data[[i]]}]], {i, 1, val,1}]
Thanks
Virgilio
You want to "do the same thing" to every element of a list. That should tell you to think of using Map. Your list is named data and each element is your four element sublist. If you look at the help page for Map it shows you need to think up a function that does what you need to do to each individual sublist. You have already understood that you need to use Transpose with x and your sublist so that tells you your function and you are almost there. The result of Map will be a list of all those results. So
In[1]:= x = {0, 1, 2, 3};
data = {{1, 5, 6, 8}, {9, 7, 1, 3}, {3, 4, 5, 6}, {2, 2, 4, 6}};
ListPlot[Map[Transpose[{x, #}] &, data], Joined -> True]
Out[3]= ...FourOverlaidPlotsSnipped...
Go through that a word at a time until you can really understand the thinking that was done to be able to write that. You will use this idea again and again if you keep using Mathematica.
For the example you give the cleanest method is to use DataRange:
data = {{1, 5, 6, 8}, {9, 7, 1, 3}, {3, 4, 5, 6}, {2, 2, 4, 6}};
ListLinePlot[data, DataRange -> {0, 3}]
Please ask your future questions on the dedicated Mathematica StackExchange site:
Suppose I have a tuple of (1, 2, 3) and want to index a multidimensional array with it such as:
index = (1, 2, 3)
table[index] = 42 # behaves like table[1][2][3]
index has an unknown number of dimensions, so I can't do:
table[index[0]][index[1]][index[2]]
I know I could do something like this:
functools.reduce(lambda x, y: x[y], index, table)
but it's utterly ugly (and maybe also inefficient), so I wonder if there's a better, more Pythonic choice.
EDIT: Maybe a simple loop is best choice:
elem = table
for i in index:
elem = elem[i]
EDIT2: Actually, there's a problem with both solutions: I can't assign a value to the indexed array :-(, back to ugly:
elem = table
for i in index[:-1]:
elem = elem[i]
elem[index[-1]] = 42
The question is very interesting and also your suggested solution looks good (havn't checked it, but this kind of problem requires a recursive treatment and you just did it in one line).
However, the pythonic way I use in my programs is to use dictionaries of tuples. The syntax is array-like, the performance - of a dictionary, and there was no problem in it for me.
For example:
a = {(1, 2, 3): 'A', (3, 4, 5): 'B', (5, 6, 7, 8): 'C'}
print a[1, 2, 3]
print a[5, 6, 7, 8]
Will output:
A
B
And assigning to an index is super easy:
a[1, 4, 5] = 42. (But you might want to first check that (1, 4, 5) is within the dict, or else it will be created by the assignment)