[英]how to seperate list of words with '->' in python 3
How do you seperate list of words with '->' or any other char?你如何用“->”或任何其他字符分隔单词列表? I tried the .join()
method but it doesn't make any change.我尝试了.join()
方法,但它没有做任何改变。 It seperated the letters just like split with comma.它像用逗号分隔一样分隔字母。
new_list = ['a' ,'b' ,'c','d']
'->'.join(new_list)
['a', 'b', 'c', 'd']
Without more information information join is the opposite of split.如果没有更多的信息信息,join 是 split 的反面。 Note that both operation returns a result and do not modify the variable in place.请注意,这两个操作都会返回结果,并且不会就地修改变量。 So you will need either to affect the result in a variable or use it directly.因此,您将需要影响变量中的结果或直接使用它。
>>> '->'.join(['foo', 'bar', '42'])
'foo->bar->42'
Please provide the value of new_list and how you test it does not work ?请提供 new_list 的值以及您如何测试它不起作用?
It depends on what do you mean with "list of words".这取决于您对“单词列表”的含义。
If, for example, you have a string l = "this, is, a, list, of, words"
, you can do:例如,如果您有一个字符串l = "this, is, a, list, of, words"
,您可以执行以下操作:
result = l.replace(', ','->')
which results in this->is->a->list->of->words
.这导致this->is->a->list->of->words
。
If you need something like: l = ["aba","bac","co"]
to "a->b->c"
, then:如果你需要这样的东西: l = ["aba","bac","co"]
到"a->b->c"
,那么:
result = '->'.join(l)
that gives aba->bac->co
.这给了aba->bac->co
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.