how to merge two lists into list of lists WITHOUT using zip/ map

Question:

First time asking a question sorry if I’ve missed something! I have to open a text file turn separate each word in the file and put it into a list i then have to do a count though the list and count how often each word is used and then combine these into list of lists.

so far i have managed to do all of the above except for merging them

my result for a small text file looks like –
[([‘archastronomer’, ‘bronze’, ‘craft’, ‘craftsman’, ‘dactylos’, ‘eyes’, ‘fish’, ‘gold’, ‘leather’, ‘silver’], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1])]

whereas i need [‘archastronomer’,1], [‘bronze’,1] etc

  #noSpaces being my list of words, wordFreq list of frequency of words
  for w in noSpaces: 

      wordFreq.append(noSpaces.count(w))


   wordList = [noSpaces] 
   freqList= [wordFreq]  #wordFreq list of frequency of words

   result= []
   for i in wordList:
      for j in freqList:
         result.append((i,j))
   print result


   mergedLists= wordList + freqList
   print(list(mergedLists))

I cannot use zip either, any help would be greatly appreciated.

Asked By: Jessica Issanchon

||

Answers:

Use an index instead of iterating through the word list and freq list with for loops.

for a in range(len(wordList)):
    some_list.append(wordList[a], freqList[a])
Answered By: TLOwater

Something like this:

  result = []
  for w in noSpaces:
      result.append([w, noSpaces.count(w)])

or, if you want to keep the first part and supposing that both list have the same leght:

result = [ [wordList[i], freqList[i]] for i in range(len(wordList)) ]
Answered By: VMRuiz

METHOD 1

a=[1,2,3]
b=['a','b','c']
c=[(i, j) for i, j in zip(a, b)]
print(c)

METHOD 2

a=[1,2,3]
b=['a','b','c']
print(list(zip(a, b)))

METHOD 3

e=[]
for i in range(len(a)):
e.insert(i,(a[i], b[i]))
print(e)

METHOD 4

a=[1,2,3]
b=['a','b','c']
c=[(i, j) for i, j in zip(a, b)]



   
Answered By: VIKRAM KRRISHNA
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.