Help us understand the problem. What is going on with this article?

# PythonのlambdaとreduceのList処理

More than 1 year has passed since last update.

# PythonであるByte中「1」のBit数を計算する

## 1. Lambda

```get_bits = lambda value: get_bits(value>>1) +  (0x01&(value))  if  value.bit_length() > 0 else 0
get_bits(0x15)
```
```get_bits = lambda bits, value:  get_bits(bits -1, value) +  (0x01&(value >> bits-1))  if bits > 0 else 0
get_bits(8, 0x70)
```

## 2.Reduce

```import functools
f = lambda value: functools.reduce(lambda x, y: x + y, [ 0x01&value>>i for i in range(value.bit_length())])
OR
def f(value):
return functools.reduce(lambda x, y: x + y, [ 0x01&value>>i for i in range(value.bit_length())])
f(0x12)
```
Why not register and get more from Qiita?
1. We will deliver articles that match you
By following users and tags, you can catch up information on technical fields that you are interested in as a whole
2. you can read useful information later efficiently
By "stocking" the articles you like, you can search right away