Próbuję stworzyć funkcję, która zamienia tekst na wieprzowy łaciński: prostą transformację tekstu, która modyfikuje każde słowo, przenosząc pierwszy znak na koniec i dodając „ay” na końcu. Ale wszystko, co otrzymuję, to pusta lista. Jakieś wskazówki?

def pig_latin(text):
  say = ""
  words = say.split()
  for word in words:
    endString= str(word[1]).upper()+str(word[2:])
    them=endString, str(word[0:1]).lower(), 'ay'
    word = ''.join(them)
  return words

print(pig_latin("hello how are you")) # Should be "ellohay owhay reaay ouyay"
print(pig_latin("programming in python is fun")) # Should be "rogrammingpay niay ythonpay siay unfay"
0
DanDan 2 kwiecień 2020, 03:28

12 odpowiedzi

Najlepsza odpowiedź
def pig_latin(text):
  words = text.split()
  pigged_text = []

  for word in words:
    word = word[1:] + word[0] + 'ay'
    pigged_text.append(word)

  return ' '.join(pigged_text)

print(pig_latin("hello how are you"))

Wyniki: ellohay owhay reaay ouyay

2
emremrah 2 kwiecień 2020, 00:43
def pig_latin(text):
  say = ""
  words = text.split()
  for word in words:
    word=word[1:] + word[0] + "ay" + " "
    say +=word
  return say
        
print(pig_latin("hello how are you"))
0
RAKESH AGARWAL 25 sierpień 2020, 06:34
def pig_latin(text):
  # Separate the text into words
  words = text.split()
  newWord = []
  for word in words:
    # Create the pig latin word and add it to the list
    n = ""
    for char in range(len(word)-1, -1, -1):
      if(char == 0) :
        n+= (word[char]+"ay")
      n += word[char]
    newWord.append(n)    
    # Turn the list back into a phrase
  return " ".join(newWord)
    
print(pig_latin("hello how are you")) # Should be "ellohay owhay reaay ouyay"
print(pig_latin("programming in python is fun")) # Should be "rogrammingpay niay ythonpay siay unfay"
0
M Z 16 sierpień 2020, 20:50

Oparty na odpowiedzi Hermana Singha, ale pomijając anty-wzorzec (może być trochę mocny, aby to tak nazwać) tworzenia pustej listy i dołączania do niej w pętli for.

def pig_latin(text):
    words = [
        word[1:] + word[0] + "ay"
        for word in text.split()
    ]
    return " ".join(words)


print(pig_latin("hello how are you"))  # Should be "ellohay owhay reaay ouyay"
print(pig_latin("programming in python is fun"))  # Should be "rogrammingpay n

Które można zredukować do jednej linii, ale myślę, że nie poprawia to czytelności.

def pig_latin(text):
    return " ".join([word[1:] + word[0] + "ay" for word in text.split()])


print(pig_latin("hello how are you"))  # Should be "ellohay owhay reaay ouyay"
print(pig_latin("programming in python is fun"))  # Should be "rogrammingpay siay unfay
0
KeithWM 30 lipiec 2020, 11:50

Oto rozwiązanie, które zadziałało w moim przypadku:

   def pig_latin(text):
      # Separate the text into words
        words = text.split()
        #creating a new empty list called pig
        pig=[]
        # creating a for loop to alter every word in words
        for word in words:
            
            """here we are assigning the altered word to a new variable pigged_word. The we can remove first word alone from the original word usin
                indexing and add/concat the letter at zeroth index (1st letter) back to the
                    word and add/concat new string 'ay' at end """
            
            pigged_word=word[1:]+word[0]+'ay'
            #append the altered words to the list
            # use append instead of insert since you can add word to end of list
            pig.append(pigged_word)
            #now convert it back to 
        return (' '.join(pig))
            
    print(pig_latin("hello how are you")) # Should be "ellohay owhay reaay ouyay"
    print(pig_latin("programming in python is fun")) # Should be "rogrammingpay niay ythonpay siay unfay"

Prosimy o zrozumienie pytania i rozwiązanie go. Dzielenie zmiennej say nie zadziała, w idealnym przypadku będziesz musiał podzielić tekst na części, a nawet wtedy nie musisz dzielić za pomocą " "

0
Dale K 24 lipiec 2020, 22:50

Próbowałem tego i zadziałało


def pig_latin(text):
  say = []

  # Separate the text into words

  words = text.split()
  for word in words:

    # Create the pig latin word and add it to the list

    word = word[1:] + word[0] + "ay"
    say.append(word)

    # Turn the list back into a phrase

  return " ".join(say)

print(pig_latin("hello how are you")) # Should be "ellohay owhay reaay ouyay"

print(pig_latin("programming in python is fun")) # Should be "rogrammingpay niay ythonpay siay unfay"


Otwarta na sugestie

1
Hemang Singh 30 lipiec 2020, 11:19
def pig_latin(text):
  say = []
  # Separate the text into words
  words = text.split(" ")
  for word in words:
    # Create the pig latin word and add it to the list
    say.append(word[1:]+word[0]+'ay')
    # Turn the list back into a phrase
  return " ".join(x for x in say)
0
Rohan Kumara 4 luty 2021, 19:17

Rozwiązanie bez .join i .append:

def pig_latin(text):
  say = ""
  words = text.split()
  for word in words:
    pig_word = word[1:] + word[0] + "ay "
    say += pig_word
  return say
        
print(pig_latin("hello how are you")) # "ellohay owhay reaay ouyay"
print(pig_latin("programming in python is fun")) # "rogrammingpay niay ythonpay siay unfay"
0
Eugene S. 18 wrzesień 2020, 13:16

Def pig_latin (tekst):

say = ""
  pig_list= []
  # Separate the text into words
  words = text.split(' ')
  for word in words:
    # Create the pig latin word and add it to the list
    word = word[1:] + word[0] + "ay"
    # print(word)
    pig_list.append(word)
    # Turn the list back into a phrase
    say = ' '.join(pig_list)
  return say
0
Ahmed Osman 4 wrzesień 2020, 21:24

Po prostu użyj funkcji rozumienia listy, aby uzyskać zwięzłość i prostotę:

’ ‘.join([word[1:] + word[0] + ‘ay’ for word in text.split(‘ ‘)])

0
Ryan Rudes 25 sierpień 2020, 06:51

Pracuje dla mnie

def pig_latin(sentence):
    output = []
    for word in sentence.split(" "):
        word = word[1:] + word[0] + "ay"
        output.append(word)
    return " ".join(output)
0
ventaquil 2 kwiecień 2020, 00:34

Po prostu użyj listy składanej na każdym słowie, a następnie połącz je ponownie.

Edycja: to rozwiązanie ma problemy z wieloma spacjami, na przykład gdy ludzie umieszczają dwie spacje po colon: !

def dowork(sentence):

    def pigword(word):
        return "".join([word[1:], word[0], "ay"])

    return " ".join([pigword(word) for word in sentence.split()])


dataexp = [
    ("hello how are you","ellohay owhay reaay ouyay"),
    ("programming in python is fun","rogrammingpay niay ythonpay siay unfay")
    ]

for inp, exp in dataexp:
    got = dowork(inp)
    msg = "exp :%s:  for %s \n      got :%s:" % (exp, inp, got)
    if exp == got:
        print("good! %s" % msg)
    else:
        print("bad ! %s" % msg)

Wynik

good! exp :ellohay owhay reaay ouyay:  for hello how are you
      got :ellohay owhay reaay ouyay:
good! exp :rogrammingpay niay ythonpay siay unfay:  for programming in python is fun
      got :rogrammingpay niay ythonpay siay unfay:
0
JL Peyret 2 kwiecień 2020, 00:49