This question already has answers here:
How to test the membership of multiple values in a list
(12 answers)
Closed 3 years ago.
I have a code in which there is a lineif y in x or f in x or h in x and so on. Even if it works, it seems to be very long.
I tried if (y or f or h) in x and if [y,f,h]in x but nothing worked.
Can anyone help?
Use any along with a generator expression:
y = 'y'
f = 'f'
x = 'x'
print(any(char in 'abcdex' for char in [y, f, x]))
Output:
True
Related
This question already has answers here:
python 3 print generator
(2 answers)
Closed 4 years ago.
I am trying to combine this into one line -
for x in set(l_rooms):
if l_rooms.count(x) == 1:
print(x)
I tried:
print(x for x in set(l_rooms) if l_rooms.count(x)== 1)
but that only yields a generator object and doesn't actually print
print([x for x in set(l_rooms) if l_rooms.count(x)==1])
This question already has answers here:
How are lambdas useful? [closed]
(26 answers)
Closed 6 years ago.
Help me understand Lambda functions, I checked a lot of codes but I still can't manage to understand it. Maybe with a simple example, thanks in advance.
Suppose you want to square in value in a list , foo = [1,2,3,4,5]
for i in range(len(a)):
a[i] = a[i] * a[i]
You could use lambda and write
map(lambda x: x * x, foo)
if you want only elements divisible by 3, then
filter(lambda x: x % 3 == 0, foo)
Basically it save you from writing a for loop or to put it better write it in an efficient way.
This question already has answers here:
How do I iterate through two lists in parallel?
(8 answers)
Closed 7 years ago.
Is it possible to iterate through multiple lists and return arguments from different lists within the same loop?
I.e., Instead of -
For x in trees:
Print(x)
For y in bushes:
Print(y)
Something like -
For x,y in trees,bushes:
Print(x +"\n"+ y)
You can simply use zip or itertools.izip:
for x, y in zip(trees, bushes):
print x, y
You can use zip():
a=['1','2','2']
b=['3','4','5']
for x,y in zip(a,b):
print(x,y)
output:
1 3
2 4
2 5
This question already has answers here:
Randomly extract x items from a list using python
(3 answers)
Closed 8 years ago.
Given an array x = [2,3,5,4,1,7,4,2,8] I am looking to create a second array y which is a length p and consists of a random election of the elements within x. Is there an easier way other than doing the following
x = [2,3,5,4,1,7,4,2,8]
random.shuffle(x)
p = 5
y = x[0:p]
print y
Use random.sample:
x = [2,3,5,4,1,7,4,2,8]
y = random.sample(x, p)
This question already has answers here:
What do lambda function closures capture?
(7 answers)
Closed 9 years ago.
Can anyone do sanity check?
I'm trying to make functions in for-loop.
The point I can't understand is summarized in the following code:
f_list = []
for i in range(10):
f = lambda j : i
f_list.append(f)
Then,
>>> f_list[0](0)
9 #I hope this is 0.
>>> f_list[1](0)
9 #I hope this is 1.
Why is this happen??
Edit: Almost the same problem is already discussed in Stackoverflow, here.
This is because of the closure property of python. To get what you actually need, you need to do like this
f = lambda j, i = i : i
So, the output of this program becomes like this
f_list = []
for i in range(5):
f = lambda j, i = i : i
f_list.append(f)
for i in range(5):
print f_list[i](0)
Output
0
1
2
3
4