-2

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

K.E.
  • 41
  • 1
  • 5
  • http://stackoverflow.com/q/13908518/3001761 – jonrsharpe Jun 02 '16 at 22:17
  • This question makes no sense at all. Those aren't nested lists, they're strings. You also have some very strange logic, like printing every element at a corresponding position on its own line, _unless_ that element happens to look like a list then it takes up `n` lines (for its `n` members)? What is really going on? – Two-Bit Alchemist Jun 02 '16 at 22:22
  • you're right my bad, see correction. – K.E. Jun 02 '16 at 23:18
  • it looks *almost* like you want `itertools.chain.fromiterable(zip(* MyMasterListwithListsInside))` to generate the order you want but `List4[4]` doesn't actually exsist so I do not understand what you are actually trying to do. – Tadhg McDonald-Jensen Jun 02 '16 at 23:53
  • Right on for List4[4], correcting right away – K.E. Jun 03 '16 at 01:06
  • @TadhgMcDonald-Jensen itertools worked, the result is exactly what I have wanted, could you make it an answer so that I can mark it as one? – K.E. Jun 03 '16 at 09:55
  • uhh... are you sure it worked? Since the lists are dfferent sizes (`len(List4) == 3`) it would skip the last item of the other lists, if you flattened `List4` then it would do `y` after the fourth element of the other lists which doesn't match up with your desired output... Although if you got `itertools` to do what you want maybe post an answer yourself so I can understand what you actually wanted and maybe enhance it if I can. – Tadhg McDonald-Jensen Jun 03 '16 at 14:18
  • @TadhgMcDonald-Jensen How does it look? I know there is room for a change but this is the best I could come up with. – K.E. Jun 03 '16 at 21:19

2 Answers2

0

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?

nesooM
  • 1
0

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()
K.E.
  • 41
  • 1
  • 5
  • ok, I cannot at all see how the attempt in your question post related to the logic you were trying to apply to the lists but this seems even less relevent then the original, what on earth are you actually doing? – Tadhg McDonald-Jensen Jun 04 '16 at 00:50
  • @TadhgMcDonald-Jensen With the code I am trying to parse a response object of a webpage which generates 10 results. If each result is x, there are 10x, each of them having the exact structure. Each x, have 4 elements, call them y, so there are 10y in each x. Since every x is identical the only way to distinguish them is through their order they appear in the list of results. The same applies for each y, one can only distinguish them through their order they appear in x. What i did not do is to distinguish them accordingly by using a dictionary structure. That is why the code looks like a mess. – K.E. Jun 04 '16 at 16:15
  • ok, I see that pretty much all of this code is parsing the request but that has nothing to do with the original question you asked about "how can I go from to " So I'm just really confused what you actually wanted by asking your question... – Tadhg McDonald-Jensen Jun 04 '16 at 16:25
  • @TadhgMcDonald-Jensen Since I had not used dictionary structure for stocking the resulting data, I was looking for a solution to iterate over every element of every list in the order they appear within the list, and iterate tools actually worked. But I will rework the code to use dictionary structure, this one is too fragile to use in other similar cases. Thank you for your patience by the way. – K.E. Jun 04 '16 at 16:35