« first day (2199 days earlier)      last day (1142 days later) » 

1:41 AM
Is there a more efficient/pythonic way of doing this?
for i1 in lst1:
    if i1:
        break
else:
    for i2 in lst2:
        if i2:
            break
    else:
        for i3 in lst3:
            if i3:
                break
        else:
            for i4 in lst4:
                if i4:
                    break
            else:
                print("Never broke")
 
judging by the look of it, yes
Is that mess looking for the first list among the 4 lists that contains a truthy value?
And what is the expected end result if you find that list?
If that's what it's supposed to do then you'd first want to put those 4 names into a single list or tuple because named variables are a crime, and then something like
first_truthy = next((lst for lst in list_of_lists if any(lst)), None)
if first_truthy is None:
    print('Never broke')  # no longer makes sense
 
it's breaking on truthy values, so presumably, it's looking for non-truthy first-values OR (at the end) the last truthy first-value in the lists
 
1:56 AM
According to my "presumably" the end result is whatever comes after "break". But this is exacty why I asked Ann what the expected end result is.
 
fair point
@AndrasDeak actually, if i1 is truthy, then the only variable which would exist after that entire block executes, is i1 (and the 4 lists)... so maybe it is looking for the first truthy value... :-/
or
`first_truthy = next(filter(None, itertools.chain(lst1, lst2, lst3, lst4)), 'Never broke')`
 
No, you're missing a level of indirection
the original code is looking at the contents of each list, trying to find a truthy value within each list
I mean, OK, you're looking for the first truthy item anywhere. Which leads us back to my previous point made twice: the requirement is unclear.
 
---that's what `filter` would do right?---
yup, need clarity, just providing an option
 
Ann just left so... we'll never know
@aneroid 1. three hyphens, 2. no newline
 
grrrr
either way, the one i provided - and the one you provided - does what the Ann asked (except print vs return)
 
2:13 AM
We don't know if it does because Ann refused to tell us what she asked.
 
it replicates the behaviour of the code given, what it does; regardless of what was meant
 
No, because the original code binds some of i1/i2/i3/i4 depending on the contents of the list.
But we've wasted way too much breath on this. Either Ann will respond or this is just noise. We'll see later.
 
well, true. which goes back to your question about intent
 
 
9 hours later…
11:37 AM
26 messages moved from Python
 
 
7 hours later…
6:09 PM
for i1 in lst1:
    if i1 in bar:
        break
else:
    for i2 in lst2:
        if i2 + 4 in foo:
            break
    else:
        for i3 in lst3:
            if i3 % 2:
                break
        else:
            for i4 in lst4:
                if i4.upper() == "HEY":
                    break
            else:
                print("Never broke")
 
1 message moved from Python
 
6:56 PM
Here is my code, where you know g already.
=========================================================
n = 100 # number of points
Lx = 100 # extension in x
Ly = 100 # extension in x
dx = n/Lx # Step in x
dy = n/Ly # Step in x
c=4
d=4
x=np.arange(-Lx,Lx)
y=np.arange(-Ly,Ly)

g = np.zeros((2*Lx,2*Ly))

lapg = np.zeros((2*Lx,2*Ly))
lapg[:,:] = c + d

g = (1/2)*c*x[None, :]**2 + (1/2)*d*y[:, None]**2

kxpad = 2*np.pi*np.fft.fftfreq(2*Lx)

kypad = 2*np.pi*np.fft.fftfreq(2*Ly)

lapgFFT = np.real(np.fft.ifft2(np.fft.fft2(g) * (-kxpad[None, :]**2 - kypad[:, None]**2)))
 
1 message moved from Python
 

« first day (2199 days earlier)      last day (1142 days later) »