Combine multiple consecutive words in python

user8871463

I have list of lists as follows.

mycookbook= [["i", "love", "tim", "tam", "and", "chocolate", "ice", "cream"], ["cooking", 
"fresh", "vegetables", "is", "easy"], ["fresh", "vegetables", "and", "fruits", "are", "good", 
"for", "health"]]

I also have a list as follows.

mylist = ["tim tam", "chocolate ice cream", "fresh vegetables and fruits"]

Now, I want to find the consecutive words in mylist and combine them as follows to update mycookbook.

mycookbook = [["i", "love", "tim tam" "and", "chocolate ice cream"], ["cooking", "fresh vegetables", 
"is", "easy"],["fresh vegetables and fruits", "are", "good", "for", "health"]]

I am currently using two words as follows.

for sentence in mycookbook:
    i = 0
    while i < len(sentence) - 1:
        if sentence[i] + ' ' + sentence[i + 1] in mylist:
            sentence[i] += ' ' + sentence[i + 1]
            sentence.pop(i + 1)
        i += 1
print(mycookbook)
Barmar

You need nested loops, one for the starting index of a phrase, the next for the ending index. You can then use a list slice to get all the words in between.

for sentence in mycookbook:
    i = 0
    while i < len(sentence):
        for j in range(i + 1, len(sentence)+1):
            phrase = ' '.join(sentence[i:j])
            if phrase in mylist:
                sentence[i:j] = [phrase]
                break
        i += 1

We can't use for i in range(len(sentence)) because the length of sentence changes whenever we replace a slice with the phrase.

DEMO

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Combine Multiple Lists (Python)

分類Dev

Python convert list of multiple words to single words

分類Dev

Split lines with multiple words in Python

分類Dev

Python Pandas Group By Consecutive Multiple Columns

分類Dev

R: Combine consecutive rows based on the previous row

分類Dev

find words that matches the 3 consecutive vowels Regex

分類Dev

Regular expression to find a same consecutive words

分類Dev

How to combine multiple csv into one file in serial manner using python?

分類Dev

Python combine integer columns to create multiple columns with suffix

分類Dev

Combine Multiple Regex into One

分類Dev

Combine multiple replacingOccurrences() with Swift

分類Dev

Combine multiple JPEGs into one

分類Dev

Regex for "or" of multiple words in grep

分類Dev

chosen search for multiple words

分類Dev

How to combine three consecutive Modbus registers to get an integer value?

分類Dev

How can I combine consecutive keys of an array depending on their values?

分類Dev

Combine multiple queries into one query

分類Dev

Redux - how to combine multiple mapDispatchToProps?

分類Dev

How to combine the dictionaries in python

分類Dev

combine two strings in python

分類Dev

Removing nonsense words in python

分類Dev

How to count consecutive substring in a string in python 3

分類Dev

Detecting Consecutive Integers in a list [Python 3]

分類Dev

How to write to consecutive CSV columns in Python

分類Dev

Comparing consecutive Numbers in .txt file in Python

分類Dev

JAVA: How to combine message boxes with multiple outputs

分類Dev

Combine Multiple Rows And Columns Into A Single Row In SQL

分類Dev

Combine multiple SQL queries with having limit

分類Dev

Read multiple xml files in R and combine the data

Related 関連記事

  1. 1

    Combine Multiple Lists (Python)

  2. 2

    Python convert list of multiple words to single words

  3. 3

    Split lines with multiple words in Python

  4. 4

    Python Pandas Group By Consecutive Multiple Columns

  5. 5

    R: Combine consecutive rows based on the previous row

  6. 6

    find words that matches the 3 consecutive vowels Regex

  7. 7

    Regular expression to find a same consecutive words

  8. 8

    How to combine multiple csv into one file in serial manner using python?

  9. 9

    Python combine integer columns to create multiple columns with suffix

  10. 10

    Combine Multiple Regex into One

  11. 11

    Combine multiple replacingOccurrences() with Swift

  12. 12

    Combine multiple JPEGs into one

  13. 13

    Regex for "or" of multiple words in grep

  14. 14

    chosen search for multiple words

  15. 15

    How to combine three consecutive Modbus registers to get an integer value?

  16. 16

    How can I combine consecutive keys of an array depending on their values?

  17. 17

    Combine multiple queries into one query

  18. 18

    Redux - how to combine multiple mapDispatchToProps?

  19. 19

    How to combine the dictionaries in python

  20. 20

    combine two strings in python

  21. 21

    Removing nonsense words in python

  22. 22

    How to count consecutive substring in a string in python 3

  23. 23

    Detecting Consecutive Integers in a list [Python 3]

  24. 24

    How to write to consecutive CSV columns in Python

  25. 25

    Comparing consecutive Numbers in .txt file in Python

  26. 26

    JAVA: How to combine message boxes with multiple outputs

  27. 27

    Combine Multiple Rows And Columns Into A Single Row In SQL

  28. 28

    Combine multiple SQL queries with having limit

  29. 29

    Read multiple xml files in R and combine the data

ホットタグ

アーカイブ