我有一个字符串"42 0"(例如),需要获取两个整数的数组.我可以.split在空间上做吗?
我有一串数字,如:
example_string = '0, 0, 0, 11, 0, 0, 0, 0, 0, 19, 0, 9, 0, 0, 0, 0, 0, 0, 11'
Run Code Online (Sandbox Code Playgroud)
我想将其转换为列表:
example_list = [0, 0, 0, 11, 0, 0, 0, 0, 0, 19, 0, 9, 0, 0, 0, 0, 0, 0, 11]
Run Code Online (Sandbox Code Playgroud)
我尝试过类似的东西:
for i in example_string:
example_list.append(int(example_string[i]))
Run Code Online (Sandbox Code Playgroud)
但这显然不起作用,因为字符串包含空格和逗号.但是,删除它们不是一种选择,因为像"19"这样的数字会被转换为1和9.你能帮我解决这个问题吗?
以下代码中的最后一行是什么意思?
import pickle, urllib
handle = urllib.urlopen("http://www.pythonchallenge.com/pc/def/banner.p")
data = pickle.load(handle)
handle.close()
for elt in data:
print "".join([e[1] * e[0] for e in elt])
Run Code Online (Sandbox Code Playgroud)
我尝试解决这个问题:
e[1] * e[0] for e in elt我正在尝试将复制/粘贴的文本转换为csv,我可以将其拆分.问题是它里面有空白标签,我似乎无法摆脱它
复制/粘贴示例:
Amarr Hybrid Tech Decryptor 12 Decryptors - Hybrid 12 m3
Ancient Coordinates Database 23 Sleeper Components 2.30 m3
Caldari Hybrid Tech Decryptor 17 Decryptors - Hybrid 17 m3
Carbon 17 General 34 m3
Cartesian Temporal Coordinator 4 Ancient Salvage 0.04 m3
Central System Controller 2 Ancient Salvage 0.02 m3
Run Code Online (Sandbox Code Playgroud)
现在我想尝试这样的事情:
Amarr Hybrid Tech Decryptor,12,Decryptors - Hybrid,12,m3,
Ancient Coordinates Database,23,Sleeper Components,2.30,m3,
Caldari Hybrid Tech Decryptor,17,Decryptors - Hybrid,17,m3,
Carbon,17,General,34,m3,
Cartesian Temporal Coordinator,4,Ancient Salvage,0.04,m3,
Central System Controller,2,Ancient Salvage,0.02,m3,
Run Code Online (Sandbox Code Playgroud)
(将始终是每行5个分色
我一直在尝试以各种方式执行此操作, 使用逗号分割并在Python中删除空格, …
我有一个字符串.
s = '1989, 1990'
Run Code Online (Sandbox Code Playgroud)
我想使用python将其转换为list并且我希望输出为,
s = ['1989', '1990']
Run Code Online (Sandbox Code Playgroud)
有没有最快的一个班轮方式相同?
我想用map来获取字符串列表:
value = '1, 2, 3'
my_list = list(map(strip, value.split(',')))
Run Code Online (Sandbox Code Playgroud)
但得到了:
NameError: name 'strip' is not defined
Run Code Online (Sandbox Code Playgroud)
预期结果: my_list=['1','2','3']
python ×6
string ×4
integer ×2
list ×2
split ×2
arrays ×1
map-function ×1
python-3.x ×1
whitespace ×1