Is There a Short Contains Function for Lists

Is there a short contains function for lists?

Use:

if my_item in some_list:
...

Also, inverse operation:

if my_item not in some_list:
...

It works fine for lists, tuples, sets and dicts (check keys).

Note that this is an O(n) operation in lists and tuples, but an O(1) operation in sets and dicts.

How to test if a list contains another list as a contiguous subsequence?

Here is my version:

def contains(small, big):
for i in xrange(len(big)-len(small)+1):
for j in xrange(len(small)):
if big[i+j] != small[j]:
break
else:
return i, i+len(small)
return False

It returns a tuple of (start, end+1) since I think that is more pythonic, as Andrew Jaffe points out in his comment. It does not slice any sublists so should be reasonably efficient.

One point of interest for newbies is that it uses the else clause on the for statement - this is not something I use very often but can be invaluable in situations like this.

This is identical to finding substrings in a string, so for large lists it may be more efficient to implement something like the Boyer-Moore algorithm.

Note: If you are using Python3, change xrange to range.

Is a hashset needed for contains with List(of String) Vb.net

Is a hashset needed for contains?

Needed? No.

Would [List<T>.Contains] be any slower than [HashSet<T>.Contains]?

Probably. It depends on how List<T>.Contains is implemented (its probably a linear search).

I'll answer a question you didn't ask.

Does it matter?

It depends. You have to code up both, profile it, and see if it's a bottleneck in your application. If it's not, just stick with List<T>.Contains.

Print lists that contains second element only

You're getting this error because you try to access the second element of an array that contains only 1 string. In this case you want to check the length of the array

for line in open("testing.txt"):
strip = line.rstrip()
words = strip.split(';')
for test in words:
if len(words) > 1:
print(words)
else: # this else is not necessary
continue

Edit: If you want to print each sentences containing at least one ';' only once, you don't actually have to use a for loop. One concise way to get the desired output would be this:

for line in open("testing.txt"):
strip = line.rstrip()
words = strip.split(';')
if len(words) > 1:
print(words)

Difficulty creating a dict that contains lists of values

When one comes from a C++ or Java background it's a natural tendency to write a lot of explicit loops over collections. That's not the Python way. It's often possible to streamline these loops with strategic use of list comprehensions and generator expressions. Your code will often end up shorter and easier to follow, a nice win/win.

Let's try that approach and build our way up to a loop-less version. In your program you're trying to build a dict. Let's see if we can return one directly. We'll fill out the pieces a step at a time. First, a dict:

return {
???
}

A dict with two key/value pairs:

return {
"X Values": ???,
"Y Values": ???,
}

Now what? What goes in the placeholders? Well, the first one is the list of X values. You already have that: it's x_values. No need to iterate over each x in x_values. Let's just put it directly in the dict.

return {
"X Values": x_values,
"Y Values": ???,
}

What do we do for the Y values? Here's where list comprehensions come in. You want to apply the formula y = m*x+b to each x value. We can do that all in one line, both applying the formula and building the list at the same time:

return {
"X Values": x_values,
"Y Values": [m*x+b for x in x_values],
}

And we're done. Ain't Python grand?

How to check if one of the following items is in a list?

>>> L1 = [2,3,4]
>>> L2 = [1,2]
>>> [i for i in L1 if i in L2]
[2]

>>> S1 = set(L1)
>>> S2 = set(L2)
>>> S1.intersection(S2)
set([2])

Both empty lists and empty sets are False, so you can use the value directly as a truth value.



Related Topics



Leave a reply



Submit