[英]Join first elements of a list of strings in python3
Consider the following string that represents a line from a tab delimited txt file: 考虑以下字符串,它代表制表符分隔的txt文件中的一行:
line = 'pf\t2\t0\t9\t0\t9\t9\n'
I would like to join the first two elements from this string using an underscore and then write the line back to file. 我想使用下划线将字符串中的前两个元素连接起来,然后将行写回到文件中。 I am using the following simple script to do it:
我正在使用以下简单脚本来做到这一点:
newLabel = '_'.join(line.split('\t')[:2])
newLine = line.split('\t')
newLine[:2] = newLabel
What I would expect is the following: 我期望的是以下内容:
['pf_2', '0', '9', '0', '9', '9\n']
Instead I am getting: 相反,我得到:
['p', 'f', '_', '2', '0', '9', '0', '9', '9\n']
Maybe I am missing something obvious here but why does python split the joined string again? 也许我在这里遗漏了一些明显的东西,但是为什么python再次拆分连接的字符串? What is the best way to achieve what I want?
实现我想要的最好的方法是什么? Thanks!
谢谢!
You were probably looking for a slightly different assignment statement: 您可能正在寻找稍微不同的赋值语句:
newLine[:2] = [newLabel]
Slice assignment simply expects an iterable on the right hand side. 切片分配只希望右侧有一个可迭代的对象。 Since
newLabel
, a string, was an iterable, the slice assignment happily goes and iterates it, adding those elements in place of newLine[:2]
. 由于字符串
newLabel
是可迭代的,因此切片分配会愉快地进行迭代,并添加这些元素代替newLine[:2]
。
You might also consider this shortcut: 您可能还会考虑此快捷方式:
>>> line.replace('\t', '_', 1)
'pf_2\t0\t9\t0\t9\t9\n'
Using the third argument to str.replace
specifies the number of occurences to replace. 在
str.replace
使用第三个参数指定要替换的出现次数。
First compute the tokens in toks
, then rebuild a list using join
for 2 first items, and the rest of the list for the rest: 首先计算
toks
的令牌,然后使用join
对前两个项进行重建,然后对其余项进行重建:
line = 'pf\t2\t0\t9\t0\t9\t9\n'
toks = line.split('\t')
newLine = ["_".join(toks[:2])]+toks[2:]
print(newLine)
result: 结果:
['pf_2', '0', '9', '0', '9', '9\n']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.