Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.
examplelist = [0,2,1,0,0]
zerolist = []
for i, j in enumerate(examplelist):
if j == 0:
zerolist.append(i)
elif j == 1:
examplelist[i]=0
shuffle(zerolist)
if zerolist:
examplelist[zerolist[0]]=1
zeropos = [ idx for idx, val in enumerate(examplelist) if val == 0
# result is [0, 3, 4]
examplelist = [0,2,1,0,0]
examplelist.index(1)
>>> 2
examplelist[examplelist.index(1)] = 0
import random
# get a random choize out of the positions containing zero
pos_to_change = random.choice(zerpos)
# now set this value to 1
examplelist[pos_to_change] = 1
import random
def find_one_and_move_to_a_zero(alist):
""" function moving a 1 value to a random zero value of the list """
# find 1 position
one_pos = alist.index(1)
# find 0 positions
zero_pos_lst = [ idx for idx, val in enumerate(alist) if val == 0 ]
# clear 1
alist[one_pos] = 0
# determine random 0 entry
chosen_zero_pos = random.choice(zero_pos_lst)
# set one value
alist[chosen_zero_pos] = 1
example_list = [2,0,0,2,1]
print("Before %r" % example_list)
find_one_and_move_to_a_zero(example_list)
print("After %r" % example_list)
#!/usr/bin/env python
import random
def find_one_and_move_to_a_zero(alist):
""" function moving a 1 value to a randmo zero value of the list """
# find 1 position. No error checking. There MUST be a 1 in alist
one_pos = alist.index(1)
# set 1 to 0 # move next line to end of function if you want to move
# the 1 to the next 0 if the one was on a position left of the first 0
alist[one_pos] = 0
# find first 0 position.
zero_pos = alist.index(0)
alist[zero_pos] = 1
example_list = [2,0,0,2,1]
print("Before %r" % example_list)
find_one_and_move_to_a_zero(example_list)
print("After %r" % example_list)
[0,1,0,2,0]
then the processed list should look like this:[0,0,1,2,0]
My item 1 must always move forward to the next available 0 but your solution seems to pick first available 0 and show the list like this:[1,0,0,2,0]
examplelist = [0,0,1,0,0]
print examplelist
zero1 = []
zero2 = []
for i, j in enumerate(examplelist):
if j == 1:
x = i
for i, j in enumerate(examplelist):
if i > x and j == 0:
zero1.append(i)
for i, j in enumerate(examplelist):
if i < x and j == 0:
zero2.append(i)
if zero1:
examplelist[zero1[0]]=1
examplelist[x]=0
elif zero2:
examplelist[zero2[0]]=1
examplelist[x]=0
print examplelist
a=[2,1,0,2,0]
oneposn=a.index(1)
nextzeroposn=a[oneposn+1:].index(0)
a[nextzeroposn+oneposn+1], a[oneposn] = 1,0
a
[2, 0, 1, 2, 0]
a=[2,0,0,2,1]
#a=[2,1,0,2,0]
if 1 in a and 0 in a:
oneposn=a.index(1)
try:
nextzeroposn=a[oneposn+1:].index(0)+oneposn+1
except:
nextzeroposn=a[:oneposn].index(0)
a[nextzeroposn], a[oneposn] = 1,0
a=[2,0,0,2,1]
#a=[2,1,0,2,0]
if 1 in a and 0 in a:
oneposn=a.index(1)
try:
nextzeroposn=a.index(0,oneposn+1)
except:
nextzeroposn=a[:oneposn].index(0)
a[nextzeroposn], a[oneposn] = 1,0
If you are experiencing a similar issue, please ask a related question
Title | # Comments | Views | Activity |
---|---|---|---|
topping3 challenge | 14 | 78 | |
silent install of security banner via msiexec command | 4 | 86 | |
Path to Python | 9 | 118 | |
Advice on Xojo as a development tool over VB. | 4 | 52 |
Join the community of 500,000 technology professionals and ask your questions.
Connect with top rated Experts
11 Experts available now in Live!