Filtering ArrayField by entire and exact array only? - django

I have a slug ArrayField on a model.
How can I filter or get by the entire exact array only?
I'm currently doing something like this:
search = f'["a", "b", "c"]'
list = search[2:-2].split("', '")
dict = {}
for n, item in enumerate(list):
dict[f"slug__{n}"] = item
obj = queryset.filter(**dict)
However, this returns any object where the slug begins with "a", "b", and "c".
E.g.
["a", "b", "c"]
["a", "b", "c", "d"]
["a", "b", "c", "d", "e"]
["a", "b", "c", "123"]
How do I do a filter or get so that only the entire and exact slug match returns? I.e. obj only returns objects with a slug of ["a", "b", "c"]

To filter an ArrayField by an exact match you can just pass the list to match against to a filter
queryset = queryset.filter(slug=["a", "b", "c"])

Related

Flutter : How creat new list with changed index from exist list?

if I have this List in Flutter :
var myList = ["a","b" , "c" , "d"];
How to create new list from myList by random index for each value like :
["b","c" , "a" , "d"];
or :
["a","c" , "d" , "b"];
or any other random list
var newList = [...myList];
newList.shuffle();
import random
myList = ["a","b" , "c" , "d"];
random.shuffle(myList)
print(mylist)
You follow this
https://www.w3schools.com/python/ref_random_shuffle.asp
Try this:
var myList = ["a", "b", "c", "d"];
myList.shuffle();

AppSync Subscription Filters

We need a way to filter a subscription in the following manner:
type Subscription {
onPlanningViewUpdate(prop1: ["a", "b", "c"]): ReturnObject
}
ReturnObject = {prop1: "a", ...} // would pass through
ReturnObject = {prop1: "b", ...} // would pass through
ReturnObject = {prop1: "c", ...} // would pass through
ReturnObject = {prop1: "x", ...} // would NOT pass through
We have tried using the request and response mapping templates of a resolver with a NONE type data source, but the mappings seem to only get called once when the subscription is first opened. It looks like subscriptions only handle an exact match. We need a way to determine if a prop is contained in the array ["a", "b", "c"]. If prop1 == "a" or prop1 == "b" or prop1 == "c" pass through.
Here is the actual filter we want to use:
type Subscription {
onPlanningViewUpdate(site_ids: ["a", "b", "c"], planDate: "aString", lob_ids: ["x", "y", "z"]):
ReturnObject
}
ReturnObject = {site_ids: "a", planDate: "aString", lob_ids: "y"} // would pass through
ReturnObject = {site_ids: "a", planDate: "wrongString", lob_ids: "y"} // would NOT pass through
Is there a way to do this ?
Thanks,
Warren Bell

Concatenate lists with Python

I would like to concatenate two lists:
list_a = ["hello", "world"]
list_b = ["a", "b", "c", "d"]
and get something like this as output:
list_c = ["hello a", "hello b", "hello c", "hello d", "world a", "world b", "world c", "world d"]
The second list is basically going from a to z and create combinations with the list_a.
from functools import reduce
list_a = ["hello", "world"]
list_b = ["a", "b", "c", "d"]
def cross_join_lists(word):
return [f'{word} {letter}' for letter in list_b]
def concat_lists(final_list, a_list):
return final_list + a_list
joined_lists = list(map(cross_join_lists, list_a))
list_c = reduce(concat_lists, joined_lists)
print(list_c)
You can use a list comprehension:
new_list = [f"{i} {j}" for i in list_a for j in list_b]
# output: ['hello a', 'hello b', 'hello c', 'hello d', 'world a', 'world b',
'world c', 'world d']

How to count items' occurence in a List

I am new to Dart. Currently I have a List of duplicate items, and I would like to count the occurence of them and store it in a Map.
var elements = ["a", "b", "c", "d", "e", "a", "b", "c", "f", "g", "h", "h", "h", "e", "a"];
I want to have a result like:
{
"a": 3,
"b": 2,
"c": 2,
"d": 2,
"e": 2,
"f": 1,
"g": 1,
"h": 3
}
I did some research and found a JavaScript solution, but I don't know how to translate it to Dart.
var counts = {};
your_array.forEach(function(x) { counts[x] = (counts[x] || 0)+1; });
Play around with this:
var elements = ["a", "b", "c", "d", "e", "a", "b", "c", "f", "g", "h", "h", "h", "e"];
var map = Map();
elements.forEach((element) {
if(!map.containsKey(element)) {
map[element] = 1;
} else {
map[element] += 1;
}
});
print(map);
What this does is:
loops through list elements
if your map does not have list element set as a key, then creates that element with a value of 1
else, if element already exists, then adds 1 to the existing key value
Or if you like syntactic sugar and one liners try this one:
var elements = ["a", "b", "c", "d", "e", "a", "b", "c", "f", "g", "h", "h", "h", "e"];
var map = Map();
elements.forEach((x) => map[x] = !map.containsKey(x) ? (1) : (map[x] + 1));
print(map);
There are many ways to achieve this in all programming languages!
The shorter way to count items' occurrence in a List
List of items. Count items equal 1.
List<int> lst = [0,1,1,1,0,8,8,9,1,0];
int res = lst.map((element) => element == 1 ? 1 : 0).reduce((value, element) => value + element);
List of objects. Count objects, which property age equals 1.
class Person {
int age;
Person(this.age);
}
List<Person> lst2 = [Person(1), Person(0), Person(1), Person(0)];
int res2 = lst2.map((element) => element.age == 1 ? 1 : 0).reduce((value, element) => value + element);
Use fold with a map:
final elements = ["a", "b", "c", "d", "e", "a", "b", "c", "f", "g", "h", "h", "h", "e", "a"];
var counts = elements.fold<Map<String, int>>({}, (map, element) {
map[element] = (map[element] ?? 0) + 1;
return map;
});
print(counts);
Out: {a: 3, b: 2, c: 2, d: 1, e: 2, f: 1, g: 1, h: 3}

updating model with parameter in elm 0.18

New to elm. Using elm 0.18.
This is a very simple SPA with 12 buttons (each with a musical note value). You click the button, and it displays the note you clicked.
I'd like to do this by assigning a function Put to a button via onClick, then passing it a string parameter note that gets used to update the model.
Here is my code:
import Html exposing (div, beginnerProgram, text, button)
import Html.Events exposing (onClick)
import List exposing (..)
main =
beginnerProgram { model = model, update = update, view = view }
-- model
model =
{ key = "C" }
keys =
["C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"]
-- update
type Action = Put
update msg model =
case msg note of
Put ->
{ model | key = note }
-- view
makeButton note =
button [ onClick Put note ] [text note]
view model =
div [] [
div [] [text (toString model.key)],
div [] (map makeButton keys)
]
I get this error:
-- NAMING ERROR -------------------------------------------------- musiccalc.elm
Cannot find variable `note`
19| case msg note of
Got it, you type the action, then evaluate first before going into onClick.
import Html exposing (div, beginnerProgram, text, button)
import Html.Events exposing (onClick)
import List exposing (..)
main =
beginnerProgram { model = model, update = update, view = view }
-- model
model =
{ key = "C" }
keys =
["C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"]
-- update
type Action =
Put String
update msg model =
case msg of
Put note ->
{ model | key = note }
-- view
makeButton note =
button [ onClick (Put note) ] [text note]
view model =
div [] [
div [] [text (toString model.key)],
div [] (map makeButton keys)
]