I searched for this for a while, basically I'm trying to conditionally increase the list of elements with another list, element-wise ...
my code is the following, but is there a better way to do this? list, map?
I think an elementary statement like ~ + = from http://www.python.org/dev/peps/pep-0225/ would be really good, but why is it postponed?
for i in range(1,len(s)): if s[i]<s[0]: s[i]+=p[i]
based on some good reviews from you guys I transcoded the following
i=s<s[0] s[i]+=p[i]
and s, p are both arrays.
ps is still slower than matlab 5 times for one of my codes.
If you do not want to create a new array, then your options are:
s[s<s[0]] += p[s<s[0]]
:
# sample data s = [10, 5, 20] p = [2,2,2] # As a one-liner. (You could factor out the lambda) s = map(lambda (si, pi): si + pi if si < s[0] else si, zip(s,p)) # s is now [10, 7, 20]
, len(s) <= len(p)
len(s) <= len(p)
, . . .: -)
, - :
[sum(a) for a in zip(*[s, p]) if a[0] < 0]
>>> [sum(a) for a in zip(*[[1, 2, 3], [10, 20, 30]]) if a[0] > 2] [33]
, zip:
>>> zip(*[[1, 2, 3], [4, 5, 6]]) [(1, 4), (2, 5), (3, 6)]
( ) . .
s = [s[i]+p[i]*(s[i]<s[0]) for i in range(1,len(s))]
Source: https://habr.com/ru/post/1775536/More articles:Is a file faster than reading data from a database? - performanceИспользование networkx с моим собственным объектом - pythonWhat would be for the JavaScript alerts that are generated on the onload () page using Selenium? - javascriptProcessing encoded response using HTTPBuilder / Groovy - xml-parsingPoint a member function pointer to a regular pointer - c ++Using C # to Retrieve Data from a Google Search - c #Настраиваемые привязки Caliburn.Micro не работают в вложенных представлениях? - silverlighthttps://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1775560/how-to-force-jinja2-templates-to-recompile&usg=ALkJrhh4H3dIUEgFzqnkmRQqt_YN8W01PQInline images in wxHaskell - staticHow a unit test database depended on C # behavior - c #All Articles