i want to create a nontransitiv dice game
A = [3, 5, 7]
B = [2, 4, 9]
C = [1, 6, 8]
choice1 = input("Chose a Dice (A/B/C)")
result1 = random.choice(choice1)
The user will enter a string like "A" and i want it to become the variable A so that the random.choice function will output a random number from the corresponding list
You could create a dictionary with the letters as keys and the arrays as values. Then, whatever the user puts, key into the dictionary.
diceDict = {
"A": [3, 5, 7],
"B": [2, 4, 9],
"C": [1, 6, 8]
}
result = random.choice(diceDict[choice1])
I don't know what language this is written in but you need to perform some sort of check on the user entered value.... e.g
(pseudocode)
if (choice1 === "A") getRandomFromArray(A)
if (choice1 === "B") getRandomFromArray(B)
.... etc...
Related
I would like to group list elements in groups of three and make the second element of the list to start the second group of elements for example;
arr = [1,2,3,4,5,6,7,8,9]
#expected output
output = [[1,2,3],[2,3,4],[3,4,5],[4,5,6],[5,6,7],[6,7,8],[7,8,9]]
How do I do this with python?
You can just use a list comprehension -
[arr[i:i+3] for i in range(len(arr)-2)]
Hello dear friend try to create new lists then append them to another list:
arr = [1,2,3,4,5,6,7,8,9]
i = 0
b = []
while i <= len(arr) - 3:
a = [arr[i],arr[i+1],arr[i+2]]
b.append(a)
i += 1
print(b)
OUTPUT
[[1, 2, 3], [2, 3, 4], [3, 4, 5], [4, 5, 6], [5, 6, 7], [6, 7, 8], [7, 8, 9]]
I have 3 lists. Is there any way to add elements from each list one by one in terraform? Like the following:
for a, b, c in UNKNOWN_FUNC([1, 2, 3], [4, 5, 6], [7, 8, 9]):
...
...
The output should be like this:
[1, 4, 7]
[2, 5, 8]
[3, 6, 9]
Note 1: I have tried zipmap, but it doesn't do the job.
Note 2: The lists would always be of the same length.
You could construct a helper list for that and then iterate over it. For example:
variable "l1" {
default = [1, 2, 3]
}
variable "l2" {
default = [4, 5, 6]
}
variable "l3" {
default = [7, 8, 9]
}
locals {
new_list = [ for idx in range(0, 3):
[ var.l1[idx], var.l2[idx], var.l3[idx] ]
]
}
And to use it in for_each:
resource "ddd" "ddd"{
for_each = {for idx, val in local.new_list: idx => val}
some_attribute1 = each.value[0]
some_attribute2 = each.value[1]
some_attribute3 = each.value[2]
I'm not fully sure I understand what your goal is, but it seems like it involves reorganizing the elements to group them by their indices.
Assuming that all of your lists will always be the same length, you could achieve that with a for expression, which is comparable to a list or map comprehension in Python.
locals {
lists = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
]
pivot = [
for i, v in local.lists[0] : [
local.lists[0][i],
local.lists[1][i],
local.lists[2][i],
]
]
}
This uses the length of the first list as a proxy for the length of all of the lists, and so this would fail if not all of the lists are of the same length.
lst = [1, 4, 5, 9, 6]
How to write a code to get some missing values which must return [2, 3, 7, 8]?
Here is my code
new_list = []
for i in range(1, len(lst1)):
if i not in lst1:
new_list.append(i)
print(new_list)
You can convert them to sets and use "-" to calculate the difference. Note that the order is important.
You need to define what you want to compare your list to if you want to find the missing elements. You can automatically do this if it is simple like "the numbers from 1 to 10" using the code:
list(range(1, 10))
The comparison code is:
lst1 = [1, 4, 5, 9, 6]
lst2 = [1, 2, 3, 4, 5, 6, 7, 8, 9]
out = list(set(lst2) - set(lst1))
print(out)
which returns
[8, 2, 3, 7]
It isn't clear exactly what you want from your question, if you give a more detail it will be easier to help.
Suppose I have a two lists:
a = [1,2,3,4,5]
b = [float]
My requirement is to just get the value of the 'a' list in 'b', i.e.
b = [1,2,3,4,5, float]
So, if we either append of insert, that will again create list of list, which I don't want.
Any suggestions
Assume your input is:
a = [1,2,3,4,5]
b = ['float']
b{:] = a+b
print (b)
The output of this will be:
[1, 2, 3, 4, 5, 'float']
If your input is:
a = [1,2,3,4,5]
b = [float]
then your output will be:
[1, 2, 3, 4, 5, <class 'float'>]
this may be a bit clumsy, but it does what you want:
a = [1, 2, 3, 4, 5]
b = ["float"]
print(id(b))
for n in reversed(a):
b.insert(0, n)
print(b) # [1, 2, 3, 4, 5, 'float']
print(id(b))
note that the id does not change - no new list is created.
slice-assignment would also work:
b0 = b[0]
b[:len(a)] = a
b.append(b0)
if you don't mind working with a deque instead of a list you could also do this:
from collections import deque
a = [1, 2, 3, 4, 5]
b = deque(["float"])
b.extendleft(reversed(a)) # deque([1, 2, 3, 4, 5, 'float'])
you can use:
a.__add__(b)
output:
[1, 2, 3, 4, 5, <class 'float'>]
How to merge two array and group by key?
Example:
my_list = [3, 4, 5, 6, 4, 6, 8]
keys = [1, 1, 2, 2, 3, 5, 7]
Expected outcome:
[[1, 3, 4], [2, 5, 6], [3, 4], [5, 6], [7, 8]]
If I understand it right, the list of keys map to the list of values. You can use the zip function to iterate through two lists at the same time. Its convenient in this case. Also check up on the beautiful defaultdict functionality - we can use it to fill a list without initialising it explicitely.
from collections import defaultdict
result = defaultdict(list) # a dictionary which by default returns a list
for key, val in zip(keys, my_list):
result[key].append(val)
result
# {1: [3, 4], 2: [5, 6], 3: [4], 5: [6], 7: [8]}
You can then go to a list (but not sure why you would want to) with:
final = []
for key, val in result.items():
final.append([key] + val) # add key back to the list of values
final
# [[1, 3, 4], [2, 5, 6], [3, 4], [5, 6], [7, 8]]
I think you have to write it by your own using set() to remove duplicates, so I have made a function called merge_group
my_list = [3, 4, 5, 6, 4, 6, 8]
keys = [1, 1, 2, 2, 3, 5, 7]
def merge_group(input_list : list, input_key : list):
result = []
i = 0
while i < len(my_list):
result.append([my_list[i], keys[i]])
i += 1
j = 0
while j < len(result):
if j+1 < len(result):
check_sum = result[j] + result[j+1]
check_sum_set = list(set(check_sum))
if len(check_sum) != len(check_sum_set):
result[j] = check_sum_set
j += 1
return result
print(merge_group(my_list, keys))