I found iterable functions, but I am not sure how I can use.
For example, skip, take, map, forEach, fold and join
Could you give me examples how to use?
Yes, let's check the following sample code.
List<int> values = [1, 2, 3, 4, 5, 6, 7, 8, 9];
print(values.skip(5).toList());
//[6, 7, 8, 9]
print(values.skip(5).take(3).toList());
//[6, 7, 8]
values.skip(5).take(3).map((e) => e.toString()).forEach((element) {print(element);});
//6 7 8
String str = values.fold("initialValue",
(previousValue, element) => previousValue + ", " + element.toString());
print(str);
//initialValue, 1, 2, 3, 4, 5, 6, 7, 8, 9
str = values.join(", ");
print(str);
//1, 2, 3, 4, 5, 6, 7, 8, 9
skip(1) skips the first value, 1, in the values list literal.
take(3) gets the next 3 values 2, 3, and 4 in the values list literal.
map() Returns a new lazy [Iterable] with elements that are created by calling f on each element of this Iterable in iteration order.
fork() Reduces a collection to a single value by iteratively combining each element of the collection with an existing value
join() Converts each element to a [String] and concatenates the strings.
Hi Avdienko and welcome to Stack Overflow. I will give you an example for a .forEach iterable function performed on a List.
List<int> listOfIntegers = [1, 2, 3, 4, 5];
listOfIntegers.forEach((element) {
print(element.toString() + " ");
});
This code will result in printing "1 2 3 4 5 " to the console.
Cant figure out how to do it.
I'm using sort() with compareTo() to sort a list ascending by one criteria, but i need to resort it with a second criteria, keeping odd numbers in the beggining of the list.
widget.tasks.sort((a,b){
return a.key.compareTo(b.key);
});
This code above just sorts one of the attributes of the list. A need to sort a second one of integer numbers.
Here is working Example Copy code and run
List numlist = [1, 2, 3, 4, 5, 6, 7, 9, 10];
List oddList = [];
List evenList = [];
List firstOddThenEven = [];
for (final i in numlist) {
if (i.isEven) {
evenList.add(i);
} else if (i.isOdd) {
oddList.add(i);
}
}
firstOddThenEven.addAll(oddList);
firstOddThenEven.addAll(evenList);
print(firstOddThenEven);
A more simple approach, which also don't require allocating new List objects, would be:
void main() {
final numlist = [1, 2, 3, 4, 5, 6, 7, 9, 10];
numlist.sort((a, b) {
if (a.isEven && !b.isEven) {
return 1;
} else if (b.isEven && !a.isEven) {
return -1;
} else {
return a.compareTo(b);
}
});
print(numlist); // [1, 3, 5, 7, 9, 2, 4, 6, 10]
}
This will also work if the input list are unsorted.
I'm having some trouble figuring out how to switch numbers in a long list.
For example if were to have a list:
numbers = [1,2,3,4,5,6,7,8]
and wanted to instead print it in the form of:
numbers_2 = [2,1,4,3,6,5,8,7]
such that each pair would be switched, using a for-loop. I thought about doing something like:
for i in range(0, len(numbers), 2):
But wasn't really able to get much further.
Loop every second index and swap two adjacent items:
numbers = [1,2,3,4,5,6,7,8]
for i in range(1, len(numbers), 2):
numbers[i-1], numbers[i] = numbers[i], numbers[i-1]
Not sure about the other answers, but this one will also work with a list of an uneven length, and leave the last item untouched.
Take 2 halves, and rearrange them:
numbers = [1,2,3,4,5,6,7,8]
first = numbers[::2]
second = numbers[1::2]
numbers_2 = sum(map(list, zip(second, first)), [])
Try this:
def swap_array_elements (a):
for i in range (0, len(a) - 1, 2):
a[i], a[i +1] = a[i + 1], a[i]
return a
a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print (swap_array_elements (a))
# prints: [1, 0, 3, 2, 5, 4, 7, 6, 9, 8]
Is there a way to find how many times a sequence repeats in a dataframe?
Lets say I have a dataframe with a large number of 1 and 3's and I wanted to see how much this sequence [3,1,3,3,1] repeats.
here's an example list. 3,1,3,3,1,3,3,1,3,3,1,3,1,1,1,1,3,1,3,1,1,3,3,3
Here's an example of what I'm trying to do
this first part would be true 3,1,3,3,1,3,3,1,3,3,1,3,1,1,1,1,3,1,3,1,1,3,3,3
this second part would be false 3,1,3,3,1,3,3,1,3,3,1,3,1,1,1,1,3,1,3,1,1,3,3,3
and the third part would be false
3,1,3,3,1,3,3,1,3,3,1,3,1,1,1,1,3,1,3,1,1,3,3,3
I want to analyze sections at a time according to the length of the sequence I'm trying to find. In numeric order of the data frame.
My data Is in a dateandtime format. But I can change that.
Thanks for all your help I really appreciate it everything everybody does on this site.
my_list = np.array([3, 1, 3, 3, 1, 3, 3, 1, 3, 3, 1, 3, 1, 1, 1, 1, 3, 1, 3, 1, 1, 3, 3, 3])
target = np.array([3, 1, 3, 3, 1])
(my_list.reshape(-1, len(sequence)) == sequence[None, :]).all(axis=1)
This converts a list of numbers into a comma separated string, and then compares each sequential chunk to the target.
from itertools import izip_longest
my_list = [3, 1, 3, 3, 1, 3, 3, 1, 3, 3, 1, 3, 1, 1, 1, 1, 3, 1, 3, 1, 1, 3, 3, 3]
target = [3, 1, 3, 3, 1]
n = len(target)
>>> sum(all(a == b for a, b in izip_longest(target, my_list[(i * n):((i + 1) * n)]))
for i in range(len(my_list) // n))
1
Below is an alternative method that converts the integers to strings and then compares the strings.
target = ",".join(str(number) for number in target)
>>> target
'3,1,3,3,1'
>>> sum(",".join(str(number) for number in my_list[(i * n):(i * n + n)]) == target
for i in range(len(my_list) / n))
1
To give some more intuition on what is going on, the list is chunked five elements at a time and then those elements are joined as a string. These strings are then compared to the target string which was similarly converted, and the number of matches are then summed.
>>> [",".join(str(number) for number in my_list[(i * n):(i * n + n)])
for i in range(len(my_list) / n)]
['3,1,3,3,1', '3,3,1,3,3', '1,3,1,1,1', '1,3,1,3,1']
Step1
Convert list of integers into string.
Step2
Use findall() function of regex module to find all occurences of target_string in my_list_string.
import re
my_list = [3, 1, 3, 3, 1, 3, 3, 1, 3, 3, 1, 3, 1, 1, 1, 1, 3, 1, 3, 1, 1, 3, 3, 3]
target = [3, 1, 3, 3, 1]
my_list_string = ''.join(str(e) for e in my_list)
target_string = ''.join(str(e) for e in target)
print(len(re.findall(target_string, my_list_string)))