How to merge names with common elements in list?

bear16

In a list called 'common', I have a bunch of names that have common elements. For instance, my list has these names in it

common = ['Jeremy Paul', 'Paul Stevens', 'John Jordan', 'Jordan Smith', 
'Jordan Walker', 'Walker Marshall']

I want to be able to merge all the names that have common elements in the most Pythonic way (don't want to have to use libraries or list comprehension unless necessary). The common elements are the ones that are first names in one string and last names in the following string. As an example, this is what I want the result to be:

result = ['Jeremy Paul Stevens', 'John Jordan Smith', 'John Jordan Walker', 
'Jordan Walker Marshall']

I only want to have three words in each string, so no John Jordan Walker Marshall or anything with more than three names.

Is there some way I can do this in Python? I'm assuming I need to use some loop or form of iteration to go through each item in common, or maybe I have to use regular expressions to achieve the desired result. I'd appreciate any pointers in the right direction, thanks!

UltraInstinct

An approach: Split each string up into constituent parts, pair up parts of every item, and check if the first pair ends with the first part of the second pair -- if so insert into a list.

Something like:

>>> common = ['Jeremy Paul', 'Paul Stevens', 'John Jordan', 'Jordan Smith', 
... 'Jordan Walker', 'Walker Marshall']
>>> parts = [x.split() for x in common]
>>> [" ".join([x[0], x[1], y[1]]) for x in parts for y in parts if x[1] == y[0]]
['Jeremy Paul Stevens', 'John Jordan Smith', 'John Jordan Walker', 'Jordan Walker Marshall']

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Combine/merge lists by elements names (list in list)

From Dev

Python Merge List With Common Elements in the List

From Dev

Combine/merge lists by elements names (list in list in list)

From Dev

How to merge all elements in a list in R considering different names for merged variables

From Dev

Using Pandas to merge 2 list of dicts with common elements

From Dev

R: How to merge logical elements in a list

From Dev

How to merge two elements in a list in Python

From Dev

How to merge elements within the same list?

From Dev

How to find common elements in several List(of)?

From Dev

How to find common elements in several List(of)?

From Dev

Spark merge sets of common elements

From Dev

How to remove elements of list of array/structs that have 2 common elements

From Dev

Merge elements in list by property

From Dev

Merge elements in list by property

From Dev

How can list elements be used as factor names in R?

From Dev

How to merge overlapping integer vector elements of a list in R

From Dev

How to merge elements of dataframes of different sizes containded in a list [R]?

From Dev

How to merge two lists? Preserving identical list elements for set manipulation

From Dev

How to merge overlapping integer vector elements of a list in R

From Dev

How to get list of common elements of 2 array in Swift?

From Dev

How to create a dataframe from a nested list of multiple common elements

From Dev

Using do() with names of list elements

From Dev

Merge pairs that share common elements overall, but not directly

From Dev

Efficient distributed algorithm to merge sets with common elements

From Dev

Merge Sets of Sets that contain common elements in Scala

From Dev

Merge Sort 2 Lists Finding Common Elements

From Dev

Efficient distributed algorithm to merge sets with common elements

From Dev

Group and Merge elements in same list

From Dev

how to merge duplicate elements?

Related Related

  1. 1

    Combine/merge lists by elements names (list in list)

  2. 2

    Python Merge List With Common Elements in the List

  3. 3

    Combine/merge lists by elements names (list in list in list)

  4. 4

    How to merge all elements in a list in R considering different names for merged variables

  5. 5

    Using Pandas to merge 2 list of dicts with common elements

  6. 6

    R: How to merge logical elements in a list

  7. 7

    How to merge two elements in a list in Python

  8. 8

    How to merge elements within the same list?

  9. 9

    How to find common elements in several List(of)?

  10. 10

    How to find common elements in several List(of)?

  11. 11

    Spark merge sets of common elements

  12. 12

    How to remove elements of list of array/structs that have 2 common elements

  13. 13

    Merge elements in list by property

  14. 14

    Merge elements in list by property

  15. 15

    How can list elements be used as factor names in R?

  16. 16

    How to merge overlapping integer vector elements of a list in R

  17. 17

    How to merge elements of dataframes of different sizes containded in a list [R]?

  18. 18

    How to merge two lists? Preserving identical list elements for set manipulation

  19. 19

    How to merge overlapping integer vector elements of a list in R

  20. 20

    How to get list of common elements of 2 array in Swift?

  21. 21

    How to create a dataframe from a nested list of multiple common elements

  22. 22

    Using do() with names of list elements

  23. 23

    Merge pairs that share common elements overall, but not directly

  24. 24

    Efficient distributed algorithm to merge sets with common elements

  25. 25

    Merge Sets of Sets that contain common elements in Scala

  26. 26

    Merge Sort 2 Lists Finding Common Elements

  27. 27

    Efficient distributed algorithm to merge sets with common elements

  28. 28

    Group and Merge elements in same list

  29. 29

    how to merge duplicate elements?

HotTag

Archive