How to get first elements from a pyspark array? - pyspark

I have an array and I just keep a specific column from it like this in PySpark:
c = [ "green", "blue", "red", "yellow",... ]
And I want to keep the n first elements of this column
How can I do this please ? I know is a simple question but I don't find the solution ...
Thanks

Related

Randomise Multiple Choice options in swift

I hope you are all well!
I am new in Coding and Swift.
I am making a quiz app and there are 2 things I would need help with.
The first one is : I want to randomise the options of the multiple choice answers for each question every time the game is played.
For example, if i define an array to contain the different answer options (the different colours), i want the question to retrieve 3 random answers together with the correct one like so:
let answers = [
"Red",
"Blue",
"Green",
"Yellow",
"Orange",
"Black",
"Grey",
"White"]
Question(q: "What colour is the ocean ?", a: ["Blue", "randomAnswer1", "randomAnswer2","randomAnswer3"], correctAnswer: "Blue"),
Question(q: "What colour are roses ?", a: ["randomAnswer1", "Red", "randomAnswer2","randomAnswer3"], correctAnswer: "Red"),
The 2nd part I would need help with is:
If my quiz has 100 questions, how could I make the series of the questions to be RANDOM but UNIQUE every time the game is played?
Meaning that until the Quiz ends, each question would need to be displayed only once.
Thank you very much for your help!
1: There is a method to get a random element from an array
let random = answers.randomElement()
2: You can iterate and delete the used element, so next time you get a random element the array does not consist it
answers = answers.filter { $0 != random }

Query by item in array of document, and sort by item's position in the array

I have a collection dinosaurs with documents of this structure:
doc#1:
name: "Tyrannosaurus rex",
dominantColors: [
"beige",
"blue",
"green"
]
doc#2:
name: "Velociraptor",
dominantColors: [
"green",
"orange",
"white"
]
I want to query the collection by color name (for example: green) to get documents sorted by color's position in dominantColors array. First get the documents in which green occurs higher in the array, then those in which it is lower. So, in the provided case I would get doc#2 first, then doc#1.
Each dominantColors array contains 3 elements, with elements sorted from most dominant to least.
I am looking through documentation, but am not able to find a solution. Maybe I need a different data structure altogether?
Cloud Firestore doesn't support querying arrays by ranked index. The only way you can query an array is using an array-contains type query.
What you could do instead is organize your colors using maps where the color is the key and their rank is the value:
name: "Tyrannosaurus rex",
dominantColors: {
"beige": 1,
"blue": 2,
"green": 3
}
Then you can order the query by the value of the map's property. So, in JavaScript, it would be something like this:
firebase
.collection('dinosaurs')
.where('dominantColors.green', '>', 0)
.orderBy('dominantColors.green')

Why not have Lists build the other way around and have them append elements?

Since in Scala lists are actually build like (here for List(1,2,3)) this:
[ 1 , [ 2, [ 3 , Nill ] ] ] // (pseudo-code)
it is more efficient to pretend new elements and that is why :: is right associative (all explained in https://stackoverflow.com/a/1162980/4533188) - to be better readable (here 1 :: 2 :: 3). That answers my question, why it's good to have right association in the first place. But why didn't the designers of Lists simply construct them like
[ Nill , [ 3, [ 2 , 1 ] ] ] // (pseudo-code)
internally and use conventional left association?
Here in a graphic what my pseudo-code is supposed to mean (since it does not show the "links" of the linked list):
Because an append-list wouldn't be immutable (or you'd have to copy it in its entirety on each change).
See https://en.wikipedia.org/wiki/Linked_list for mre.

Assign a color to individual row numbers

I am trying to assign an individual color to each row in a group. There are approximately 50 rows I need to assign a color and they all must be unique. What would be the easiest way to do this?
I do not want to do the alternating row colors:
"=IIf(RowNumber("DataSet1") Mod 2 = 1, "White","Blue")",
but if there was a way to modify this expression to do 50 colors and then repeat I'd be okay with that.
you can use choose . otherwise use custom code function
=Choose(ROWNUMBER(nothing) mod 50, "Brown", "Blue", "GoldenRod", "Olive", "MediumTurquoise","Red", "Green", "DeepSkyBlue", "Yellow", "Chocolate", "Purple", "DarkOrange" ,.....)

How do you do an AND query on an array in mongodb?

I have an array with tags which is part of a document, eg
["red", "green", "blue", "white", "black"]
Now I want to find all documents, which have red AND blue.
Use the $all condition to find records that match both "red" and "blue" conditions.
db.my_collection.find({tags: { $all : ["red","blue"]}})
If you want records that match either "red" or "blue" then you can use the $in condition.
db.my_collection.find({tags: { $in : ["red","blue"]}})
Also in case someone is wondering how to negate itemized search, i.e. find record that match both "red" and "blue" and DO NOT match "green" and "white" this can be accomplished with the $nin operator, which may not be obvious when someone is reading $nin docs (http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-%24nin - parsing description was tricky for me):
db.my_collection.find({tags: { $all : ["red","blue"], $nin : ["green", "white" ]}})
This is very cool, as it allows relatively nice search syntax with negation:
tokenRequired1 tokenRequired2 !tokenForbidden1 !tokenForbidden2
Very natural, Gmail-style search.
As suggested here and here you could do full text search if you create an array of all tokens from a record, though I have no idea if it's efficient or even the best way to do it.