suppose you have a 3D figure that I have attached the screenshot in the below, how to fill the number in this 3d and plot it out, your kind response will be appreciated.
how to plot the below image in python with filled data which is already filled in the below
You are looking at a multi dimensional Jagged array.
Notice the following:
face 1: 4 x 5 (front)
face 2: 3 x 5 (top)
face 3: 3 x 3 (left)
You can immediately deduce the other 3 faces of course
face 4: 4 x 5 (back)
face 5: 3 x 5 (bottom)
face 6: 3 x 3 (right)
After you got that, you just have to create the arrays according to the size specified. There are many ways to go about doing this but i will stick to the most basic.
For instance to get the front and back,
for i in range(0,2):
for j in range(0,4):
x = []
for k in range(0,5):
x.append(0)
arr.append(x)
x = []
JointArr.append(arr)
arr = []
You iterate twice (one for front and one for back) and then iterate according to the dimension (4x5). Therefore, the whole implementation will look something like this:
JointArr = []
arr = []
for i in range(0,2):
for j in range(0,4):
x = []
for k in range(0,5):
x.append(0)
arr.append(x)
x = []
JointArr.append(arr)
arr = []
arr = []
for i in range(0,2):
for j in range(0,3):
x = []
for k in range(0,5):
x.append(1)
arr.append(x)
x = []
JointArr.append(arr)
arr = []
arr = []
for i in range (0,2):
for j in range(0,3):
x = []
for k in range(0,3):
x.append(2)
arr.append(x)
x = []
JointArr.append(arr)
arr = []
print(JointArr)
This will give you something like:
This is a simplified version of course. You can consider using numpy or other shorthands after you get the gist of what is going on.
Related
np.random.seed(123456)
X = np.random.normal(0,1,1000)
Y = np.random.normal(0,1,1000)
Z = np.random.normal(0.5, 1.7,1000)
W = np.random.normal(0,1,1000)
stream_A = np.concatenate((X,Y,Z,W))
Then I am running the code below: Basically I need to create a iterator to feed one sample at a time to another function.
# 4 chunks of 1000 samples, so X,Y,W and Z arrays
n = 4
iter_array = iter(stream_A) # Size 4000
result = [[] for _ in range(n)]
for _ in itertools.repeat(None, 1000):
for i in range(n):
result[i].append(next(iter_array))
The problem is:
results[0] is a list with all elements of stream X defined above.
If a compare results[0] == X I get false
If I transform the list into np.array:
y=np.array([np.array(xi) for xi in result], dtype=object)
and then:
y[0] == X I also get false
Can someone help me why I am getting False?
And they are somehow not same because the results I am getting when I apply X to the function is not the same of them result when I apply y[0] to the same function.
Just stack them:
stream_A = np.stack((X,Y,Z,W))
stream_A.shape
# (4, 1000)
np.all(stream_A[0] == X)
# True
I am trying to generate a grid symmetric or asymmetric based on the given length L
when L is a perfect square
L = 4, m=n=2
L = 9, m=n=3
but if L is not a perfect square
L = 6, m=3,n=2
L = 7, m=4,n=2
I have the following code, if it helps others or if one could suggest improvements:
def get_rows_cols(self):
sr = math.sqrt(len(self._data.columns))
numberOfCols = math.floor(sr)
numberOfRows = 0
if ((sr - math.floor(sr)) == 0):
numberOfRows = math.floor(sr)
else:
diff = (len(self._data.columns)-1) - pow(numberOfCols,2)
numberOfRows = (diff / numberOfCols) + 1 + numberOfCols
n = numberOfCols
m = int(numberOfRows)
So, you have determined your numberOfCols and numberOfRows. If all you want to do is make a grid, you could do it in a pandas dataframe (I'll let you look that one up), or go old-school and just have a list of lists:
rows = []
one_row = [0] * numberOfCols
for i in range(numberOfRows):
rows.append(one_row)
This will give you a list of lists, where you can access a cell by using the row index and the col index: row[desired_row][desired_col]
Hope that helps, happy coding!
I have a the following code (as an example):
answ = []
for i in range(1, 3):
x = y + 2
y = 3 + x
answ.append(y)
Where x and y are simultaneously determined. How can I determine them simultaneously? Or how can I assume that for the first loop y=0 (so x will equal to 2) and then starting from the second iteration 'y' = 3 + x.
Just set y to 0 before the for loop:
answ = []
y = 0
for i in range(1, 3):
x = y + 2
y = 3 + x
append.answ(y)
Like this:
x, y = y + 2, 3 + x
Right now, x and y are local variables, meaning they are deleted at the end of each iteration of the for loop. If you want them to carry over from the previous iteration, you need to define x and y outside of the for loop. This puts them above and the scope of the loop, so they don't get redefined.
Another thing, your for loop runs 2 times, because computers count from 0 and don't include the last number. Because i is not used in your loop at all, it would be better to do:
for i in range(2):
# code
But I am guessing that you want your loop to run three times, in which case you would write:
for i in range(3):
# code
I also noticed that you wrote append.answ(y) instead of answ.append(y). append is a member function of answ, so you would call it the second way.
Anyways, here is the final code for your program:
answ = []
x = 0
y = 0
for i in range(3):
x = y + 2
y = 3 + x
answ.append(y)
Instructions: Compute and store R=1000 random values from 0-1 as x. moving_window_average(x, n_neighbors) is pre-loaded into memory from 3a. Compute the moving window average for x for the range of n_neighbors 1-9. Store x as well as each of these averages as consecutive lists in a list called Y.
My solution:
R = 1000
n_neighbors = 9
x = [random.uniform(0,1) for i in range(R)]
Y = [moving_window_average(x, n_neighbors) for n_neighbors in range(1,n_neighbors)]
where moving_window_average(x, n_neighbors) is a function as follows:
def moving_window_average(x, n_neighbors=1):
n = len(x)
width = n_neighbors*2 + 1
x = [x[0]]*n_neighbors + x + [x[-1]]*n_neighbors
# To complete the function,
# return a list of the mean of values from i to i+width for all values i from 0 to n-1.
mean_values=[]
for i in range(1,n+1):
mean_values.append((x[i-1] + x[i] + x[i+1])/width)
return (mean_values)
This gives me an error, Check your usage of Y again. Even though I've tested for a few values, I did not get yet why there is a problem with this exercise. Did I just misunderstand something?
The instruction tells you to compute moving averages for all neighbors ranging from 1 to 9. So the below code should work:
import random
random.seed(1)
R = 1000
x = []
for i in range(R):
num = random.uniform(0,1)
x.append(num)
Y = []
Y.append(x)
for i in range(1,10):
mov_avg = moving_window_average(x, n_neighbors=i)
Y.append(mov_avg)
Actually your moving_window_average(list, n_neighbors) function is not going to work with a n_neighbors bigger than one, I mean, the interpreter won't say a thing, but you're not delivering correctness on what you have been asked.
I suggest you to use something like:
def moving_window_average(x, n_neighbors=1):
n = len(x)
width = n_neighbors*2 + 1
x = [x[0]]*n_neighbors + x + [x[-1]]*n_neighbors
mean_values = []
for i in range(n):
temp = x[i: i+width]
sum_= 0
for elm in temp:
sum_+= elm
mean_values.append(sum_ / width)
return mean_values
My solution for +100XP
import random
random.seed(1)
R=1000
Y = list()
x = [random.uniform(0, 1) for num in range(R)]
for n_neighbors in range(10):
Y.append(moving_window_average(x, n_neighbors))
I have a list with several nested lists inside like this:
MyMasterListwithListsInside = [List1,List2,List3,List4]
List1 = [f,e,g,t]
List2 = [t,r,e,y]
List3 = [g,k,f,k]
List4 = [o,y,[t,y]]
I am trying to have an output files like that looks like this this:
file 1
f or List1[1] \n
t or List2[1] \n
g or List3[1] \n
o or List4[1] \n
file 2
e or List1[2] \n
r or List2[2] \n
k or List3[2]\n
y or List4[2]\n
file 3
g or List1[3] \n
e or List2[3] \n
f or List3[3] \n
t or List4[3][1] \n
y or List4[3][2] \n
So far I have tried:
for x in a:
with open("whatever","a", encoding="utf-8") as file:
file.write("\n")
for y in x:
if y is not None:
file.write("\n")
file.write(y)
x.remove(y)
for f in ok:
file.write("\n")
file.write(f)
ok.remove(f)
for k in kok:
file.write("\n")
file.write(k)
kok.remove(k)
for s in sok:
file.write("\n")
file.write(s)
sok.remove(s)
for o in yok:
for ik in o:
if ik is not None:
file.write("\n")
file.write(ik)
else:
yok.remove(o)
else:
print("Done!")
I have also tried several combinations of different indentations. None of them work. Either I get List1[1:4],List2[1:4],... etc. like output or List1[1],List2[1],List3[1:4],... etc. At one point I managed to find the write combination of indenting, but then I had a syntax error, and while I was debugging, I lost the correct form. However I am sure there is more elegant solution than making a leader of "for"s.
My actual data is a list which contains several nested lists, each containing ten elements. One of them also contains 10 nested lists. I can also compromise to a format that looks like this:
f or List1[1] \n
t or List2[1] \n
g or List3[1] \n
o or List4[1] \n
e or List1[2] \n
r or List2[2] \n
k or List3[2]\n
y or List4[2]\n
g or List1[3] \n
e or List2[3] \n
f or List3[3] \n
t or List4[3][1] \n
y or List4[3][2] \n
Thanks in Advance
You could do something recursive like this (psuedocode):
for each position in a
printPosition()
function printPosition(arrays, position)
for each element in array
if array[position] != array
print array[position]
else
for each position
printPosition()
Does that make sense to you?
The solution was with itertools after all. Here is my overall function:
def metin_işle_Page(Kök):
sayfa1 = BeautifulSoup(Kök, "lxml") # Page with 10 results
sayfa = sayfa1.find_all("result") # Each of them are seperate xml #files,
#with json data in between and
#each of them having the same structure
başlıklar2 = [x.find("title") for x in sayfa]
başlıklar = [x.get_text() for x in başlıklar2] # A list for their titles 10 elements
print("Başlıklar Alındı")
kayıt_kaynağı2 = [x.find("recordsourceinfo") for x in sayfa] # a list for their id
kayıtUrl = [link.get("landingpage") for link in kayıt_kaynağı2]
kayıt_id = [link.get_text(strip=True) for link in kayıt_kaynağı2]
print("kayıt id ve ilgili urller alındı")
nesne_tipi4 = [x.find("objecttype") for x in sayfa] # another list with 10 elements
nesne_tipi = [x.get_text(strip=True) for x in nesne_tipi4]
print("nesne tipleri alındı")
malzeme3 = [x.find("material") for x in sayfa] # you get the idea ..........
malzeme = [x.get_text(strip=True) for x in malzeme3]
print("malzemeler alındı")
boyut3 = [x.find("dimensions") for x in sayfa]
boyut2 = [x.prettify(formatter="minimal") for x in boyut3]
boyut = [x.strip() for x in boyut2]
print("boyutlar alındı")
tarihi2 = [x.find("origindating") for x in sayfa]
kaynak_tarihi2 = [x.get_text(strip=True) for x in tarihi2]
kaynak_tarihi = [x.strip() for x in kaynak_tarihi2]
print("kaynak tarihleri alındı")
eski_Yer2 = [x.find("ancientfindspot") for x in sayfa]
eski_yer1 = [x.get_text("|", strip=True) for x in eski_Yer2]
eski_yer = [x.strip() for x in eski_yer1]
print("Eserin ait olduğu yer alındı")
modern_yer3 = [x.find("modernfindspot") for x in sayfa]
modern_yer1 = [x.get_text(strip=True) for x in modern_yer3]
modern_yer = [x.strip() for x in modern_yer1]
print("Eserin bulunduğu modern yer alındı")
modern_ülke3 = [x.find("moderncountry") for x in sayfa]
modern_ülke1 = [x.get_text(strip=True) for x in modern_ülke3]
modern_ülke = [x.strip() for x in modern_ülke1]
print("Eserlerin bulunduğu ülkeler alındı")
korunma_ülkesi3 = [x.find("conservationcountry") for x in sayfa]
korunma_ülkesi1 = [x.get_text("|", strip=True) for x in korunma_ülkesi3]
korunma_ülkesi = [x.strip() for x in korunma_ülkesi1]
print("Eserin korunduğu ülkeler alındı")
müzesi3 = [x.find("museum") for x in sayfa]
müzesi1 = [x.get_text("|", strip=True) for x in müzesi3]
müzesi = [x.strip() for x in müzesi1]
print("Eserin korunduğu Müze alındı")
yazıttipi3 = [x.find("inscriptiontype") for x in sayfa]
yazıttipi2 = [x.get_text(strip=True) for x in yazıttipi3]
yazıt_tipi = [x.strip() for x in yazıttipi2]
print("Yazıt tipleri alındı")
yazıt_tekniği3 = [x.find("engravingtechnique") for x in sayfa]
yazıt_tekniği2 = [x.get_text(strip=True) for x in yazıt_tekniği3]
yazıt_tekniği = [x.strip() for x in yazıt_tekniği2]
print("yazıt teknikleri alındı")
metin_normal2 = [x.find("text") for x in sayfa]
metin_normal1 = [x.get_text(strip=True) for x in metin_normal2]
metin_normal = [x.strip()for x in metin_normal1]
print("Metinler alındı")
metin_epidoc3 = [x.find("textepidoc") for x in sayfa]
metin_epidoc2 = [x.prettify(formatter="minimal") for x in metin_epidoc3]
metin_epidoc = [x.strip() for x in metin_epidoc2]
print("Epidoc metinleri alındı")
kaynakça3 = [x.find_all("bibliography") for x in sayfa] # Here is the
#tricky part for every list so far there was only 1 element beneath the tag
#corresponding in each results, but for this tag, there are
#sometimes 2 or more elements
kaynakça4 = [] # I made a new list in order to match the number of other lists.
for x in kaynakça3: # list containing more than one elements
kaynaklar = [] # some empty list
for y in x: # since x, a list of "bibliography" element for each element
# of sayfa,a list of "result" elements, i call y, each attestation of
# bibliography in x.
adf1 = y.get_text(strip=True) # I took the text of each attestation
#and reproduce them in another list. This way I got rid of the tags
# plus it is difficult to work with a Result Set, and less difficult
# to work with a list
adf = adf1.strip()
kaynaklar.append(adf)
kaynakça4.append(kaynaklar)
kaynakça = []
for g in kaynakça4: # here I tried to join together the nested lists within
# the nested list element, so that I would have at most two level of nested
#lists.
zip(g)
kaynakça.append(g)
Genel_sayfa = [] # Then I created a master list and appended my processed
Genel_sayfa.append(başlıklar) #elements within it.
Genel_sayfa.append(kayıt_id)
Genel_sayfa.append(kayıtUrl)
Genel_sayfa.append(nesne_tipi)
Genel_sayfa.append(malzeme)
Genel_sayfa.append(boyut)
Genel_sayfa.append(kaynak_tarihi)
Genel_sayfa.append(eski_yer)
Genel_sayfa.append(modern_yer)
Genel_sayfa.append(modern_ülke)
Genel_sayfa.append(korunma_ülkesi)
Genel_sayfa.append(yazıt_tekniği)
Genel_sayfa.append(yazıt_tipi)
Genel_sayfa.append(metin_normal)
Genel_sayfa.append(metin_epidoc)
Genel_sayfa.append(kaynakça)
Sıralı = itertools.chain.from_iterable(zip(* Genel_sayfa)) #used iterate tools
sayfasayısı = list(range(0,112)) #over the lists which contain the same number
for SayfaNo in sayfasayısı: #of elements
with open("TümSayfa" + str(SayfaNo), "a", encoding="utf-8") as sonuç:
sonuç.write("\n")
for k in Sıralı:
sonuç.write("\n")
sonuç.write("\n")
afrc = str(k) #to assure that there was no problem in the output
sonuç.write("\n") # I changed the chain object to string
sonuç.write(afrc)
sonuç.close()