简体   繁体   中英

concatenate two strings that are in different lists

I need to concatenate two strings that are in different lists and check if the output string is in a dictionary. The code I've tried is:

x=['casa','lo','pre','computer']
y=['music','sun','ve','sident','house']
dic=['sunday','love','president','house','computer']
text=[]
errors=[]
iter_y=iter(y)
iter_x=iter(x)

for i in iter_x:
    if i in dic:
        text.append(i)
    else:
        try:
            concatenated= i + next(iter_y)
            if concatenated in dic:
                text.append(concatenated)
      except StopIteration:
          continue
        else:
            errors.append(i)
   print (text)

This code is returning only the word that is common to x and y ('Computer'). My desired output is x=[love, president, computer] That is, with the words love and president concatenated in the output.

IIUC then you can use itertools.product to get product of two different lists and then perform set intersection to find the common words

from itertools import product
x=['casa','lo','pre','computer']
y=['music','sun','ve','sident','house']
dic=['sunday','love','president','house','computer']
set(list(map(''.join, list(product(x, y)))) + x + y) & set(dic)

OUTPUT:

{'computer', 'house', 'love', 'president'}

If the expected output should not include house from second list then do not append list y in the final concatenated list

set(list(map(''.join, list(product(x, y)))) + x) & set(dic)

OUTPUT

{'computer', 'love', 'president'}

With your approach, you need to reset the iterator over y each time you try a new value in x .

It might be clearer like this:

for i in x:
  if i in dic:
    text.append(i)
  else:
    for j in y:
      concatenated = i + j
      if concatenated in dic:
        text.append(concatenated)

The for j in y tries all the things in y , otherwise it moves on each time, and never looks back.

For a one liner use filter , ''.join and add [''] to the second list (so you don't have to do two if s):

list(filter(lambda i: i in dic, [''.join((s1, s2)) for s1 in x for s2 in (y + [''])]))
>>['love', 'president', 'computer']

Does this work for you?

x=['casa','lo','pre','computer']
y=['music','sun','ve','sident','house']
dic=['sunday','love','president','house','computer']

possibles = []
possibles += x
possibles += y # if you want the house...

hits = []
for a in x:
    for b in y:
        possibles.append(a+b)
for a in y:
    for b in x:
        possibles.append(a+b)

for p in possibles:
    if p in dic:
        hits.append(p)

print(p)

Here's a straightforward version with nothing fancy. Others have suggested options that are likely more efficient; however, I think this solution captures your desired solution better than some (eg, by checking all variations of concatenations).

You will want to use sets if you're doing a lot of lookups.

x = ['casa','lo','pre','computer']
y = ['music','sun','ve','sident','house']
dic = set(['sunday','love','president','house','computer'])
in_dic = set()
for str in y:
    if str in dic:
        in_dic.add(str)
for str1 in x:
    if str1 in dic:
        in_dic.add(str1)
    for str2 in y:
        str3 = str1 + str2
        str4 = str2 + str1
        if str3 in dic:
            in_dic.add(str3)
        if str4 in dic:
            in_dic.add(str4)

print(list(in_dic))
['president', 'love', 'computer', 'house']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM