如何将Complex Collection转换为String?

时间:2016-05-16 01:02:30

标签: python-3.x collections dataset

尝试创建一个非常基本的脚本,该脚本将显示一组字符串中的随机字符串。但是,我需要它显示好像它是一个打印功能(即没有括号或逗号)。我尝试过使用连接并遇到错误(不可用类型:列表)

name = ("Tom")
greeting = {
["Hello", name, "How are you today?"],
["Welcome", name, "How was your day?"],
["Greetings", name, "Shall we play a game?"],
["Well hey there", name, "Whats up?"],
}
print (', '.join(greeting))

真的非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

您的问题是您正在greeting dictionary而不是list  #allows us to use randint function from random import randint name = ("Tom") #change greeting from a dictionary to a list by replacing { with [ greeting = [ ["Hello", name, "How are you today?"], ["Welcome", name, "How was your day?"], ["Greetings", name, "Shall we play a game?"], ["Well hey there", name, "Whats up?"], ] #assign myGreeting as a random num between 0 and 3 myGreeting = randint(0,len(greeting)-1) #define out output to be printed to console (its a String) output = "" #itterate through our random greeting word by word and #concatinate to output variable one word at a time for myWord in greeting[myGreeting]: output+=myWord+" " print (output)

继承我对您的代码的修复:

var j = function(){
 this.name = "Joe";
 var no = "23";
}

<强>输出: enter image description here

希望这有帮助! 〜枪手