Python Forum
hope to know a rank, if it has. if not, just show 0.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
hope to know a rank, if it has. if not, just show 0.
#1
Dear All,

I think my question would be easy for most, not just for me.
I've spent two day to solve it, but I'm now giving up!

Please advice...

import re 

data = """
t=A n=1 www.aaa.com
t=A n=2 www.bbb.com
t=A n=3 www.ccc.com

t=B n=1 www.aaa.com
t=B n=2 www.bbb.com
t=B n=3 www.ddd.com

t=C n=1 www.bbb.com
t=C n=2 www.ddd.com
t=C n=3 www.ccc.com

t=D n=1 www.aaa.com
t=D n=2 www.bbb.com
t=D n=3 www.ccc.com
t=D n=4 www.eee.com
"""

domain = "www.ddd.com"
for line in data.splitlines():
	tn = re.compile("t=(\w)\s+n=(\d)")
	get = tn.findall(line)
	get = str(get)
	if domain in line:
		print("The rank is " + get + " " + domain)	
From above code, I hope to get the result as below;

The rank is 0 in A group.
The rank is 3 in B group.
The rank is 2 in C group.
The rank is 0 in D group.

But I got the result as below: Wall

The rank is [('B', '3')] www.ddd.com
The rank is [('C', '2')] www.ddd.com

Could you please give me your teachings?
Thank you for your interesting.
Reply
#2
No need for re
from collections import OrderedDict
data = """
t=A n=1 www.aaa.com
t=A n=2 www.bbb.com
t=A n=3 www.ccc.com

t=B n=1 www.aaa.com
t=B n=2 www.bbb.com
t=B n=3 www.ddd.com

t=C n=1 www.bbb.com
t=C n=2 www.ddd.com
t=C n=3 www.ccc.com
 
t=D n=1 www.aaa.com
t=D n=2 www.bbb.com
t=D n=3 www.ccc.com
t=D n=4 www.eee.com
"""

def process_data(data):
    ranks = OrderedDict()
    for line in data.split('\n'):
        line = line.strip() # because you have rows with just space between groups
        if line:
            line_data = line.split(' ')
            url = line_data[-1]
            group, rank = [item.split('=')[-1] for item in line_data[:-1]]
            ranks.setdefault(group, {})[url] = rank
    return ranks
    
def get_ranks(url, ranks):
    return ((url_ranks.get(url, 0), group) for group, url_ranks in ranks.items())

    
if __name__ == '__main_'':   
    url = "www.ddd.com"
    all_ranks = process_data(data=data)
    for rank, group in get_ranks(url=url, ranks=all_ranks):
        print('The rank is {} is {} group'.format(rank, group))
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Hi Tyyi,

The findall will return all the matched pattern in the line, from there you need to parse and print accordingly.

For reference read the doc,
https://docs.python.org/2/library/re.htm...ll-adverbs

Thanks,
Praba
Reply
#4
Thank you for your teaching. buran!

I did copy and paste your code, then execute in console.
But I didn't see anything in the console.
I will try to do to see something that I hope to get.

I really appreciate your help again.
I'm very very newbie to Python, also other languages.
So my questions will be usually idiots, but I will learn from it.
Thank YOU!



(Jul-10-2018, 09:59 AM)buran Wrote: No need for re
from collections import OrderedDict
data = """
t=A n=1 www.aaa.com
t=A n=2 www.bbb.com
t=A n=3 www.ccc.com

t=B n=1 www.aaa.com
t=B n=2 www.bbb.com
t=B n=3 www.ddd.com

t=C n=1 www.bbb.com
t=C n=2 www.ddd.com
t=C n=3 www.ccc.com
 
t=D n=1 www.aaa.com
t=D n=2 www.bbb.com
t=D n=3 www.ccc.com
t=D n=4 www.eee.com
"""

def process_data(data):
    ranks = OrderedDict()
    for line in data.split('\n'):
        line = line.strip() # because you have rows with just space between groups
        if line:
            line_data = line.split(' ')
            url = line_data[-1]
            group, rank = [item.split('=')[-1] for item in line_data[:-1]]
            ranks.setdefault(group, {})[url] = rank
    return ranks
    
def get_ranks(url, ranks):
    return ((url_ranks.get(url, 0), group) for group, url_ranks in ranks.items())

    
if __name__ == '__main_'':   
    url = "www.ddd.com"
    all_ranks = process_data(data=data)
    for rank, group in get_ranks(url=url, ranks=all_ranks):
        print('The rank is {} is {} group'.format(rank, group))
Reply
#5
My bad, fix line 36
from collections import OrderedDict
data = """
t=A n=1 www.aaa.com
t=A n=2 www.bbb.com
t=A n=3 www.ccc.com

t=B n=1 www.aaa.com
t=B n=2 www.bbb.com
t=B n=3 www.ddd.com

t=C n=1 www.bbb.com
t=C n=2 www.ddd.com
t=C n=3 www.ccc.com
 
t=D n=1 www.aaa.com
t=D n=2 www.bbb.com
t=D n=3 www.ccc.com
t=D n=4 www.eee.com
"""

def process_data(data):
    ranks = OrderedDict()
    for line in data.split('\n'):
        line = line.strip()
        if line:
            line_data = line.split(' ')
            url = line_data[-1]
            group, rank = [item.split('=')[-1] for item in line_data[:-1]]
            ranks.setdefault(group, {})[url] = rank
    return ranks
    
def get_ranks(url, ranks):
    return ((url_ranks.get(url, 0), group) for group, url_ranks in ranks.items())

    
if __name__ == '__main__':   
    url = "www.ddd.com"
    all_ranks = process_data(data=data)
    for rank, group in get_ranks(url=url, ranks=all_ranks):
        print('The rank is {} in {} group'.format(rank, group))
Output:
The rank is 0 in A group The rank is 3 in B group The rank is 2 in C group The rank is 0 in D group >>>
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
Thank you for your quick revised answer!

When I executed the first one, I got the error message, so I just removed ' from 36 line, not added _ Wall
I got the result I hope to see.
I have to study your code from now on.

really appreciated your answer one more!

It is my first question after subscribed in Python forum.
I got very good impression from YOU!

(Jul-10-2018, 10:25 AM)buran Wrote: My bad, fix line 36
from collections import OrderedDict
data = """
t=A n=1 www.aaa.com
t=A n=2 www.bbb.com
t=A n=3 www.ccc.com

t=B n=1 www.aaa.com
t=B n=2 www.bbb.com
t=B n=3 www.ddd.com

t=C n=1 www.bbb.com
t=C n=2 www.ddd.com
t=C n=3 www.ccc.com
 
t=D n=1 www.aaa.com
t=D n=2 www.bbb.com
t=D n=3 www.ccc.com
t=D n=4 www.eee.com
"""

def process_data(data):
    ranks = OrderedDict()
    for line in data.split('\n'):
        line = line.strip()
        if line:
            line_data = line.split(' ')
            url = line_data[-1]
            group, rank = [item.split('=')[-1] for item in line_data[:-1]]
            ranks.setdefault(group, {})[url] = rank
    return ranks
    
def get_ranks(url, ranks):
    return ((url_ranks.get(url, 0), group) for group, url_ranks in ranks.items())

    
if __name__ == '__main__':   
    url = "www.ddd.com"
    all_ranks = process_data(data=data)
    for rank, group in get_ranks(url=url, ranks=all_ranks):
        print('The rank is {} in {} group'.format(rank, group))
Output:
The rank is 0 in A group The rank is 3 in B group The rank is 2 in C group The rank is 0 in D group >>>
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  PIL Image im.show() no show! Pedroski55 2 927 Sep-12-2022, 10:19 PM
Last Post: Pedroski55
  PIL Image im.show() no show! Pedroski55 6 4,738 Feb-08-2022, 06:32 AM
Last Post: Pedroski55
  Rank word frequent in a list standenman 1 2,799 Nov-07-2018, 09:24 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020