user5406649
user5406649

Reputation:

How can I create a list that is created using the indexes and items of two other lists?

I have two lists. One is made up of positions from a sentence and the other is made up of words that make up the sentence. I want to recreate the variable sentence using poslist and wordlist.

recreate = []
sentence = "This and that, and this and this."
poslist = [1, 2, 3, 2, 4, 2, 5]
wordlist = ['This', 'and', 'that', 'this', 'this.']

I wanted to use a for loop to go through poslist and if the item in poslist was equal to the position of a word in wordlist it would append it to a new list, recreating the original list. My first try was:

for index in poslist:
    recreate.append(wordlist[index])
print (recreate)

I had to make the lists strings to write the lists into a text file. When I tried splitting them again and using the code shown above it does not work. It said that the indexes needed to be slices or integers or slices not in a list. I would like a solution to this problem. Thank you.

The list of words is gotten using:

sentence = input("Enter a sentence >>") #asking the user for an input
sentence_lower = sentence.lower() #making the sentence lower case
wordlist = [] #creating a empty list
sentencelist = sentence.split() #making the sentence into a list

for word in sentencelist: #for loop iterating over the sentence as a list
    if word not in wordlist: 
        wordlist.append(word)

txtfile = open ("part1.txt", "wt")
for word in wordlist:
    txtfile.write(word +"\n")
txtfile.close()

txtfile = open ("part1.txt", "rt")
for item in txtfile:
    print (item)
txtfile.close()
print (wordlist)

And the positions are gotten using:

poslist = []

textfile = open ("part2.txt", "wt")
for word in sentencelist:
    poslist.append([position + 1 for position, i in enumerate(wordlist) if i == word])

print (poslist)
str1 = " ".join(str(x) for x in poslist)

textfile = open ("part2.txt", "wt")
textfile.write (str1)
textfile.close()

Upvotes: 1

Views: 376

Answers (4)

Julien Spronck
Julien Spronck

Reputation: 15423

First, your code can be simplified to:

sentence = input("Enter a sentence >>") #asking the user for an input
sentence_lower = sentence.lower() #making the sentence lower case
wordlist = [] #creating a empty list
sentencelist = sentence.split() #making the sentence into a list

with open ("part1.txt", "wt") as txtfile:
    for word in sentencelist: #for loop iterating over the sentence as a list
        if word not in wordlist: 
            wordlist.append(word)
            txtfile.write(word +"\n")

poslist = [wordlist.index(word) for word in sentencelist]
print (poslist)

str1 = " ".join(str(x) for x in poslist)
with open ("part2.txt", "wt") as textfile:
    textfile.write (str1)

In your original code, poslist was a list of lists instead of a list of integers.

Then, if you want to reconstruct your sentence from poslist (which is now a list of int and not a list of lists as in the code you provided) and wordlist, you can do the following:

sentence = ' '.join(wordlist[pos] for pos in poslist)

Upvotes: 0

pzp
pzp

Reputation: 6597

You can use operator.itemgetter() for this.

from operator import itemgetter

poslist = [0, 1, 2, 1, 3, 1, 4]
wordlist = ['This', 'and', 'that', 'this', 'this.']

print(' '.join(itemgetter(*poslist)(wordlist)))

Note that I had to subtract one from all of the items in poslist, as Python is a zero-indexed language. If you need to programmatically change poslist, you could do poslist = (n - 1 for n in poslist) right after you declare it.

Upvotes: 0

Julien Spronck
Julien Spronck

Reputation: 15423

You can also do it using a generator expression and the string join method:

sentence = ' '.join(wordlist[pos-1] for pos in poslist if pos if pos <= len(wordlist))
# 'This and that, and this and this.'

Upvotes: 0

L3viathan
L3viathan

Reputation: 27273

Lists are 0-indexed (the first item has the index 0, the second the index 1, ...), so you have to substract 1 from the indexes if you want to use "human" indexes in the poslist:

for index in poslist:
    recreate.append(wordlist[index-1])
print (recreate)

Afterwards, you can glue them together again and write them to a file:

with open("thefile.txt", "w") as f:
    f.write("".join(recreate))

Upvotes: 1

Related Questions