numpy 连接两个字符串的单词

bkkx9g8r  于 2023-04-21  发布在  其他
关注(0)|答案(1)|浏览(126)

我有两个字符串,需要连接字符串a的第0位置和字符串B的第0位置。
请查看输出格式。

a='Python is a programming language'

b='We have to practice'

# Need output in the format as below

Python We

is have

a to

programming practice

language
a='Python is a programming language'
b='We have to practice'
a=a.split()
b=b.split()
c=''
print(a)
print(b)
for i in a:
  for j in b:
    c=i+' '+j
  print(c)

低于输出

Python practice

is practice

a practice

programming practice

language practice
rslzwgfq

rslzwgfq1#

其中一个选项是zip_longest

from itertools import zip_longest
​
pairs = zip_longest(a.split(), b.split(), fillvalue="")
​
wrap = "\n".join([" ".join(pair) for pair in pairs]) # optional

输出:

for pair in pairs:
    print(*pair)

#or print(wrap)
​
Python We
is have
a to
programming practice
language

相关问题