[英]Preferred way to check if a line starts with a string from list?
I'm trying to to sort through a file line by line, comparing the beginning with a string from a list, like so: 我试图逐行对文件进行排序,将开头与列表中的字符串进行比较,如下所示:
for line in lines:
skip_line = True
for tag in tags:
if line.startswith(tag) is False:
continue
else:
skip_line = False
break
if skip_line is False:
#do stuff
While the code works just fine, I'm wondering if there's a neater way to check for this condition. 虽然代码可以正常工作,但我想知道是否有一种更整洁的方法来检查这种情况。 I have looked at
any()
, but it seems to just give me the possibility to check if any of my lines start with a fixed tag (not eliminating the for
loop needed to loop through my list. 我已经看过
any()
,但似乎只是让我可以检查我的任何行是否都以固定标签开头(而不是消除遍历列表的for
循环)。
So, essentially I'm asking this: 所以,本质上我是在问:
Is there a better, sleeker option than using a for loop to iterate over my tags
list to check if the current line starts with one of its elements? 是否有比使用for循环遍历我的
tags
列表以检查当前行是否以其元素之一开头更好的,更时尚的选择?
As Paradox pointed out in his answer: Using a dictionary to lookup if the string exists has O(1) complexity and actually makes the entire code look a lot cleaner, while being faster than looping through a list. 正如Paradox在他的回答中指出的那样:使用字典查找字符串是否存在具有O(1)复杂性,实际上使整个代码看起来更简洁,同时比遍历列表更快。 Like so:
像这样:
tags = {'ticker':0, 'orderBook':0, 'tradeHistory':0}
for line in lines:
if line.split('\t')[0] in tags:
#do stuff
If you're determined to pull this down into a one-liner, you can use a generator: 如果您决定将其归为一类,则可以使用生成器:
tagged_lines = (line for line in lines if any(line.startswith(tag) for tag in tags))
for line in tagged_lines:
# Do something with line here
Of course, how readable this is is a different question. 当然,这是如何可读的是另一个问题。
You've probably seen syntax like [x*x for x in range(10)]
before, but by swapping the []
for ()
, we instead generate each item only when it's asked for. 您之前可能已经看过
[x*x for x in range(10)]
语法[x*x for x in range(10)]
语法,但是通过将[]
换为()
,我们只在需要时才生成每个项目。
Instead of iterating over your tags list, you can put all your tags inside a HashMap and do a simple lookup like myMap.exists("word"). 无需遍历标签列表,您可以将所有标签放入HashMap中,并进行类似于myMap.exists(“ word”)的简单查找。 This would be much faster that iterating through your tags list and works in O(1) complexity.
这将比遍历标签列表要快得多,并且可以处理O(1)复杂性。 In python its actually a dictionary data structure.
在python中,它实际上是一个字典数据结构。 http://progzoo.net/wiki/Python:Hash_Maps
http://progzoo.net/wiki/Python:Hash_Maps
This has been asked before. 这已经被问过了。 Take a look at this post for more solutions.
看看这篇文章,了解更多解决方案。 I would flag this post as a duplicate but I still do not have the reputation.
我会将此帖子标记为重复,但我仍然没有声誉。
https://stackoverflow.com/a/10477481/5016492 https://stackoverflow.com/a/10477481/5016492
You'll need to modify the regular expression so that it looks at the start of the line. 您需要修改正则表达式,使其看起来在行的开头。 Something like this should work for you '^tag' .
这样的事情应该为您工作'^ tag'。
In fact any()
will do the job 实际上,
any()
可以胜任
Looping each line 循环每行
for line in lines:
tagged = any(lambda: line.startswith(y), tags)
Any list start with any tag 任何列表以任何标签开头
any(lambda x: any(lambda y: x.startswith(y), tags), lines)
Filter tagged lines 过滤标记的行
filter(lambda x: any(lambda y: x.startswith(y), tags), lines)
How about a combination off any() and filter() like in this example: 如何将any()和filter()结合起来,如本例所示:
# use your data here ...
mytags = ('hello', 'world')
mylines = ('hello friend', 'you are great', 'world is cruel')
result = filter(lambda line: any(map(lambda tag: line.startswith(tag), mytags)), mylines)
print result
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.