[英]python pattern cutting of strings in a list
I have a dictionary variable "d" with key ,an integer, and value as a list of strings. 我有一个字典变量“ d”,其键,整数和值作为字符串列表。
368501900 ['GH131.hmm ', 'CBM1.hmm ']
368499531 ['AA8.hmm ']
368500556 ['AA7.hmm ']
368500559 ['GT2.hmm ']
368507728 ['GH16.hmm ']
368496466 ['AA2.hmm ']
368504803 ['GT21.hmm ']
368503093 ['GT1.hmm ', 'GT4.hmm ']
The code is like this: 代码是这样的:
d = dict()
for key in d:
dictValue = d[key]
dictMerged = list(sorted(set(dictValue), key=dictValue.index))
print (key, dictMerged)
However, I want to remove string after the numbers in the lists so I can have a result like this: 但是,我想删除列表中数字之后的字符串,以便获得如下结果:
368501900 ['GH', 'CBM']
368499531 ['AA']
368500556 ['AA']
368500559 ['GT']
368507728 ['GH']
368496466 ['AA']
368504803 ['GT']
368503093 ['GT']
I think the code should be inserted between dictValue and dictMerged, but I cannot make a logic. 我认为应该将代码插入dictValue和dictMerged之间,但是我无法做出逻辑。 Please, any ideas? 拜托,有什么想法吗?
import this at the beginning 在开始时导入
import re
now use this line between dictValue and dictMerged 现在在dictValue和dictMerged之间使用此行
new_dict_value = [re.sub(r'\d.*', '', x) for x in dictValue]
and then use new_dict_value in the next line 然后在下一行中使用new_dict_value
String objects have a nice .isdigit()
method. 字符串对象有一个不错的.isdigit()
方法。 Here are some non- re
solutions for cleaning your data. 下面是一些非re
清洗您的数据解决方案。
Plain old loop: 普通的旧循环:
values = ['GT1.hmm ', 'GT4.hmm ']
clean_values = []
for item in values:
clean_item = []
for c in item:
if c.isdigit():
break
clean_item.append(c)
clean_values.append("".join(clean_item))
list comprehension using a StopIteration
exception to act as a break
inside of a generator expression: ( Note using this stop()
method in a list comprehension doesn't work, it requires a generator expression, normally denoted by ()
, but inside of a .join()
these are optional. 使用StopIteration
异常作为生成器表达式内部的break
的列表理解:( 请注意 ,在列表理解中使用此stop()
方法不起作用,它需要生成器表达式,通常用()
表示,但在.join()
这些是可选的。
def stop():
raise StopIteration
values = ['GT1.hmm ', 'GT4.hmm ']
clean_values = ["".join(c if not c.isdigit() else stop() for c in item) for item in values]
list comprehension using itertools.takewhile
: 使用itertools.takewhile
列表理解:
from itertools import takewhile
values = ['GT1.hmm ', 'GT4.hmm ']
clean_values = ["".join(takewhile(lambda c: not c.isdigit(),item)) for item in values]
Examples derived from: 示例源自:
http://tech.pro/tutorial/1554/four-tricks-for-comprehensions-in-python#breaking_the_loop http://tech.pro/tutorial/1554/four-tricks-for-comprehensions-in-python#breaking_the_loop
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.