python - Creating a new list from 2 lists based on the index return from a third list -
say have following lists (python 3):
numbers = [1,2,3,4,5,6] letters = [a,b,a,b,c,c] state = [false, false, false, false, false, false]
what looking receive 2 inputs user 2 index positions within range of length of list letters. if case choices correspond matching letters such , (index 0 , 2), there must change in list state false true said positions. afterwards, should create new list depending on new state list, getting index item numbers if element in state false , getting index item list letters if state true:
choice = 0 choice_2 = 2 if letters[choice] == letters[choice_2]: change state[choice] , state[choice_2] true create fourth list list state , use values numbers , letters in range(len(state)): if state[i] == true: element in index[i] of list letters used else: element in index[i] of list numbers used
creating new list such that:
new_list = [a,2,a,4,5,6]
numbers = [1,2,3,4,5,6] letters = ["a","b","a","b","c","c"] state = [false, false, false, false, false, false] choice1 = input("enter 1st choice: ") choice2 = input("enter 2nd choice: ") if letters[choice1] == letters[choice2]: state[choice1] = (not state[choice1]) state[choice2] = (not state[choice2]) list = [] print state in range(len(state)): if state[i] == true: list.append(letters[i]) else: list.append(numbers[i]) print list
input:
enter 1st choice: 0 enter 1st choice: 2
output:
['a', 2, 'a', 4, 5, 6]
Comments
Post a Comment