I have a list l =[253, 59, 2, 0, 0, 0, 0, 0]
and I want to set a range of (0, 255)
for each element in the list. I want to implement it in python. I want to increment the elements one by one such that when it reaches the max range the element should reset to 0 and the next element should increment by 1
The output should look like this after every iteration:
l =[253, 59, 2, 0, 0, 0, 0, 0]
l =[254, 59, 2, 0, 0, 0, 0, 0]
l =[255, 59, 2, 0, 0, 0, 0, 0]
l =[0, 60, 2, 0, 0, 0, 0, 0]
l =[1, 60, 2, 0, 0, 0, 0, 0]
.
.
.
.
l =[255, 60, 2, 0, 0, 0, 0, 0]
l =[0, 61, 2, 0, 0, 0, 0, 0]
CodePudding user response:
To get your exact output you could try this:
l =[253, 59, 2, 0, 0, 0, 0, 0]
x = 253
for i in range(59,61):
for j in range(x,256):
l[0], l[1] = j, i
print("l = ", l)
x=0
l[0], l[1] = 0, 61
print("l = ", l)
Output:
l = [253, 59, 2, 0, 0, 0, 0, 0]
l = [254, 59, 2, 0, 0, 0, 0, 0]
l = [255, 59, 2, 0, 0, 0, 0, 0]
l = [0, 60, 2, 0, 0, 0, 0, 0]
l = [1, 60, 2, 0, 0, 0, 0, 0]
l = [2, 60, 2, 0, 0, 0, 0, 0]
l = [3, 60, 2, 0, 0, 0, 0, 0]
l = [4, 60, 2, 0, 0, 0, 0, 0]
l = [5, 60, 2, 0, 0, 0, 0, 0]
l = [6, 60, 2, 0, 0, 0, 0, 0]
l = [7, 60, 2, 0, 0, 0, 0, 0]
l = [8, 60, 2, 0, 0, 0, 0, 0]
l = [9, 60, 2, 0, 0, 0, 0, 0]
l = [10, 60, 2, 0, 0, 0, 0, 0]
l = [11, 60, 2, 0, 0, 0, 0, 0]
l = [12, 60, 2, 0, 0, 0, 0, 0]
l = [13, 60, 2, 0, 0, 0, 0, 0]
l = [14, 60, 2, 0, 0, 0, 0, 0]
l = [15, 60, 2, 0, 0, 0, 0, 0]
l = [16, 60, 2, 0, 0, 0, 0, 0]
l = [17, 60, 2, 0, 0, 0, 0, 0]
l = [18, 60, 2, 0, 0, 0, 0, 0]
l = [19, 60, 2, 0, 0, 0, 0, 0]
l = [20, 60, 2, 0, 0, 0, 0, 0]
l = [21, 60, 2, 0, 0, 0, 0, 0]
l = [22, 60, 2, 0, 0, 0, 0, 0]
l = [23, 60, 2, 0, 0, 0, 0, 0]
l = [24, 60, 2, 0, 0, 0, 0, 0]
l = [25, 60, 2, 0, 0, 0, 0, 0]
l = [26, 60, 2, 0, 0, 0, 0, 0]
l = [27, 60, 2, 0, 0, 0, 0, 0]
l = [28, 60, 2, 0, 0, 0, 0, 0]
l = [29, 60, 2, 0, 0, 0, 0, 0]
l = [30, 60, 2, 0, 0, 0, 0, 0]
l = [31, 60, 2, 0, 0, 0, 0, 0]
l = [32, 60, 2, 0, 0, 0, 0, 0]
l = [33, 60, 2, 0, 0, 0, 0, 0]
l = [34, 60, 2, 0, 0, 0, 0, 0]
l = [35, 60, 2, 0, 0, 0, 0, 0]
l = [36, 60, 2, 0, 0, 0, 0, 0]
l = [37, 60, 2, 0, 0, 0, 0, 0]
l = [38, 60, 2, 0, 0, 0, 0, 0]
l = [39, 60, 2, 0, 0, 0, 0, 0]
l = [40, 60, 2, 0, 0, 0, 0, 0]
l = [41, 60, 2, 0, 0, 0, 0, 0]
l = [42, 60, 2, 0, 0, 0, 0, 0]
l = [43, 60, 2, 0, 0, 0, 0, 0]
l = [44, 60, 2, 0, 0, 0, 0, 0]
l = [45, 60, 2, 0, 0, 0, 0, 0]
l = [46, 60, 2, 0, 0, 0, 0, 0]
l = [47, 60, 2, 0, 0, 0, 0, 0]
l = [48, 60, 2, 0, 0, 0, 0, 0]
l = [49, 60, 2, 0, 0, 0, 0, 0]
l = [50, 60, 2, 0, 0, 0, 0, 0]
l = [51, 60, 2, 0, 0, 0, 0, 0]
l = [52, 60, 2, 0, 0, 0, 0, 0]
l = [53, 60, 2, 0, 0, 0, 0, 0]
l = [54, 60, 2, 0, 0, 0, 0, 0]
l = [55, 60, 2, 0, 0, 0, 0, 0]
l = [56, 60, 2, 0, 0, 0, 0, 0]
l = [57, 60, 2, 0, 0, 0, 0, 0]
l = [58, 60, 2, 0, 0, 0, 0, 0]
l = [59, 60, 2, 0, 0, 0, 0, 0]
l = [60, 60, 2, 0, 0, 0, 0, 0]
l = [61, 60, 2, 0, 0, 0, 0, 0]
l = [62, 60, 2, 0, 0, 0, 0, 0]
l = [63, 60, 2, 0, 0, 0, 0, 0]
l = [64, 60, 2, 0, 0, 0, 0, 0]
l = [65, 60, 2, 0, 0, 0, 0, 0]
l = [66, 60, 2, 0, 0, 0, 0, 0]
l = [67, 60, 2, 0, 0, 0, 0, 0]
l = [68, 60, 2, 0, 0, 0, 0, 0]
l = [69, 60, 2, 0, 0, 0, 0, 0]
l = [70, 60, 2, 0, 0, 0, 0, 0]
l = [71, 60, 2, 0, 0, 0, 0, 0]
l = [72, 60, 2, 0, 0, 0, 0, 0]
l = [73, 60, 2, 0, 0, 0, 0, 0]
l = [74, 60, 2, 0, 0, 0, 0, 0]
l = [75, 60, 2, 0, 0, 0, 0, 0]
l = [76, 60, 2, 0, 0, 0, 0, 0]
l = [77, 60, 2, 0, 0, 0, 0, 0]
l = [78, 60, 2, 0, 0, 0, 0, 0]
l = [79, 60, 2, 0, 0, 0, 0, 0]
l = [80, 60, 2, 0, 0, 0, 0, 0]
l = [81, 60, 2, 0, 0, 0, 0, 0]
l = [82, 60, 2, 0, 0, 0, 0, 0]
l = [83, 60, 2, 0, 0, 0, 0, 0]
l = [84, 60, 2, 0, 0, 0, 0, 0]
l = [85, 60, 2, 0, 0, 0, 0, 0]
l = [86, 60, 2, 0, 0, 0, 0, 0]
l = [87, 60, 2, 0, 0, 0, 0, 0]
l = [88, 60, 2, 0, 0, 0, 0, 0]
l = [89, 60, 2, 0, 0, 0, 0, 0]
l = [90, 60, 2, 0, 0, 0, 0, 0]
l = [91, 60, 2, 0, 0, 0, 0, 0]
l = [92, 60, 2, 0, 0, 0, 0, 0]
l = [93, 60, 2, 0, 0, 0, 0, 0]
l = [94, 60, 2, 0, 0, 0, 0, 0]
l = [95, 60, 2, 0, 0, 0, 0, 0]
l = [96, 60, 2, 0, 0, 0, 0, 0]
l = [97, 60, 2, 0, 0, 0, 0, 0]
l = [98, 60, 2, 0, 0, 0, 0, 0]
l = [99, 60, 2, 0, 0, 0, 0, 0]
l = [100, 60, 2, 0, 0, 0, 0, 0]
l = [101, 60, 2, 0, 0, 0, 0, 0]
l = [102, 60, 2, 0, 0, 0, 0, 0]
l = [103, 60, 2, 0, 0, 0, 0, 0]
l = [104, 60, 2, 0, 0, 0, 0, 0]
l = [105, 60, 2, 0, 0, 0, 0, 0]
l = [106, 60, 2, 0, 0, 0, 0, 0]
l = [107, 60, 2, 0, 0, 0, 0, 0]
l = [108, 60, 2, 0, 0, 0, 0, 0]
l = [109, 60, 2, 0, 0, 0, 0, 0]
l = [110, 60, 2, 0, 0, 0, 0, 0]
l = [111, 60, 2, 0, 0, 0, 0, 0]
l = [112, 60, 2, 0, 0, 0, 0, 0]
l = [113, 60, 2, 0, 0, 0, 0, 0]
l = [114, 60, 2, 0, 0, 0, 0, 0]
l = [115, 60, 2, 0, 0, 0, 0, 0]
l = [116, 60, 2, 0, 0, 0, 0, 0]
l = [117, 60, 2, 0, 0, 0, 0, 0]
l = [118, 60, 2, 0, 0, 0, 0, 0]
l = [119, 60, 2, 0, 0, 0, 0, 0]
l = [120, 60, 2, 0, 0, 0, 0, 0]
l = [121, 60, 2, 0, 0, 0, 0, 0]
l = [122, 60, 2, 0, 0, 0, 0, 0]
l = [123, 60, 2, 0, 0, 0, 0, 0]
l = [124, 60, 2, 0, 0, 0, 0, 0]
l = [125, 60, 2, 0, 0, 0, 0, 0]
l = [126, 60, 2, 0, 0, 0, 0, 0]
l = [127, 60, 2, 0, 0, 0, 0, 0]
l = [128, 60, 2, 0, 0, 0, 0, 0]
l = [129, 60, 2, 0, 0, 0, 0, 0]
l = [130, 60, 2, 0, 0, 0, 0, 0]
l = [131, 60, 2, 0, 0, 0, 0, 0]
l = [132, 60, 2, 0, 0, 0, 0, 0]
l = [133, 60, 2, 0, 0, 0, 0, 0]
l = [134, 60, 2, 0, 0, 0, 0, 0]
l = [135, 60, 2, 0, 0, 0, 0, 0]
l = [136, 60, 2, 0, 0, 0, 0, 0]
l = [137, 60, 2, 0, 0, 0, 0, 0]
l = [138, 60, 2, 0, 0, 0, 0, 0]
l = [139, 60, 2, 0, 0, 0, 0, 0]
l = [140, 60, 2, 0, 0, 0, 0, 0]
l = [141, 60, 2, 0, 0, 0, 0, 0]
l = [142, 60, 2, 0, 0, 0, 0, 0]
l = [143, 60, 2, 0, 0, 0, 0, 0]
l = [144, 60, 2, 0, 0, 0, 0, 0]
l = [145, 60, 2, 0, 0, 0, 0, 0]
l = [146, 60, 2, 0, 0, 0, 0, 0]
l = [147, 60, 2, 0, 0, 0, 0, 0]
l = [148, 60, 2, 0, 0, 0, 0, 0]
l = [149, 60, 2, 0, 0, 0, 0, 0]
l = [150, 60, 2, 0, 0, 0, 0, 0]
l = [151, 60, 2, 0, 0, 0, 0, 0]
l = [152, 60, 2, 0, 0, 0, 0, 0]
l = [153, 60, 2, 0, 0, 0, 0, 0]
l = [154, 60, 2, 0, 0, 0, 0, 0]
l = [155, 60, 2, 0, 0, 0, 0, 0]
l = [156, 60, 2, 0, 0, 0, 0, 0]
l = [157, 60, 2, 0, 0, 0, 0, 0]
l = [158, 60, 2, 0, 0, 0, 0, 0]
l = [159, 60, 2, 0, 0, 0, 0, 0]
l = [160, 60, 2, 0, 0, 0, 0, 0]
l = [161, 60, 2, 0, 0, 0, 0, 0]
l = [162, 60, 2, 0, 0, 0, 0, 0]
l = [163, 60, 2, 0, 0, 0, 0, 0]
l = [164, 60, 2, 0, 0, 0, 0, 0]
l = [165, 60, 2, 0, 0, 0, 0, 0]
l = [166, 60, 2, 0, 0, 0, 0, 0]
l = [167, 60, 2, 0, 0, 0, 0, 0]
l = [168, 60, 2, 0, 0, 0, 0, 0]
l = [169, 60, 2, 0, 0, 0, 0, 0]
l = [170, 60, 2, 0, 0, 0, 0, 0]
l = [171, 60, 2, 0, 0, 0, 0, 0]
l = [172, 60, 2, 0, 0, 0, 0, 0]
l = [173, 60, 2, 0, 0, 0, 0, 0]
l = [174, 60, 2, 0, 0, 0, 0, 0]
l = [175, 60, 2, 0, 0, 0, 0, 0]
l = [176, 60, 2, 0, 0, 0, 0, 0]
l = [177, 60, 2, 0, 0, 0, 0, 0]
l = [178, 60, 2, 0, 0, 0, 0, 0]
l = [179, 60, 2, 0, 0, 0, 0, 0]
l = [180, 60, 2, 0, 0, 0, 0, 0]
l = [181, 60, 2, 0, 0, 0, 0, 0]
l = [182, 60, 2, 0, 0, 0, 0, 0]
l = [183, 60, 2, 0, 0, 0, 0, 0]
l = [184, 60, 2, 0, 0, 0, 0, 0]
l = [185, 60, 2, 0, 0, 0, 0, 0]
l = [186, 60, 2, 0, 0, 0, 0, 0]
l = [187, 60, 2, 0, 0, 0, 0, 0]
l = [188, 60, 2, 0, 0, 0, 0, 0]
l = [189, 60, 2, 0, 0, 0, 0, 0]
l = [190, 60, 2, 0, 0, 0, 0, 0]
l = [191, 60, 2, 0, 0, 0, 0, 0]
l = [192, 60, 2, 0, 0, 0, 0, 0]
l = [193, 60, 2, 0, 0, 0, 0, 0]
l = [194, 60, 2, 0, 0, 0, 0, 0]
l = [195, 60, 2, 0, 0, 0, 0, 0]
l = [196, 60, 2, 0, 0, 0, 0, 0]
l = [197, 60, 2, 0, 0, 0, 0, 0]
l = [198, 60, 2, 0, 0, 0, 0, 0]
l = [199, 60, 2, 0, 0, 0, 0, 0]
l = [200, 60, 2, 0, 0, 0, 0, 0]
l = [201, 60, 2, 0, 0, 0, 0, 0]
l = [202, 60, 2, 0, 0, 0, 0, 0]
l = [203, 60, 2, 0, 0, 0, 0, 0]
l = [204, 60, 2, 0, 0, 0, 0, 0]
l = [205, 60, 2, 0, 0, 0, 0, 0]
l = [206, 60, 2, 0, 0, 0, 0, 0]
l = [207, 60, 2, 0, 0, 0, 0, 0]
l = [208, 60, 2, 0, 0, 0, 0, 0]
l = [209, 60, 2, 0, 0, 0, 0, 0]
l = [210, 60, 2, 0, 0, 0, 0, 0]
l = [211, 60, 2, 0, 0, 0, 0, 0]
l = [212, 60, 2, 0, 0, 0, 0, 0]
l = [213, 60, 2, 0, 0, 0, 0, 0]
l = [214, 60, 2, 0, 0, 0, 0, 0]
l = [215, 60, 2, 0, 0, 0, 0, 0]
l = [216, 60, 2, 0, 0, 0, 0, 0]
l = [217, 60, 2, 0, 0, 0, 0, 0]
l = [218, 60, 2, 0, 0, 0, 0, 0]
l = [219, 60, 2, 0, 0, 0, 0, 0]
l = [220, 60, 2, 0, 0, 0, 0, 0]
l = [221, 60, 2, 0, 0, 0, 0, 0]
l = [222, 60, 2, 0, 0, 0, 0, 0]
l = [223, 60, 2, 0, 0, 0, 0, 0]
l = [224, 60, 2, 0, 0, 0, 0, 0]
l = [225, 60, 2, 0, 0, 0, 0, 0]
l = [226, 60, 2, 0, 0, 0, 0, 0]
l = [227, 60, 2, 0, 0, 0, 0, 0]
l = [228, 60, 2, 0, 0, 0, 0, 0]
l = [229, 60, 2, 0, 0, 0, 0, 0]
l = [230, 60, 2, 0, 0, 0, 0, 0]
l = [231, 60, 2, 0, 0, 0, 0, 0]
l = [232, 60, 2, 0, 0, 0, 0, 0]
l = [233, 60, 2, 0, 0, 0, 0, 0]
l = [234, 60, 2, 0, 0, 0, 0, 0]
l = [235, 60, 2, 0, 0, 0, 0, 0]
l = [236, 60, 2, 0, 0, 0, 0, 0]
l = [237, 60, 2, 0, 0, 0, 0, 0]
l = [238, 60, 2, 0, 0, 0, 0, 0]
l = [239, 60, 2, 0, 0, 0, 0, 0]
l = [240, 60, 2, 0, 0, 0, 0, 0]
l = [241, 60, 2, 0, 0, 0, 0, 0]
l = [242, 60, 2, 0, 0, 0, 0, 0]
l = [243, 60, 2, 0, 0, 0, 0, 0]
l = [244, 60, 2, 0, 0, 0, 0, 0]
l = [245, 60, 2, 0, 0, 0, 0, 0]
l = [246, 60, 2, 0, 0, 0, 0, 0]
l = [247, 60, 2, 0, 0, 0, 0, 0]
l = [248, 60, 2, 0, 0, 0, 0, 0]
l = [249, 60, 2, 0, 0, 0, 0, 0]
l = [250, 60, 2, 0, 0, 0, 0, 0]
l = [251, 60, 2, 0, 0, 0, 0, 0]
l = [252, 60, 2, 0, 0, 0, 0, 0]
l = [253, 60, 2, 0, 0, 0, 0, 0]
l = [254, 60, 2, 0, 0, 0, 0, 0]
l = [255, 60, 2, 0, 0, 0, 0, 0]
l = [0, 61, 2, 0, 0, 0, 0, 0]
CodePudding user response:
One option in pure python:
l = [253, 59, 2, 0, 0, 0, 0, 0]
pos = 0
n_iter = 10
step = 0
for step in range(n_iter):
for pos in range(len(l)):
if l[pos] < 255:
l[pos] = 1
break
else:
l[pos] = 0
print(f'step {step 1}: {l}')
Output:
step 1: [254, 59, 2, 0, 0, 0, 0, 0]
step 2: [255, 59, 2, 0, 0, 0, 0, 0]
step 3: [0, 60, 2, 0, 0, 0, 0, 0]
step 4: [1, 60, 2, 0, 0, 0, 0, 0]
step 5: [2, 60, 2, 0, 0, 0, 0, 0]
step 6: [3, 60, 2, 0, 0, 0, 0, 0]
step 7: [4, 60, 2, 0, 0, 0, 0, 0]
step 8: [5, 60, 2, 0, 0, 0, 0, 0]
step 9: [6, 60, 2, 0, 0, 0, 0, 0]
step 10: [7, 60, 2, 0, 0, 0, 0, 0]
CodePudding user response:
with loop,
array = [253, 59, 2, 0, 0, 0, 0, 0]
index = 0
while index < len(array):
while array[index]<256:
array[index] = 1
print(array)
array[index] = 0
index = 1
CodePudding user response:
One way with a loop:
def inc_list(l):
for i in range(len(l)):
l[i] = 1
if l[i] > 255:
l[i] = 0
else:
return
for _ in range(300):
print(l)
inc_list(l)
Alternatively, a recursive solution:
def inc_list(l):
if not l:
return []
elif l[0] == 255:
return [0] inc_list(l[1:])
else:
return [l[0] 1] l[1:]
for _ in range(300):
print(l)
l = inc_list(l)