如何加入整数数组? [英] how to join array of integers?
问题描述
我认为在Ruby中,如果你有一个整数数组(或列表)
foo = [1,2,3]
你可以使用foo.join(",")将它们连接成一个字符串1,2,3
in Python ...是方法使用,,。join()?但是那时候一定要拿
a字符串列表......不是整数...
任何快速方法?
2007年9月15日星期六12:36:02 +0000,Summercool写道:
i think in Ruby,如果你有一个整数数组(或列表)
foo = [1,2,3]
你可以使用foo .join(",")将它们连接成一个字符串1,2,3
in Python ...是使用,的方法。 .join()?但是它必须把
一个字符串列表...不是整数......
任何快速方法?
加入前将它们转换为字符串:
在[145]中:foo = [1,2,3]
在[146]:'',''。join(map(str,foo))
Out [146]:''1,2, 3''
Ciao,
Marc''BlackJack''Rintsch
9月15日,10:36 pm,Summercool< Summercooln ... @ gmail.comwrote:
i在Ruby中思考,如果你有一个整数的数组(或列表)
foo = [1,2,3]
您可以使用foo.join(",")将它们连接成一个字符串1,2,3
Python中的
...是使用,。join()的方法吗?但是它必须把
一个字符串列表...不是整数......
任何快速方法?
>> foo = [1,2,3]
"," .join(str(x)for f in foo)
''1,2, 3''
>>"," .join(map(str) ,foo))
''1,2,3''
< blockquote class =post_quotes>
>>>
如果要将几个这样的结果写入文件,请考虑使用csv模块
。
HTH,
John
print''''。join ([str(i)for i in [1,2,3]])
2007年9月15日,Summercool< Su ********* ***@gmail.com写道:
i想在Ruby中,如果你有一个整数数组(或列表)
foo = [1,2,3]
您可以使用foo.join(",")将它们连接成字符串1,2,3< Python中的
...是使用,。join()的方法吗?但是接下来必须要
a字符串列表...不是整数...
任何快速方法?
-
http://mail.python。 org / mailman / listinfo / python-list
i think in Ruby, if you have an array (or list) of integers
foo = [1, 2, 3]
you can use foo.join(",") to join them into a string "1,2,3"
in Python... is the method to use ",".join() ? but then it must take
a list of strings... not integers...
any fast method?
On Sat, 15 Sep 2007 12:36:02 +0000, Summercool wrote:
i think in Ruby, if you have an array (or list) of integers
foo = [1, 2, 3]
you can use foo.join(",") to join them into a string "1,2,3"
in Python... is the method to use ",".join() ? but then it must take
a list of strings... not integers...
any fast method?Convert them to strings before joining:
In [145]: foo = [1, 2, 3]
In [146]: '',''.join(map(str, foo))
Out[146]: ''1,2,3''
Ciao,
Marc ''BlackJack'' Rintsch
On Sep 15, 10:36 pm, Summercool <Summercooln...@gmail.comwrote:i think in Ruby, if you have an array (or list) of integers
foo = [1, 2, 3]
you can use foo.join(",") to join them into a string "1,2,3"
in Python... is the method to use ",".join() ? but then it must take
a list of strings... not integers...
any fast method?
>>foo = [1,2,3]
",".join(str(x) for x in foo)
''1,2,3''
>>",".join(map(str, foo))
''1,2,3''
>>>
If you are going to write several such results to a file, consider
using the csv module.
HTH,
John
print ''''.join([str(i) for i in [1,2,3]])
On 9/15/07, Summercool <Su************@gmail.comwrote:i think in Ruby, if you have an array (or list) of integers
foo = [1, 2, 3]
you can use foo.join(",") to join them into a string "1,2,3"
in Python... is the method to use ",".join() ? but then it must take
a list of strings... not integers...
any fast method?
--
http://mail.python.org/mailman/listinfo/python-list
这篇关于如何加入整数数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!