问题:Python等同于PHP的内爆吗?

有没有等同于PHP在Python中内爆的工具?我已经阅读并拆分了一组分隔的单词,现在我想以随机顺序对其进行排序,并在单词之间用空格将其打印出来。

implode —用字符串连接数组元素

http://php.net/manual/zh/function.implode.php

Is there an equivalent for PHP’s implode in Python? I’ve read in and split up a set of delimited words, and now I want to sort them out in random orders and print the words out with spaces in between.

implode — Join array elements with a string

http://php.net/manual/en/function.implode.php


回答 0

使用字符串join-method

print ' '.join(['word1', 'word2', 'word3'])

您可以连接任何可迭代的对象(不仅是list此处使用的对象),并且当然可以使用任何字符串(不仅是' ')作为定界符。

如果您想要一个随机的命令(如您在问题中说的那样),请使用shuffle

Use the strings join-method.

print ' '.join(['word1', 'word2', 'word3'])

You can join any iterable (not only the list used here) and of course you can use any string (not only ' ') as the delimiter.

If you want a random order like you said in your question use shuffle.


回答 1

好的,我刚刚找到了一个可以完成我想做的事情的函数。

我读了一个文件,其中的单词格式如下: Jack/Jill/my/kill/name/bucket

然后,我使用split()方法将其拆分,一旦将单词包含在列表中,便使用此方法将单词连接起来:

concatenatedString = ' - '.join(myWordList)
# ie: delimeter.join(list)

Okay I’ve just found a function that does what I wanted to do;

I read in a file with words in a format like: Jack/Jill/my/kill/name/bucket

I then split it up using the split() method and once I had the word into an list, I concatenated the words with this method:

concatenatedString = ' - '.join(myWordList)
# ie: delimeter.join(list)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。