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()
Related
For example, I have 2 lists:
list1 = [6,6,6,6,6,6,6]
list2 = [0,2,4]
If there are the same indexes in the list1 and list2, I need to remove these indexes from the list1, because I should sum the unique indexes from the list1, for example:
a = [1,2,3,4,5]
b = [0,2,4]
x = [a.index(i) for i in a]
y = [b.index(j) for j in b]
for idx in y:
if idx in x:
x.remove(idx)
print(sum(x))
printed is >> 7
I tried this but did not work if there are the same values in list1
a = [6,6,6,6,6,6,6]
b = [0,2,4]
x = [a.index(i) for i in a]
y = [b.index(j) for j in b]
for idx in y:
if idx in x:
x.remove(idx)
printed is >> 0
Indexes and values are different. There will never be the same index twice in one list. You get their index by their value, however index(value) function gives you the first index which matches your value. Have a look at:
a, b, x = [1,2,3,4,5,6,7], [1,2,3], 0
c, d = len(a), len(b)
if d < c:
d, c = len(a), len(b)
for i in range(c, d):
x += i
print(x)
Your question is not very clear, so here are two answers:
If you want to sum the elements from the first list that do not appear in the second list, here is a way to do it:
a = [1,2,3,4,5]
b = [0,2,4]
# We create a set in order to have O(1) operations to check if an element is in b
b_set = set(b)
# We sum on the values of a that are not in b
res = sum(x for x in a if x not in b_set)
print(res)
>>> 9
If you want to sum the elements of the first list that do not have their rank/index in the second list, a way to do that could be:
a = [1,2,3,4,5]
b = [0,2,4]
# We create a set in order to have O(1) operations to check if an element is in b
b_set = set(b)
# We sum on the values of a that don't have their rank/index in b
res = sum(x for (i, x) in enumerate(a) if i not in b_set)
print(res)
>>> 6
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.
I have a list of of values and would like to print it in such a way that after a certain number of elements pprint forces a new line. I'm working with a dynamic data set and it's not possible to predict what the width of each new desired line will be. The elements themselves are exclusively strings and represent a boolean value. This is the desired effect:
[ o x x x o
x o x x x
x x o x o
x o x o x
x x x x o ]
You can use textwrap for that. The specifics will depend on your code & what you want but here's an example:
import random
import textwrap
some_booleans = [
random.choice([True, False]) for _ in range(30)
]
stringified = ''.join('x' if elem else 'o' for elem in some_booleans)
adjusted_lines = textwrap.wrap(stringified, width=10)
to_print = '\n'.join(adjusted_lines)
print(to_print)
Output:
oxoxoxoxxo
xxxxooxooo
xxxxoxxxoo
I wrote a python program that has as input a matrix, in which, each element appears in each row and column once. Elements are only positive integers.
e.g.
0,2,3,1
3,1,0,2
1,3,2,0
2,0,1,3
Then i find all possible traversals. They are defined as such:
choose an element from the first column
move on to the next column and
choose the element that is not in the same line from previous elements in traversal and the element has not the same value with previous elements in traversal.
e.g.
0,*,*,*
*,*,*,2
*,3,*,*
*,*,1,*
I have constructed the code that finds the traversals for matrices 4x4, but i have trouble generalizing it for NxN matrices. My code follows below. Not looking for a solution, any tip would be helpful.
import sys # Import to input arguments from cmd.
import pprint # Import for a cool print of the graph
import itertools # Import to find all crossings' combinations
# Input of arguments
input_filename = sys.argv[1]
# Create an empty graph
g = {}
# Initialize variable for the list count
i = 0
# Opens the file to make the transfer into a matrix
with open(input_filename) as graph_input:
for line in graph_input:
# Split line into four elements.
g[i] = [int(x) for x in line.split(',')]
i += 1
# Initialize variable
var = 0
# Lists for the crossings, plus rows and cols of to use for archiving purposes
f = {}
r = {}
c = {}
l = 0
# Finds the all the crossings
if len(g) == 4:
for x in range (len(g)):
for y in range (len(g)):
# When we are in the first column
if y == 0:
# Creates the maximum number of lists that don't include the first line
max_num = len(g) -1
for z in range (max_num):
f[l] = [g[x][y]]
r[l] = [x]
c[l] = [y]
l += 1
# When on other columns
if y != 0:
for z in range(len(g)):
# Initializes a crossing archive
used = [-1]
for item in f:
# Checks if the element should go in that crossing
if f[item][0] == g[x][0]:
if g[z][y] not in f[item] and z not in r[item] and y not in c[item] and g[z][y] not in used:
# Appends the element and the archive
f[item].append(g[z][y])
used.append(g[z][y])
r[item].append(z)
c[item].append(y)
# Removes unused lists
for x in range (len(f)):
if len(f[x]) != len(g):
f.pop(x)
#Transfers the value from a dictionary to a list
f_final = f.values()
# Finds all the combinations from the existing crossings
list_comb = list(itertools.combinations(f_final, i))
# Initialize variables
x = 0
w = len(list_comb)
v = len(list_comb[0][0])
# Excludes from the combinations all invalid crossings
while x < w:
# Initialize y
y = 1
while y < v:
# Initialize z
z = 0
while z < v:
# Check if the crossings have the same element in the same position
if list_comb[x][y][z] == list_comb[x][y-1][z]:
# Removes the combination from the pool
list_comb.pop(x)
# Adjust loop variables
x -= 1
w -= 1
y = v
z = v
z += 1
y += 1
x += 1
# Inputs the first valid solution as the one to create the orthogonal latin square
final_list = list_comb[0]
# Initializes the orthogonal latin square matrix
orthogonal = [[v for x in range(v)] for y in range(v)]
# Parses through the latin square and the chosen solution
# and creates the orthogonal latin square
for x in range (v):
for y in range (v):
for z in range (v):
if final_list[x][y] == g[z][y]:
orthogonal[z][y] = int(final_list[x][0])
break
# Initializes the orthogonal latin square matrix
gr_la = [[v for x in range(v)] for y in range(v)]
# Creates the greek-latin square
for x in range (v):
for y in range (v):
coords = tuple([g[x][y],orthogonal[x][y]])
gr_la[x][y] = coords
pprint.pprint(gr_la)
Valid traversals for the 4x4 matrix above are:
[[0123],[0312],[3210],[3021],[1203],[1032],[2130],[2301]]
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))