2016-03-23 8 views
0

hallo ich habe eine Kompressionsfunktion in Python-Code zu entwickeln, wo, wenn der EingangZuordnung Nummer zu Wort in einer Zeichenfolge in Python

'hello its me, hello can you hear me, hello are you listening'

Dann sollte der Ausgang seine

1,2,3,1,4,5,6,3,1,7,5,8

Grundsätzlich wird jedem Wort ein numerischer Wert zugewiesen und wenn das Wort wiederholt wird, ist das Wort auch. Diese Codierung ist in Python, bitte helfen Sie mir danke

+3

Hast du irgendwas probiert? StackOverflow ist kein Code-Schreibdienst. –

Antwort

3

Eine einfache Möglichkeit ist, ein Diktat zu verwenden, wenn Sie ein neues Wort finden Sie eine Schlüssel/Wert-Paarung mit einer inkrementierenden Variable, wenn Sie das Wort vor gesehen haben drucken Sie den Wert aus dem dict:

s = 'hello its me, hello can you hear me, hello are you listening' 


def cyc(s): 
    # set i to 1 
    i = 1 
    # split into words on whitespace 
    it = s.split() 
    # create first key/value pair 
    seen = {it[0]: i} 
    # yield 1 for first word 
    yield i 
    # for all var the first word 
    for word in it[1:]: 
     # if we have seen this word already, use it's value from our dict 
     if word in seen: 
      yield seen[word] 
     # else first time seeing it so increment count 
     # and create new k/v pairing 
     else: 
      i += 1 
      yield i 
      seen[word] = i 


print(list(cyc(s))) 

Ausgang:

[1, 2, 3, 1, 4, 5, 6, 3, 1, 7, 5, 8] 

Sie auch schneiden, indem Sie iter vermeiden und ruft next das erste Wort Pop, auch wenn Siemachen wollenbrauchen wir jede Interpunktion aus dem String zu entfernen, die mit str.rstrip erfolgen Cam:

from string import punctuation 
def cyc(s): 
    i = 1 
    it = iter(s.split()) 
    seen = {next(it).rstrip(punctuation): i} 
    yield i 
    for word in it: 
     word = word.rstrip(punctuation) 
     if word in seen: 
      yield seen[word] 
     else: 
      i += 1 
      yield i 
      seen[word] = i 
2

Wie über den Aufbau einer dict mit Artikel: Indexabbildung:

>>> s 
'hello its me, hello can you hear me, hello are you listening' 
>>> 
>>> l = s.split() 
>>> d = {} 
>>> i = 1 
>>> for x in l: 
     if x not in d: 
      d[x]=i 
      i += 1 


>>> d 
{'its': 2, 'listening': 8, 'hear': 6, 'hello': 1, 'are': 7, 'you': 5, 'me,': 3, 'can': 4} 
>>> for x in l: 
     print(x, d[x]) 


hello 1 
its 2 
me, 3 
hello 1 
can 4 
you 5 
hear 6 
me, 3 
hello 1 
are 7 
you 5 
listening 8 
>>> 

Wenn Sie don‘ t wollen keine Interpunktion in Ihre Split-Liste, dann können Sie tun:

>>> import re 
>>> l = re.split(r'(?:,|\s)\s*', s) 
>>> l 
['hello', 'its', 'me', 'hello', 'can', 'you', 'hear', 'me', 'hello', 'are', 'you', 'listening'] 
1
import re 
from collections import OrderedDict 

text = 'hello its me, hello can you hear me, hello are you listening' 
words = re.sub("[^\w]", " ", text).split() 
uniq_words = list(OrderedDict.fromkeys(words)) 
res = [uniq_words.index(w) + 1 for w in words] 

print(res) # [1, 2, 3, 1, 4, 5, 6, 3, 1, 7, 5, 8] 
Verwandte Themen