list to string using "".join() – issues with passing a list of lists of strings

huangapple go评论98阅读模式
英文:

list to string using "".join() - issues with passing a list of lists of strings

问题

I feel like maybe this is an issue with using "".join() with a list of lists of strings instead of a list of strings, but haven't found an answer.

可能是因为在使用"".join()时,您正在使用字符串列表的列表而不是字符串列表,但尚未找到答案。

Maybe it's something else that I am missing.

也许还有其他我遗漏的问题。

I hope my notes show what I have been trying (to take a list of strings turn them into a str and append 'scramble' with that string)

我希望我的注释显示了我一直在尝试的内容(将字符串列表转换为字符串并将'scramble'附加到该字符串)

英文:

I feel like maybe this is an issue with using "".join() with a list of lists of strings instead of a list of strings, but haven't found an answer.

Maybe it's something else that I am missing.

I hope my notes show what I have been trying (to take a list of strings turn them into a str and append 'scramble' with that string)

  1. # SCRAMBLE WORD/PHRASE
  2. # IMPORTS
  3. import random
  4. # VARIABLES
  5. test_words_list = ["dark", "happy holidays", "computer", "I like pie", "dogs love pizza"]
  6. # FUNCTIONS
  7. #function too big? cut down into smaller? or too small to be worth it?
  8. def scramble_string(string, seperate_words = False):
  9. scramble = ""
  10. if seperate_words == False:
  11. """Remove spaces & scramble all letters together into one string"""
  12. words_list = string.split(" ") # removes whitespace (list)
  13. print(f"letters_list= {words_list}")
  14. for word_str in words_list:
  15. word_letter_list = [letter for letter in word_str]
  16. print(f"word_letters_list = {word_letter_list}")
  17. random.shuffle(word_letter_list)
  18. print(f"After shuffle word_letters_list = {word_letter_list}")
  19. word_letter_str = "".join(word_letter_list)
  20. print(f"word_letter_str = {word_letter_str}")
  21. scramble += word_letter_str
  22. print(f"scramble = {scramble}")
  23. return scramble
  24. else:
  25. """scramble letters in words without mixing letters from different words.
  26. List words in same order with space between, but with letters scrambled"""
  27. scrambled_list = []
  28. words_list = string.split() # passed string into list of strings
  29. print(f"words_list = {words_list}")
  30. for word_str in words_list:
  31. letters_list = [letter for letter in word_str]
  32. print(f"letters_list = {letters_list}")
  33. random.shuffle(letters_list)
  34. print(f"After shuffle letters_list = {letters_list}")
  35. scrambled_list.append(letters_list) # list of lists
  36. print(f"scrambled_list = {scrambled_list}")
  37. # vv PROBLEM vv
  38. scrambled_str = "".join(scrambled_list) #"sequence item 0: expected str instance, list found"
  39. scramble += scrambled_str
  40. #scramble = ''.join(str(scrambled_list)[1:-1]) # makes scramble a list???
  41. print(f"scramble = {scramble}")
  42. return scramble
  43. # Tests
  44. #print(scramble_string(test_words_list[1])) # WORKS (returned = "hyppahadsilyo")
  45. print(scramble_string(test_words_list[-1], True)) # returns =

答案1

得分: 0

更新您的字符串列表到首先是字符串列表。我在这一行做了这个。

  1. scrambled_list.append("".join(letters_list)) # 字符串列表

不是将字符串列表附加到列表中,而是将那个字母列表组合成一个单词,然后将那个打乱的单词附加到scrambled_list中。

  1. import random
  2. # 变量
  3. test_words_list = ["dark", "happy holidays", "computer", "I like pie", "dogs love pizza"]
  4. # 函数
  5. # 函数太大了吗?要分割成更小的函数吗?还是太小不值得?
  6. def scramble_string(string, separate_words=False):
  7. scramble = ""
  8. if separate_words == False:
  9. """Remove spaces & scramble all letters together into one string"""
  10. words_list = string.split(" ") # 移除空格 (列表)
  11. print(f"letters_list= {words_list}")
  12. for word_str in words_list:
  13. word_letter_list = [letter for letter in word_str]
  14. print(f"word_letters_list = {word_letter_list}")
  15. random.shuffle(word_letter_list)
  16. print(f"After shuffle word_letters_list = {word_letter_list}")
  17. word_letter_str = "".join(word_letter_list)
  18. print(f"word_letter_str = {word_letter_str}")
  19. scramble += word_letter_str
  20. print(f"scramble = {scramble}")
  21. return scramble
  22. else:
  23. """scramble letters in words without mixing letters from different words.
  24. List words in same order with space between, but with letters scrambled"""
  25. scrambled_list = []
  26. words_list = string.split() # 将字符串转换成字符串列表
  27. print(f"words_list = {words_list}")
  28. for word_str in words_list:
  29. letters_list = [letter for letter in word_str]
  30. print(f"letters_list = {letters_list}")
  31. random.shuffle(letters_list)
  32. print(f"After shuffle letters_list = {letters_list}")
  33. scrambled_list.append("".join(letters_list)) # 字符串列表
  34. print(f"scrambled_list = {scrambled_list}")
  35. # vv 问题 vv
  36. scrambled_str = "".join(scrambled_list) # "sequence item 0: expected str instance, list found"
  37. scramble += scrambled_str
  38. #scramble = ''.join(str(scrambled_list)[1:-1]) # makes scramble a list???
  39. print(f"scramble = {scramble}")
  40. return scramble
  41. # 测试
  42. # print(scramble_string(test_words_list[1])) # 工作正常 (返回 = "hyppahadsilyo")
  43. print(scramble_string(test_words_list[-1], True))
英文:

Update your list of list of string to list of string first. I did that at this line.

  1. scrambled_list.append("".join(letters_list)) # list of lists

Instead of appending list of strings to a list, I am combining that list of letters to form a word and appending that scrambled word to the scrambled_list.

  1. import random
  2. # VARIABLES
  3. test_words_list = ["dark", "happy holidays", "computer", "I like pie", "dogs love pizza"]
  4. # FUNCTIONS
  5. #function too big? cut down into smaller? or too small to be worth it?
  6. def scramble_string(string, seperate_words = False):
  7. scramble = ""
  8. if seperate_words == False:
  9. """Remove spaces & scramble all letters together into one string"""
  10. words_list = string.split(" ") # removes whitespace (list)
  11. print(f"letters_list= {words_list}")
  12. for word_str in words_list:
  13. word_letter_list = [letter for letter in word_str]
  14. print(f"word_letters_list = {word_letter_list}")
  15. random.shuffle(word_letter_list)
  16. print(f"After shuffle word_letters_list = {word_letter_list}")
  17. word_letter_str = "".join(word_letter_list)
  18. print(f"word_letter_str = {word_letter_str}")
  19. scramble += word_letter_str
  20. print(f"scramble = {scramble}")
  21. return scramble
  22. else:
  23. """scramble letters in words without mixing letters from different words.
  24. List words in same order with space between, but with letters scrambled"""
  25. scrambled_list = []
  26. words_list = string.split() # passed string into list of strings
  27. print(f"words_list = {words_list}")
  28. for word_str in words_list:
  29. letters_list = [letter for letter in word_str]
  30. print(f"letters_list = {letters_list}")
  31. random.shuffle(letters_list)
  32. print(f"After shuffle letters_list = {letters_list}")
  33. scrambled_list.append("".join(letters_list)) # list of lists
  34. print(f"scrambled_list = {scrambled_list}")
  35. # vv PROBLEM vv
  36. scrambled_str = "".join(scrambled_list) #"sequence item 0: expected str instance, list found"
  37. scramble += scrambled_str
  38. #scramble = ''.join(str(scrambled_list)[1:-1]) # makes scramble a list???
  39. print(f"scramble = {scramble}")
  40. return scramble
  41. # Tests
  42. # print(scramble_string(test_words_list[1])) # WORKS (returned = "hyppahadsilyo")
  43. print(scramble_string(test_words_list[-1], True))

答案2

得分: 0

这是目前已经正确工作的函数(带有基本改进),供将来遇到类似问题的人参考。

  1. # 扰乱单词/短语
  2. # 导入
  3. import random
  4. # 变量
  5. test_words_list = ["dark", "happy holidays", "computer", "I like pie", "dogs love pizza"]
  6. # 函数
  7. def scramble_string(string: str, seperate_words = False) -> str:
  8. string = string.lower()
  9. scramble = ""
  10. if seperate_words == False:
  11. """Remove spaces & scramble all letters together into one string"""
  12. words_list = string.split(" ")
  13. for word_str in words_list:
  14. word_letter_list = [letter for letter in word_str] # 每个单词中字母的列表
  15. random.shuffle(word_letter_list)
  16. scrambled_word_str = "".join(word_letter_list)
  17. scramble += scrambled_word_str
  18. else:
  19. """scramble letters in words WITHOUT mixing letters from different words.
  20. List scrambled words in orginal order (with space between)"""
  21. words_list = string.split()
  22. for word in words_list:
  23. scrambled_list = [letter for letter in word]
  24. random.shuffle(scrambled_list)
  25. scrambled_str = "".join(scrambled_list)
  26. scrambled_str += " " # 在字符串中单词之间添加空格
  27. scramble += scrambled_str
  28. return scramble
  29. # 测试
  30. print(scramble_string(test_words_list[-2])) # 工作正常(返回值 = "hyppahadsilyo")
  31. print(scramble_string(test_words_list[-1], True)) # 工作正常(返回值 = "sdog eovl zapzi")
  32. # 工作笔记(用于跟踪和从错误中学习)
  33. # 错误来源:scrambled_list.append(letters_list)
  34. # 失败的解决方案(使scramble成为列表):scramble = ''.join(str(scrambled_list)[1:-1])
  35. # 解决方案(错误:“sequence item 0: expected str instance, list found”):scrambled_list.append("".join(letters_list))
  36. # 错误已解决(将字符串列表的列表给予"".join):scrambled_str = "".join(scrambled_list)
英文:

This is the function now working correctly (with basic improvements) for anyone with a similar issue in the future.

  1. # SCRAMBLE WORD/PHRASE
  2. # IMPORTS
  3. import random
  4. # VARIABLES
  5. test_words_list = ["dark", "happy holidays", "computer", "I like pie", "dogs love pizza"]
  6. # FUNCTIONS
  7. def scramble_string(string: str, seperate_words = False) -> str:
  8. string = string.lower()
  9. scramble = ""
  10. if seperate_words == False:
  11. """Remove spaces & scramble all letters together into one string"""
  12. words_list = string.split(" ")
  13. for word_str in words_list:
  14. word_letter_list = [letter for letter in word_str] # list of letter in word for each word in words_list
  15. random.shuffle(word_letter_list)
  16. scrambled_word_str = "".join(word_letter_list)
  17. scramble += scrambled_word_str
  18. else:
  19. """scramble letters in words WITHOUT mixing letters from different words.
  20. List scrambled words in orginal order (with space between)"""
  21. words_list = string.split()
  22. for word in words_list:
  23. scrambled_list = [letter for letter in word]
  24. random.shuffle(scrambled_list)
  25. scrambled_str = "".join(scrambled_list)
  26. scrambled_str += " " # Add space between words in string
  27. scramble += scrambled_str
  28. return scramble
  29. # Tests
  30. print(scramble_string(test_words_list[-2])) # WORKS (returned = "hyppahadsilyo")
  31. print(scramble_string(test_words_list[-1], True)) # WORKS (returned = "sdog eovl zapzi")
  32. # Working Notes (to track & learn from mistakes)
  33. # Error SOURCE: scrambled_list.append(letters_list)
  34. # Failed Solution(makes scramble a list): scramble = ''.join(str(scrambled_list)[1:-1])
  35. # Soultion(Error: "sequence item 0: expected str instance, list found") scrambled_list.append("".join(letters_list))
  36. # Error RESOLVED (list of lists of strings given to "".join): scrambled_str = "".join(scrambled_list)

huangapple
  • 本文由 发表于 2023年6月26日 20:57:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76556916.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定