Pythonic way to find 2 items from two lists existing in a another list

user1452494

I have some twitter data and I split the text into those with happy emoticons and sad emoticons elegantly and pythonically like so:

happy_set = [":)",":-)","=)",":D",":-D","=D"]
sad_set = [":(",":-(","=("]

happy = [tweet.split() for tweet in data for face in happy_set if face in tweet]
sad = [tweet.split() for tweet in data for face in sad_set if face in tweet]

This works, however, it could be the case that both an emoticon from the happy_set and sad_set could be found in a single tweet. What is the pythonic way to ensure that the happy list only contains emoticons from the happy_set and vice versa?

Alex Riley

You could try using sets, specifically set.isdisjoint. Check to see if the set of tokens in a happy tweet is disjoint from sad_set. If so, it definitely belongs in happy:

happy_set = set([":)",":-)","=)",":D",":-D","=D"])
sad_set = set([":(",":-(","=("])

# happy is your existing set of potentially happy tweets. To remove any tweets with sad tokens...
happy = [tweet for tweet in happy if sad_set.isdisjoint(set(tweet.split()))]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Pythonic way to find 2 items from two lists existing in a another list

From Dev

find lists that start with items from another list

From Dev

Pythonic way of creating list of lists from list

From Dev

pythonic way of removing similar items from list

From Dev

Copying a list of lists into part of another one in the most pythonic way

From Dev

Pythonic way of sum of dot product of two list of lists

From Dev

What is the most Pythonic way to find all Nones in a list of lists?

From Dev

Pythonic way to find elementa of a python list that are not contained in another python list

From Dev

Pythonic way to group items in a list

From Dev

Pythonic way to group items in a list

From Dev

most pythonic way to use a value from a list as an index to another list

From Dev

Pythonic way of filtering out a list of strings from another list of strings

From Dev

most pythonic way to use a value from a list as an index to another list

From Dev

A Pythonic way to insert elements of a list into a list of lists?

From Dev

How can i select random items from a set of two lists and then remove that set of items from another list built out of all the possibilities

From Dev

"Best" pythonic way to retrieve an item from a list based on two criteria

From Dev

Pythonic way to find datetime with smallest time difference to target from list?

From Dev

Pythonic Way to compare two unordered lists by attributes

From Dev

Pythonic Way to Compare Values in Two Lists of Dictionaries

From Dev

Pythonic Way to compare two unordered lists by attributes

From Dev

Pythonic way to find and replace with a dictionary of lists

From Dev

Pythonic way to find and replace with a dictionary of lists

From Dev

How to find and truncate a list to the nearest power of 2 (in the most pythonic way)

From Dev

Count items existing in 2 Lists

From Dev

Pythonic way to find all the different intersections and differences between two lists of arrays

From Dev

Pythonic way of getting all consecutive 2-tuples from list

From Dev

Python List: Pythonic way to get unique items in given List from a given Set

From Dev

The fastest way to find 2 numbers from two lists that in sum equal to x

From Dev

More pythonic way to remove duplcate key/values from dict inside two diferent lists

Related Related

  1. 1

    Pythonic way to find 2 items from two lists existing in a another list

  2. 2

    find lists that start with items from another list

  3. 3

    Pythonic way of creating list of lists from list

  4. 4

    pythonic way of removing similar items from list

  5. 5

    Copying a list of lists into part of another one in the most pythonic way

  6. 6

    Pythonic way of sum of dot product of two list of lists

  7. 7

    What is the most Pythonic way to find all Nones in a list of lists?

  8. 8

    Pythonic way to find elementa of a python list that are not contained in another python list

  9. 9

    Pythonic way to group items in a list

  10. 10

    Pythonic way to group items in a list

  11. 11

    most pythonic way to use a value from a list as an index to another list

  12. 12

    Pythonic way of filtering out a list of strings from another list of strings

  13. 13

    most pythonic way to use a value from a list as an index to another list

  14. 14

    A Pythonic way to insert elements of a list into a list of lists?

  15. 15

    How can i select random items from a set of two lists and then remove that set of items from another list built out of all the possibilities

  16. 16

    "Best" pythonic way to retrieve an item from a list based on two criteria

  17. 17

    Pythonic way to find datetime with smallest time difference to target from list?

  18. 18

    Pythonic Way to compare two unordered lists by attributes

  19. 19

    Pythonic Way to Compare Values in Two Lists of Dictionaries

  20. 20

    Pythonic Way to compare two unordered lists by attributes

  21. 21

    Pythonic way to find and replace with a dictionary of lists

  22. 22

    Pythonic way to find and replace with a dictionary of lists

  23. 23

    How to find and truncate a list to the nearest power of 2 (in the most pythonic way)

  24. 24

    Count items existing in 2 Lists

  25. 25

    Pythonic way to find all the different intersections and differences between two lists of arrays

  26. 26

    Pythonic way of getting all consecutive 2-tuples from list

  27. 27

    Python List: Pythonic way to get unique items in given List from a given Set

  28. 28

    The fastest way to find 2 numbers from two lists that in sum equal to x

  29. 29

    More pythonic way to remove duplcate key/values from dict inside two diferent lists

HotTag

Archive