字符串列表中的笛卡尔积 [英] Cartesian product from list of Strings

查看:94
本文介绍了字符串列表中的笛卡尔积的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要回答此问题,您需要编写将产生的代码包含可以进行的所有组合的字符串列表从两个输入列表中选取第一个列表中的一个元素,然后紧随其后的是第二个列表的元素(之间有一个空格).

To answer this question you need to write code that will produce a list of strings that contains all combinations that can be made from two input lists by taking an element from the first list and following it by an element of the second list (with a space in between).

应确定新列表中元素的顺序首先是第一个列表中的顺序,其次是(对于具有相同的第一部分的元素)在第二个列表中.因此,如果第一个输入的格式为:A,B,.. X第二个形式为:1、2,.. n输出将为以下形式:
["A 1","A 2",.."An","B 1","B 2",.."Bn",.."X 1","X 2",.."Xn"]

The ordering of elements in the new list should be determined primarily the the ordering in the first list and secondarily (for elements with the same same first part) by the ordering of the second list. So, if the first input is of the form: A, B, .. X and the second is of the form: 1, 2, .. n the output would be of the form:
["A 1", "A 2", .. "An", "B 1", "B 2", .. "Bn", .. "X 1", "X 2", .. "Xn"]

(其中".."表示之间可以有更多元素).

(where the ".." indicates there can be more elements in between).

注意:1.两个列表中可以有不同数量的元素.2.如果第一个列表包含M个元素,第二个列表包含N个元素,那么输出列表应该包含M * N个元素.

NOTES: 1. There can be different numbers of elements in the two lists. 2. If the first list has M elements and the second list has N elements, then the output list should have M*N elements.

")

print("INPUT:")
listA=[] #Creating a list
listB=[]#Creating a second list
listC=[]


while True:
        print("add element y to continue")
        if input()!='y':
            break
        else:
            print("keep adding or x to break" )
            while True:
                if input=='x':
                    break
                else:
                    input_listA = input( "Enter first comma separated list of strings: " )

                    print(n)
                    listA.append(input_listA)
                    print(input_listA)
                    print(listA)
                if input=='x':
                    break
                else:
                    input_listB=input( "Enter second comma separated list of int: " )
                    input_listB.split()
                    listB.append(input_listB)
                    break

但是,当我在笛卡尔乘积的计算中使用正确的单词时,例如[黑火腿",两次鼠标",浮动奶酪",蓝精灵",电精灵",浮动精灵"],是计算字符而不是诸如"b","l","a","c","k"之类的单词因为如您所见,我在字符串中输入了定界符

however when i right words it for instance ["black ham ", "twice mouse", "floating cheese", "blue elf", "electric elf", "floating elf"] in the calculation for the cartesian product will be calculatin characters not words like 'b', 'l','a','c','k' how can i make the strings words inside the list and also how can i print it in the form of i mean without commas because as you can see i input delimiter along the strings

推荐答案

尝试一下:

import itertools

a = input("Enter first comma separated list of strings: ")
b = input("Enter second comma separated list of strings: ")
result = []
for i in itertools.product(a.split(","), b.split(",")):
  result.append("".join(map(str, i)))

print(result)

结果:

~ $ python3 test.py
Enter first comma separated list of strings: aa,bb
Enter second comma separated list of strings: 1,2,3
['aa1', 'aa2', 'aa3', 'bb1', 'bb2', 'bb3']


如果您想在每对两个单词之间留一个空格,请更改


If you'd rather like a space between the two words in each pair, change

result.append("".join(map(str, i)))

result.append(" ".join(map(str, i)))

输出:

['aa 1', 'aa 2', 'aa 3', 'bb 1', 'bb 2', 'bb 3']

这篇关于字符串列表中的笛卡尔积的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆