Home > Enterprise >  Return the value of bitwise and of consecutive element each time in list [python]
Return the value of bitwise and of consecutive element each time in list [python]

Time:10-25

I need to print [72,8,0] by comparing a[i] with a[i 1] on bitwise AND ( and add, sub,* like know along with that), the error I am facing is 'list index out of range'. I tried with recursive but it print the whole value.

a=[123,456,654,4]
arr=[]
for i in range(len(a)):
    ass=a[i]&a[i 1]
    arr.append(ass)
print(arr) 

I am new to programming. As I am learning I encountered many way of list comprehension (python). I need to know all the possible way like a cheat code. Is there any == True, pls attach the link

CodePudding user response:

Thanks @cooperfield to get me ,near to the answer.

a = [123, 456, 654, 4]
b=a[0]
arr = []
def recurs(b):
    for i in range(len(a)-1):
        b=b&a[i 1]
        arr.append(b)
    return b
recurs(b)
print(arr)

But If there any list comprehension for the above code pls mention below

CodePudding user response:

There is a problem with your current solution, you modify something outside of your function, that is usually ill advised

>>> arr = []
>>> a = [123, 456, 654, 4]
>>> b=a[0]
>>> arr = []
>>> def recurs(b):
        for i in range(len(a)-1):
            b=b&a[i 1]
            arr.append(b)
        return b

>>> recurs(b)
0
>>> arr
[72, 8, 0]
>>> recurs(b)
0
>>> arr
[72, 8, 0, 72, 8, 0]
>>> 

because two identical call to the same function should result in the same result, tho by technicality the result of function is the same, the side effect it has surely are undesired.

The fix is that the function itself does everything it need to do inside and don't depend in some outside thing that can easily be provided as argument to it

>>> arr = []
>>> def recur(data):
        b = data[0]
        arr = []
        for a in data[1:]:
            b = b&a
            arr.append(b)
        return arr

>>> a = [123, 456, 654, 4]
>>> recur(a)
[72, 8, 0]
>>> recur(a) #2 identical call, same result
[72, 8, 0]
>>> arr  # the arr outside wasn't touched, the function make its own inside
[]
>>>   

A couple of thing to notice here, list, tuples, string and many more can be used directly in a for loop, in which case you loop directly over the elements

>>> for x in a:
        print(x)

    
123
456
654
4
>>>     

the slice notation allow you get a sublist of your list

>>> a
[123, 456, 654, 4]
>>> a[:1]
[123]
>>> a[1:]
[456, 654, 4]
>>> a[1:3]
[456, 654]
>>> 

the syntax is mylist[start:end], where start is the initial index you want and end the final one, either can be omitted and an appropriate default value will be used, you can find more about it here: common-sequence-operations

As for a list comprehension version, until the introduction of the walrus operator in py3.8 it wasn't easily done given the way that b is used in the function, namely the previous result affect the next, unless you use some dark magic or something...

But with that then in can be done as

>>> def recur2(data):
        b = data[0]
        return [ (b := b&a) for a in data[1:] ]

>>> recur2(a)
[72, 8, 0]
>>>   
  • Related