concatenate two strings that are in different lists

耗尽温柔 提交于 2019-12-11 06:46:59

问题


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.


回答1:


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.




回答2:


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'}



回答3:


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

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



回答4:


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)



回答5:


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 (e.g., 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']


来源:https://stackoverflow.com/questions/55163092/concatenate-two-strings-that-are-in-different-lists

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!