Python3 cross multiplication among tuples - python-3.x

I need to multiply the number in each tuple, not the order i[0] * j[0] and i[1] * j[1], but i[0] * i[0], i[0] * j[1], i[0] * j[2] and so on.
Moreover, I need to add the number as well, such as i[0] + i[0], i[0] + j[1], i[0] + j[2] and so on.
Is there an easy way to do this, instead of my code below that needs a lot of for?
dice1 = (1, 2, 3, 4)
dice2 = (1, 2, 3, 4, 5, 6, 7, 8)
dice3 = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
dice4 = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
dice5 = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15)
dice6 = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20)
myList = []
comp = []
d = 0
e = 0
for i in dice1:
for j in dice2:
d = i * j
myList.append(d)
e = len(myList)
comp.append(e)

You can utilize the itertools product function as follows:
from itertools import product
dice1 = (1, 2, 3, 4)
dice2 = (1, 2, 3, 4, 5, 6, 7, 8)
dice3 = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
dice4 = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
dice5 = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15)
dice6 = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20)
myList = []
comp = []
myList = [k[0] * k[1] for k in product(dice1, dice2)]
comp.append(len(myList)

Related

Apply if statement on multiple lists with multiple conditions

I would like to append ids to a list which meet a specific condition.
output = []
areac = [4, 4, 4, 4, 1, 6, 7,8,9,6, 10, 11]
arean = [1, 1, 1, 4, 5, 6, 7,8,9,10, 10, 10]
id = [1, 2, 3, 4, 5, 6, 7,8,9,10, 11, 12]
dist = [2, 2, 2, 4, 5, 6, 7.2,5,5,5, 8.5, 9.1]
for a,b,c,d in zip(areac,arean,id,dist):
if a >= 5 and b==b and d >= 3:
output.append(c)
print(comp)
else:
pass
The condition is the following:
- areacount has to be >= 5
- At least 3 ids with a distance of >= 3 with the same area_number
So the id output should be [10,11,12].I already tried a different attempt with Counter that didn't work out. Thanks for your help!
Here you go:
I changed the list names to something more descriptive.
output = []
area_counts = [4, 4, 4, 4, 1, 6, 7, 8, 9, 6, 10, 11]
area_numbers = [1, 1, 1, 4, 5, 6, 7, 8, 9, 10, 10, 10]
ids = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
distances = [2, 2, 2, 4, 5, 6, 7.2, 5, 5, 5, 8.5, 9.1]
temp_numbers, temp_ids = [], []
for count, number, id, distance in zip(counts, numbers, ids, distances):
if count >= 5 and distance >= 3:
temp_numbers.append(number)
temp_ids.append(id)
for (number, id) in zip(temp_numbers, temp_ids):
if temp_numbers.count(number) == 3:
output.append(id)
output will be:
[10, 11, 12]

Python for loop one-line difference from regullar

I just came up with a difference between one line and regular for loop.
As an example;
obs = [6, 12, 8, 10, 20 16]
freq = [5, 4, 3, 2, 1, 5]
data = []
data.extend(obs[i:i+1] * freq[i] for i in range(len(obs)))
outputs
[[6, 6, 6, 6, 6], [12, 12, 12, 12], [8, 8, 8], [10, 10], [20], [16, 16, 16, 16, 16]]
However,
for i in range(len(obs)):
data.extend(obs[i:i+1] * freq[i])
outputs
[6, 6, 6, 6, 6, 12, 12, 12, 12, 8, 8, 8, 10, 10, 20, 16, 16, 16, 16, 16]
Can someone kindly explain what causes this?
extending x by y means appending each entry of y to x.
Since the entries of obs[i:i+1] * freq[i] for i in range(len(obs)) are lists of integers, data.extend(obs[i:i+1] * freq[i] for i in range(len(obs))) will append of lists of integers to data, not integers.
On the other hand, the elements of obs[i:i+1] * freq[i] are integers, and therefore data.extend(obs[i:i+1] * freq[i]) will append integers to data.
This would generate the same output as the one-liner:
obs = [6, 12, 8, 10, 20, 16]
freq = [5, 4, 3, 2, 1, 5]
data = []
for i in range(len(obs)):
data.extend([obs[i:i+1] * freq[i]])

Creating a graph in VTK

I want to create a graph using VTK class https://lorensen.github.io/VTKExamples/site/Python/Graphs/ConstructGraph/
def main():
g = vtk.vtkMutableUndirectedGraph()
v1 = g.AddVertex()
v2 = g.AddVertex()
g.AddEdge ( v1, v2 )
print ('Number of vertices:', g.GetNumberOfVertices())
print ("Number of edges:", g.GetNumberOfEdges())
g.AddEdge ( v1, v2 )
print ('Number of vertices:', g.GetNumberOfVertices())
print ('Number of edges:', g.GetNumberOfEdges())
graphLayoutView = vtk.vtkGraphLayoutView()
graphLayoutView.AddRepresentationFromInput(g)
graphLayoutView.ResetCamera()
graphLayoutView.Render()
graphLayoutView.GetInteractor().Start()
is the function defined. But I not quite sure how to use this.
For instance, in networkx I would do
t = [0, 2, 4, 5, 6, 8, 9, 11, 13, 1, 10, 1, 3, 7]
h= [1, 3, 3, 1, 7, 7, 10, 12, 12, 14, 14, 12, 14, 10]
ed_ls = [(x, y) for x, y in zip(tail, head)]
G = nx.OrderedGraph()
G.add_edges_from(ed_ls)
nx.draw(G)
Suggestions on how to create the same graph in VTK will be helpful
You already found the correct API. Just have to use it. Something like
g = vtk.vtkMutableUndirectedGraph()
# add your 15 vertices
for i in range(15):
g.AddVertex()
t = [0, 2, 4, 5, 6, 8, 9, 11, 13, 1, 10, 1, 3, 7]
h= [1, 3, 3, 1, 7, 7, 10, 12, 12, 14, 14, 12, 14, 10]
for start, end in zip(t,h):
g.AddEdge( start, end )

To find unit digit at a particular index of fibonacci series in O(1) time. (fibonacci series may be <=10^18)

This code give wrong output above 10^7 input. can any body help me to solve this problem?
from math import sqrt,floor,log
def fib(N):
var = (1 + sqrt(5)) / 2
return round(pow(var, N) / sqrt(5))
test = int(input())
a=floor(log(test,2))
b=2**a
a=b%60
print(fib(a-1)%10)
Fibonacci series has a cycle of 60 for its unit digit (without getting deep into map you can see that after 60 you get 1 and 1 again, so the sum would be 2 and so on).
Therefore, you can prepare a list of these Fibonacci unit digits:
fib_digit = [1, 1, 2, 3, 5, 8, 3, 1, 4, 5, 9, 4, 3, 7, 0, 7, 7, 4, 1, 5, 6, 1, 7, 8, 5, 3, 8, 1, 9, 0, 9, 9, 8, 7, 5, 2, 7, 9, 6, 5, 1, 6, 7, 3, 0, 3, 3, 6, 9, 5, 4, 9, 3, 2, 5, 7, 2, 9, 1, 0]
and return fib_digit[N % 60] in O(1).

How would I add numbers between 1 and 20 to a list using a for loop without using range()

Is there a replacement for the range command that can be used in for loop to add the numbers between 1 and 20 to a list.
Simple for loop
i = 1
l = []
while i <= 20:
l.append(i)
i = i + 1
Outputs
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
You can use np.linspace(1, 20, num=20)
If we are to avoid using a builtin as crucial as range, we might as well have some fun doing so.
l = []
while len(l) < 20:
l.append(len(l) + 1)
print(l)
Output
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]

Resources