Remove all items with the same domain in Python [closed] - python

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have the following array:
array = [
'javi#indaloymedia.com',
'caroline#grupoplatinum.com'
]
Then I have the following file:
javi#indaloymedia.com
asdsd#indaloymedia.com
jasdasd#indaloymedia.com
caroline#grupoplatinum.com
asdasde#grupoplatinum.com
wata#man.com
How can I do to eliminate all the elements that are in the array with domain 'indaloymedia.com' and grupoplatinum that is to say so that the file is as follows:
wata#man.com

The easiest way would be:
blacklist = [
'javi#indaloymedia.com',
'caroline#grupoplatinum.com'
]
domains = [e.split('#')[-1] for e in blacklist]
filtered_emails = []
with open("emails.txt") as f:
for line in f:
line = line.strip()
domain = line.split('#')[-1]
if domain not in domains:
filtered_emails.append(line.strip())
print(filtered_emails)
Note that this solution won't cover every corner case, but should be enough to get you started.
https://repl.it/repls/RealDramaticField
Also, if your blacklist is huge, domains should be a set instead of a list for fast lookups.

Only using basic for loops and an if statement:
def remove_domains(addresses, blacklist):
for a in addresses:
for b in blacklist:
if b in a:
addresses.remove(a)
return a

Related

How to count frequency of multiple items in a list and print relative frequencies [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
Given two lists, I need to count the frequency of the items in one list as they are found in the other list; and place the relative frequencies of each item inside frequencyList (where the
frequency of searchFor[0] is stored in frequencyList[0])
I am unable to import anything
textList=['a','b','a','c',...]
searchFor=['a','b']
frequencyList=[2,1]
Try:
[textList.count(i) for i in searchFor]
Or?
list(map(textList.count, searchFor))
The other answer is quite compact and very pythonic but this is an alternate solution that is slightly more efficient as it only requires one pass over the input list.
textList=['a','b','a','c']
output_dict = {}
for i in textList:
try:
output_dict[i] = d[i] + 1
except:
output_dict[i] = 1
print(output_dict['a'])

how to transform a flattened json data to a taxonomy string? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have a data like this, aka input data:
data = ['a-aa-aab', 'a-aa-aaa', 'b-ba', 'a-aa-aab-aaba', 'a-aa-aab-aabb']
And I want to transform that to the taxonomy string like this, aka output data:
root a b
a a-aa
a-aa a-aa-aab a-aa-aaa
a-aa-aab a-aa-aab-aaba a-aa-aab-aabb
b b-ba
I think there is a recursive solution in this sample, but I don't know how to achive this target. If you happen to know the answer, please tell me, god bless you!
from collections import defaultdict
data = ['a-aa-aab', 'a-aa-aaa', 'b-ba', 'a-aa-aab-aaba', 'a-aa-aab-aabb']
result = defaultdict(set)
for string in data:
parts = string.split('-')
for i in range(len(parts)):
key = '-'.join(parts[:i])
val = '-'.join(parts[:i+1])
result[key].add(val)
print(result)
for prefix, children in result.items():
print(prefix or 'root', *children)

Efficient way of parsing string [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
How would you turn a string that looks like this
7.11,8,9:00,9:15,14:30,15:00
into this dictionary entry
{7.11 : [8, (9:00, 9:15), (14:30, 15:00)]}?
Suppose that the number of time pairs (such as 9:00,9:15 and 14:30,15:00 is unknown and you want to have them all as tuple pairs.
First split the string at the commas, then zip cluster starting from the 3rd element and put it into a dictionary:
s = "7.11,8,9:00,9:15,14:30,15:00"
ss = s.split(',')
d = {ss[0]: [ss[1]] + list(zip(*[iter(ss[2:])]*2))}
Output:
{'7.11': ['8', ('9:00', '9:15'), ('14:30', '15:00')]}
If you need to convert it from string to appropiate data types (you'll have to adapt it according to your needs), then after getting the ss list:
time_list = [datetime.datetime.strptime(t,'%H:%M').time() for t in ss[2:]]
d = {float(ss[0]): [int(ss[1])] + list(zip(*[iter(time_list)]*2))}

create more lists from one big list [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have the following list which was created from a text file that I already have.
In the end I have a list that contains all the values that I need from the text, and now I am trying to have a more than one small lists from the list array where every small list start with switch and end where the value is empty.
with open("read.txt") as f:
for line in f:
if line.startswith('switch '):
array.append(line)
for line in f: # Continue iterating f for additional lines to keep
if not line.rstrip():
break # We hit an empty line, return to looking for switch
array.append(line)
i = 0
while i+4 <= len(my_list):
print(my_list[i+1:i+4])
i+=4
#['v1', 'v2', 'v3']
#['m1', 'm2', 'm3']

List groupings of a given length [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I'm trying to find all possible groupings for a list so that the resulting list has a specified length, here's an example:
group([2,3,5,6,8], length=3)
would give
[[2,[3,5],[6,8]], [[2,3],5,[6,8]], [2,3,[5,6,8]], ...
what would be the best way to approach this?
Try this recursive solution, warning: if your list grows larger, this will grow exponentially! Also, this will have some duplicate in that the orders are different.
from itertools import permutations
master = []
def group(l, num, res=[]):
if num == 1:
res.append(l)
master.append(res)
return
for i in range(len(l)-num + 1):
firstgroup = list({e[:i+1] for e in permutations(l)})
for each in firstgroup:
myres = res[:]
myres.append(list(each))
lcp = l[:]
for j in each:
lcp.remove(j)
group(lcp, num-1, myres)

Categories