【问题标题】:Calculate Cartasian product Itertools.product without comma(Delimiter)计算笛卡尔积 Itertools.product 不带逗号(分隔符)
【发布时间】:2018-04-03 13:25:35
【问题描述】:

您好,我只是想知道我们如何计算笛卡尔积(intertools.product(*somelist)) 但不打印逗号?

我想打印这样的东西!

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).  

Here is a more specific case:
    For inputs:  blue, electric, floating 
           and:  cheese, eel
we would get:
  ["blue cheese", "electric cheese", "floating cheese", "blue eel",...........

HOWEVER WHEN I DO 
listA=[] #Creating a list
listB=[]#Creating a second list
listC=[]
input_listA=input( "Enter second comma separated list of int: " )
listA.append(input_listA)
input_listB = input( "Enter first comma separated list of strings: " )
listB.append(input_listB)
listC.append(listB)
listC.append(listA)

for i in itertools.product(*listA,*listB):
      print(i)

它给出这个输出:

INPUT:输入第二个逗号分隔的 int 列表:A、B、C 输入第一个逗号分隔的字符串列表: 1, 2, 3 ('A', '1') ('A', ',') ('A', ' ') ('A', '2') ('A', ',') ('A', '') ('A', '3') (',', '1') (',' , ',') (',', '') (',', '2') (',', ',') (',', '') (',', '3') ('', '1' ) (' ', ',') (' ', ' ') (' ', '2') (' ', ',') (' ', ' ') (' ', '3') ('B', '1') ('B', ',') ('B', '')

我怎样才能以“A 1”、“A 2”、..“An”的正确形式打印它们 如何删除“,”和“”

【问题讨论】:

    标签: python python-3.x delimiter itertools cartesian-product


    【解决方案1】:

    一些问题。

    您正在处理 input 上的 strings(也是可迭代的),并且在调用 itertools.product 时无需解包。

    以下修改应该可以满足您的要求:

    ...
    listA.extend(input_listA.split(','))
    
    listB.extend(input_listB.split(','))
    ...
    for i in itertools.product(listA, listB):
        print(i)
    

    但是,您要确保您正在为itertools.product 提供您想要的内容,并且不清楚您是否来自该帖子。您需要确保listAlistB 是包含您想要的内容的列表。我相信你的意图是: ['A', 'B', 'C'] ['1', '2', '3'] 分别,因此出于调试目的,在进入for 循环之前发出print 语句以确保您拥有所需的内容。

    我的输入净化结果:

    ('A', '1')
    ('A', '2')
    ('A', '3')
    ('B', '1')
    ('B', '2')
    ('B', '3')
    ('C', '1')
    ('C', '2')
    ('C', '3')
    

    打印到空格分隔列表中:

    for i in itertools.product(listA, listB):
        print('{} {}'.format(*i))
    

    结果:

    A 1
    A 2
    A 3
    B 1
    B 2
    B 3
    C 1
    C 2
    C 3
    

    【讨论】:

    • itertools.product(listA, listB) 中的 i 的内容: print('{} {}'.format(*i)) 它给出的结果与您的不同:它是逗号的结果(',') 与其他 A 1 A , A 2 A , A 3 , 1 , , , 2 , , , 3 B 1 B , B 2 B , B 3 , 1 , , , 2 , , , 3 C 1 C , C 2 C , C 3
    • @A.kaisis 您还需要进行其他修改。扩展listAlistB
    • 与他们的结果是: a a a a a a a a , a , a , a , a b a b a b a b a , a , a , a , a c a c a c a c a
    • listA=[] #创建一个列表 listB=[]#创建第二个列表 listC=[] input_listA=input( "输入第二个逗号分隔的 int 列表:" ) listA.append(input_listA) listA.extend(input_listA.split(',')) input_listB = input("输入第一个逗号分隔的字符串列表:" ) listB.append(input_listB) listB.extend(input_listB.split(',')) listC.append (listB) listC.append(listA) for i in itertools.product(*listA,listB): print ('{} {}'.format(*i))
    • 还有一个问题,当我们输入单词时,您的代码无法正常工作,它会获取每个字符串的每个字符并在笛卡尔积中进行计算,但无法用单词正确计算
    猜你喜欢
    • 1970-01-01
    • 2011-03-24
    • 2017-03-07
    • 2013-04-20
    • 2021-11-19
    • 2021-02-17
    • 2011-01-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多