Flutter : How creat new list with changed index from exist list? - 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();

Related

Filtering ArrayField by entire and exact array only?

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"])

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

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}

How to append dictionary in array as index in swift 3.0

How to add dictionary's key, value in array as array index form, like this
[
{
"summary": "fdsfvsd"
},
{
"content_date": "1510158480"
},
{
"content_check": "yes"
}
]
As per your question, you want to work with array of dictionaries in Swift,
here is the simple way to achieve the same :
var arrayOfDict = [[String: String]]()
//creating dictionaries
let dict1 = ["name" : "abc" , "city" : "abc1"]
let dict2 = ["name" : "def" , "city" : "def1"]
let dict3 = ["name" : "ghi" , "city" : "ghi1"]
let dict4 = ["name" : "jkl" , "city" : "jkl1"]
//Appending dictionaries to array
arrayOfDict.append(dict1)
arrayOfDict.append(dict2)
arrayOfDict.append(dict3)
arrayOfDict.append(dict4)
//accessing each and every element of the arrayOfDictionary
for dict in arrayOfDict{
for (key, value) in dict {
print("the value for \(key) is = \(value)")
}
}
Hope it helps you!

How to count elements from list if specific key present in list using scala?

I have following list structure -
"disks" : [
{
"name" : "A",
"memberNo" :1
},
{
"name" : "B",
"memberNo" :2
},
{
"name" : "C",
"memberNo" :3
},
{
"name" : "D",
}
]
I have many elements in list and want to check for 'memberNo', if it exists
I want count of from list elements.
e.g. here count will be 3
How do I check if key exists and get count of elements from list using scala??
First create class to represent your input data
case class Disk (name : String, memberNo : String)
Next load data from repository (or other datasource)
val disks: List[Disk] = ...
And finally count
disks.count(d => Option(d.memberNo).isDefined)
In a similar fashion as in #SergeyLagutin answer, consider this case class
case class Disk (name: String, memberNo: Option[Int] = None)
where missing memberNo are defaulted with None; and this list,
val disks = List( Disk("A", Some(1)),
Disk("B", Some(2)),
Disk("C", Some(3)),
Disk("D"))
Then with flatMap we can filter out those disks with some memberNo, as follows,
disks.flatMap(_.memberNo)
res: List[Int] = List(1, 2, 3)
Namely, for the counting,
disks.flatMap(_.memberNo).size
res: Int = 3
Likewise, with a for comprehension,
for (d <- disks ; m <- d.memberNo) yield m
res: List[Int] = List(1, 2, 3)