  C RUBY-ON-RAILS MYSQL ASP.NET DEVELOPMENT RUBY .NET LINUX SQL-SERVER REGEX WINDOWS ALGORITHM ECLIPSE VISUAL-STUDIO STRING SVN PERFORMANCE APACHE-FLEX UNIT-TESTING SECURITY LINQ UNIX MATH EMAIL OOP LANGUAGE-AGNOSTIC VB6 MSBUILD # How to get the average of many lists embedded within each other?  » python » How to get the average of many lists embedded within each other?

By : lexstephen
Date : October 24 2020, 06:10 AM
it should still fix some issue A simple implementation that supports nested lists, does without any specific library and works with any python version: code :
``````def nested_avg(elements):
try:
seq = iter(elements)
except TypeError:
return elements
s = 0
while True:
try:
el = next(seq)
except StopIteration:
return s / len(elements)
s += nested_avg(el)

l1=[2,3,[4,6]]
print(int(nested_avg(l1)))

l2=[2,3,[4,[5,7]]]
print(int(nested_avg(l2)))
`````` ## Advice with lists, for loops, and user lists iput in order to create a calculator that calculates average

Date : March 29 2020, 07:55 AM
hop of those help? what you are doing wrong is changing knowledge from a list [100,90] into an int 190
for n in 190 is not a valid statement ## how to turn a string of letters embedded in squared brackets into embedded lists

By : Heroicguy
Date : March 29 2020, 07:55 AM
will be helpful for those in need I'm trying to find a simple way to convert a string like this:
code :
``````>>> import json
>>> a = '[[a b] [c d]]'
>>> a = ''.join(map(lambda x: '"{}"'.format(x) if x.isalpha() else x, a))
>>> a
'[["a" "b"] ["c" "d"]]'
[[u'a', u'b'], [u'c', u'd']]
``````
``````>>> a = '[[[a b] [c d]] [[e f] [g h]]]'
>>> ...
[[[u'a', u'b'], [u'c', u'd']], [[u'e', u'f'], [u'g', u'h']]]
``````
``````>>> import json
>>> a = '[[X v] -s]'
>>> a = ''.join(map(lambda x: '"{}"'.format(x) if x.isalpha() or x=='-' else x, a))
>>> json.loads(a.replace('[ [', '[[').replace('] ]', ']]').replace(' ', ',').replace('][', '],[').replace('""',''))
[[u'X', u'v'], u'-s']
`````` ## Reorder a Python list of lists according to the second element of embedded lists

By : juleshoward
Date : March 29 2020, 07:55 AM
code :
``````>>> l = [[1, 'A'], [2, 'B'], [3, 'B'], [4, 'A'], [5, 'C']]
>>> from operator import itemgetter
>>> sorted(l, key=itemgetter(1))
[[1, 'A'], [4, 'A'], [2, 'B'], [3, 'B'], [5, 'C']]
`````` ## Trouble Appending to Lists Embedded in Lists

By : Albert So
Date : March 29 2020, 07:55 AM
I wish did fix the issue. Python dictionary will do a better mapping between strings and values.
code :
``````myd = {"A": 1, "B": 2, "C": 0}
myd["C"] = 3
myd.update({"D": 4}) # this will add D
``````
``````X = ["A", "B", "C"]
Y = 
Z = [X, Y]
for i in X:
val = X.index(i) + 1
if val not in Y:
Y.append(val)
`````` ## Average across lists of lists in numpy

By : user3085151
Date : March 29 2020, 07:55 AM
wish helps you Create np.array from list of lists li specifying dtype='object' and swapaxes to group same s into same group. Slicing 2 last elements on axis 2 (right-most axis), and sum, and divide it by num_exp. Finally, column_stack unique string values to it.
code :
``````num_exp = 3
li = [[["s1", 1, 2], ["s2", 3, 4], ["s3", 5, 6]],
[["s1", 2, 3], ["s2", 4, 5], ["s3", 6, 7]],
[["s1", 3, 4], ["s2", 5, 6], ["s3", 7, 8]]]

arr = np.array(li, dtype='object').swapaxes(0, 1)

Out:
array([[['s1', 1, 2],
['s1', 2, 3],
['s1', 3, 4]],

[['s2', 3, 4],
['s2', 4, 5],
['s2', 5, 6]],

[['s3', 5, 6],
['s3', 6, 7],
['s3', 7, 8]]], dtype=object)

arr1 = arr[...,[1,2]].sum(axis=1) / num_exp

Out:
array([[2.0, 3.0],
[4.0, 5.0],
[6.0, 7.0]], dtype=object)

s = arr[:,0, 0]
result = np.column_stack([s, arr1])

Out:
array([['s1', 2.0, 3.0],
['s2', 4.0, 5.0],
['s3', 6.0, 7.0]], dtype=object)
`````` 