我希望我的Python函数分割一个句子(输入)并将每个单词存储在一个列表中.我当前的代码拆分了句子,但没有将单词存储为列表.我怎么做?
def split_line(text):
# split the text
words = text.split()
# for each word in the line:
for word in words:
# print the word
print(words)
Run Code Online (Sandbox Code Playgroud) 在第一次出现分隔符时拆分字符串的最佳方法是什么?
例如:
"123mango abcd mango kiwi peach"
Run Code Online (Sandbox Code Playgroud)
分裂第一个mango得到:
"abcd mango kiwi peach"
Run Code Online (Sandbox Code Playgroud) 我试图在python中拆分此字符串: 2.7.0_bf4fda703454
我想在下划线上拆分该字符串,_以便我可以使用左侧的值.
我需要在此示例1234567中的最后一个冒号后获取值
client:user:username:type:1234567
Run Code Online (Sandbox Code Playgroud)
我不需要字符串中的任何其他内容只是最后一个id值.
嗨,我一直在阅读正则表达式,我有一些基本的res工作.我现在一直在尝试使用Re来整理这样的数据:
"144,1231693144,26959535291011309493156476344723991336010898738574164086137773096960,26959535291011309493156476344723991336010898738574164086137773096960,1.00,4295032833,1563,2747941288,1231823695,26959535291011309493156476344723991336010898738574164086137773096960,26959535291011309493156476344723991336010898738574164086137773096960,1.00,4295032833,909,4725008"
...进入一个元组,但我不能让它工作.
任何人都可以解释他们会怎么做这样的事情?
谢谢
我有一个清单:
row = ["Title", "url", 33, "title2", "keyword"]
Run Code Online (Sandbox Code Playgroud)
是否有更多pythonic方式来解压缩这些值,如:
title, url, price, title2, keyword = row[0], row[1], row[2], row[3], row[4]
Run Code Online (Sandbox Code Playgroud) 是否有一种"简单"的方法将包含数字的str转换为[x,y]整数列表?
# from: '5,4,2,4,1,0,3,0,5,1,3,3,14,32,3,5'
# to: [[5, 4], [2, 4], [1, 0], [3, 0], [5, 1], [3, 3], [14, 32], [3, 5]]
Run Code Online (Sandbox Code Playgroud)
顺便说一句,下面的工作,但不会直接称之为...此外,可以假设输入str已经过验证,以确保它只包含偶数个逗号交错的数字.
num_str = '5,4,2,4,1,0,3,0,5,1,3,3,14,32,3,5'
numpairs_lst = [] # ends up as [[5, 4], [2, 4], [1, 0], ...]
current_num_str = '' # the current num within the str; stop when a comma is found
xy_pair = [] # this is one of the [x,y] pairs -> [5, 4]
for ix,c in enumerate(num_str):
if c == ',':
xy_pair.append(int(current_num_str))
current_num_str …Run Code Online (Sandbox Code Playgroud) 我发现了一个有趣的事情,它比在分隔符之后获取整个子字符串partition更快。split我已经在 Python 3.5 和 3.6 (Cpython) 中进行了测试
In [1]: s = \'validate_field_name\'\n\nIn [2]: s.partition(\'_\')[-1]\nOut[2]: \'field_name\'\n\nIn [3]: s.split(\'_\', maxsplit=1)[-1]\nOut[3]: \'field_name\'\n\nIn [4]: %timeit s.partition(\'_\')[-1]\n220 ns \xc2\xb1 1.12 ns per loop (mean \xc2\xb1 std. dev. of 7 runs, 1000000 loops each)\n\nIn [5]: %timeit s.split(\'_\', maxsplit=1)[-1]\n745 ns \xc2\xb1 48.8 ns per loop (mean \xc2\xb1 std. dev. of 7 runs, 1000000 loops each)\n\nIn [6]: %timeit s[s.find(\'_\')+1:]\n340 ns \xc2\xb1 1.44 ns per loop (mean \xc2\xb1 std. dev. of 7 runs, 1000000 …Run Code Online (Sandbox Code Playgroud) 我有一个看起来像这样的文本文件:
“Distance 1: Distance XY” 1 2 4 5 9 “Distance 2: Distance XY” 3 6 8 10 5 “Distance 3: Distance XY” 88 45 36 12 4
Run Code Online (Sandbox Code Playgroud)
这一切都在一条大线上.我的问题是我如何采取这个并分开距离测量,以使线看起来更像这样:
“Distance 1: Distance XY” 1 2 4 5 9
“Distance 2: Distance XY” 3 6 8 10 5
“Distance 3: Distance XY” 88 45 36 12 4
Run Code Online (Sandbox Code Playgroud)
我想这样做,为每个距离测量制作一本字典.
在Python中是否可以转换用逗号分隔的原始字符串,例如
tt0099700,tt0096061,tt0118688,tt0120784
Run Code Online (Sandbox Code Playgroud)
到Python数组/列表?
这样的字符串作为变量在循环中row[i]。
谢谢你的帮助。
我有这个字符串:
var(HELLO,)|var(Hello again)| var(HOW ARE YOU?)|outV(0)|outV(1)|outV(2)|END
Run Code Online (Sandbox Code Playgroud)
我想把它分开|,但我不知道怎么做.我不希望它在白色空间分裂,只是在白色空间|.
这可能吗?