Counting Sublist Elements in Prolog
By : Laura Christie
Date : March 29 2020, 07:55 AM
I wish did fix the issue. How can I count nested list elements in Prolog? I have the following predicates defined, which will count a nested list as one element: code :
len([HT],N) :
len(H, LH),
len(T, LT),
!,
N is LH + LT.
len([], 0):!.
len(_, 1):!.
? len([a,b,[c,d,e],f],Output).
Output = 6.

Python find list lengths in a sublist
By : user1671062
Date : March 29 2020, 07:55 AM
hope this fix your issue I am trying to find out how to get the length of every list that is held within a particular list. For example: , [len(x) for x in a[0]] ? code :
>>> a = []
>>> a.append([])
>>> a[0].append([1,2,3,4,5])
>>> a[0].append([1,2,3,4])
>>> a[0].append([1,2,3])
>>> [len(x) for x in a[0]]
[5, 4, 3]

Counting how many time a list is sublist of another one
By : user7371296
Date : March 29 2020, 07:55 AM
Any of those help I guess everyone has their own simplified version. Mine's only slightly different with a tailrecursion: code :
% headsub(S, L) is true if S is a sublist at the head of L
headsub([HsTs], [HsTl]) :
headsub(Ts, Tl).
headsub([], _).
subcount(L, S, C) :
subcount(L, S, 0, C).
subcount([HlTl], S, A, C) :
( headsub(S, [HlTl])
> A1 is A + 1 % if S is a sublist in front, count it
; A1 = A % otherwise, don't count it
),
subcount(Tl, S, A1, C). % Count the rest, adding it into the accumulator
subcount([], _, C, C). % We're done here. Accumulator becomes the answer.
countSublist([HT], L2, R): size(L2, SizeL2), %% we need L2's size
subList([HT], SizeL2, SubL1), %% we get L1's sublist
countSublist(T, L2, R),
SubL1 = L2,
R1 is R+1. %% we do R+1 only if L2=Sublist of L1
countSublist([HT], L2, R): size(L2, SizeL2), %% we need L2's size
subList([HT], SizeL2, SubL1), %% we get L1's sublist
countSublist(T, L2, R1),
( SubL1 = L2
> R is R1+1 %% we do R+1 only if L2=Sublist of L1
; R = R1
).
 ? countSublist([1,2,3,4,5,2,3,2,3,4], [2,3,4], R).
R = 2 ? a
R = 2
R = 2

Counting elements in a sublist of strings
By : Shadaab Khan
Date : March 29 2020, 07:55 AM
it helps some times Use nested list comprehension. Then, take the sum of elements in each sublist to get the corresponding total number of characters. Furthermore, I am presenting a shorter simplified version without using range(len(...)). You can directly loop over the list elements code :
mylist2 = [sum([len(j) for j in subl]) for subl in mylist]
# [29, 75, 77, 33]
mylist2 = [[len(j) for j in subl] for subl in mylist]
# [[16, 13], [21, 33, 21], [28, 16, 33], [33]]

Java. Find the lengths of strings in an array list for which those string lengths occur the most
By : user3574503
Date : March 29 2020, 07:55 AM
Hope this helps For example, say I have the following array after splitting it. , You can make use of Java Stream Library's groupingBy collector code :
String[] arr = new String[]{
"Hello",
"world",
"what",
"a",
"fine",
"day",
"it",
"is",
"the",
"date",
"is",
"01/10/2020"
};
List<Integer> maxRepeatingSizes =
Arrays.stream(arr)
.collect(Collectors.groupingBy(
String::length,
Collectors.counting()
)).entrySet()
.stream()
.collect(
Collectors.groupingBy(
Map.Entry::getValue,
Collectors.mapping(
Map.Entry::getKey,
Collectors.toList()
)
)
).entrySet()
.stream()
.max(
Map.Entry.comparingByKey()
).get()
.getValue();

