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']
Related
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
I have a string variable which has some data as shown below:
'From\tTo\nA0A3Q8IUE6\t13392634\nA4I9M8\t5072523\nE9BQL4\t13392634\nQ4Q3E9\t5654813\nE9B4M7\t13452251\nA0A088S7I8\t22574266\nA4HAG8\t5414882\nA0A3P3Z499\t5414882'
The data basically has two columns 'From' and 'To'. How do I extract the entries from the 'To' column in python?
You can use split, and then extract the data from the odd indexes, like so:
data = 'From\tTo\nA0A3Q8IUE6\t13392634\nA4I9M8\t5072523\nE9BQL4\t13392634\nQ4Q3E9\t5654813\nE9B4M7\t13452251\nA0A088S7I8\t22574266\nA4HAG8\t5414882\nA0A3P3Z499\t5414882'
print(data)
data = data.split()
to = [data[i] for i in range(3, len(data), 2)]
print(to)
In python you could split a string at specific chars, in your case \n delimits the row and \t delimits the column
something like this should work:
string='From\tTo\nA0A3Q8IUE6\t13392634\nA4I9M8\t5072523\nE9BQL4\t13392634\nQ4Q3E9\t5654813\nE9B4M7\t13452251\nA0A088S7I8\t22574266\nA4HAG8\t5414882\nA0A3P3Z499\t5414882'
f=[]
t=[]
for row in string.split("\n")[1:]:
fr,to=row.split("\t")
f.append(fr)
t.append(to)
print(f,t)
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
How can I find an element or his index in a list by a few characters of it?
For Example:
x = input()
list = ['alberto', 'kari', 'mino']
#now I want to find the element, which starts or contain x (for exampke x = 'albe')
#the next similir element should be found
#and then remove it
for item in list:
If 'alb' in item:
Item.pop()
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 3 years ago.
Improve this question
Text file or string:
SomeText1/SomeText2/SomeText3/SomeText4/SomeText5
#What I am looking for:
split_func(3, "/")
>>> SomeText3
Try:
s = "SomeText1/SomeText2/SomeText3/SomeText4/SomeText5"
# s.split("/") returns a list of strings, split at the "/"
# I.e. ["SomeText1", "SomeText2", "SomeText3", "SomeText4", "SomeText5"]
# Then take the second element (remembering that the count starts at 0
result = s.split("/")[2]
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
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))}