prisethesun13 - 1 year ago 71
Python Question

# Combining boolean operators

We have a 4 list:

``````lst1 = ['asd', '123', 'uniq','all']
lst2 = ['asd', '123', 'all']
lst3 = ['asd', 'al']
lst4 = ['all']
result = {}
for i in lst1:
if statement1:
result[i] = 0.25
elif statement2:
result[i] = 0.50
elif statement3:
result[i] = 0.75
else:
``````

• statement1 - word from lst1 only in lst1(1/4-0.25)

• statement2- word from lst1 only in lst1 and lst2 or lst or
lst4(2/4-0.50)

• statement3- word from lst1 only in 3 of those lists(2,3/2,4/3,4)
3/4-0.75

So how can I combine if boolean operators in python to get the result above?

Update:

So we take word from lst1 - 'asd' and we see that this word only in 3 of 4 lists, so the spread is 3/4

Than we take '123', and we see that this word only in 2 of 4, so 2/4

Then we take 'uniq', this words only in list1, so 1/4

I need something like this "Hindu" code:

``````for i in lst1:
if i in (lst2 and lst3 and i not in lst4) or i in (lst3 and lst 4 and not in lst2) or i in (lst2 and lst4 and not in lst3)
``````

I need to check spreading of the word. We have only 4 lists. If the word only in 3 lists of 4 - it spread 3/4, if in 2 of 4 - 2/4, if it uniqs(only in one lists) 1/4
Sorry for my "stupid" english.

A basic solution,

``````s1 = set(['asd', '123', 'uniq','all'])
s2 = set(['asd', '123', 'all'])
s3 = set(['asd', 'all'])
s4 = set(['all'])

result = {}

l = [s1, s2, s3, s4]
nrof_lists = len(l)

for word in s1:
times = sum([word in s for s in l])

if times:
result[word] = times*1.0/nrof_lists
else:

# Output
print(result)
{'123': 0.5, 'all': 1.0, 'uniq': 0.25, 'asd': 0.75}
[]
``````

Now, I understand what you want. So, it is better to use `collections.Counter`,

``````import collections
import itertools

l = [s1, s2, s3, s4]
nrof_lists = len(l)

result = {k : v*1.0/nrof_lists for k, v in collections.Counter(itertools.chain.from_iterable(l)).items()}

print(result)
{'uniq': 0.25, 'all': 1.0, '123': 0.5, 'asd': 0.75}
``````
Source (Stackoverflow)