简体   繁体   English

如何从列表中获取 python 到 output 只是字符串?

[英]How can I get python to output just string out of a list?

import random as r

list = ['left','right','center']

shoot = r.choices(list)
print('python says, shoot ' + str(shoot))

How can I get it to output just left or right or center instead of ['left']我怎样才能把它拿到 output 只是leftrightcenter而不是['left']

The method random.choices make multiples choice, use random.choice方法random.choices进行多项选择,使用random.choice

import random as r

values = ['left','right','center']

shoot = r.choice(values)
print('python says, shoot', shoot)

Note笔记

  • don't use list as variable name, that's python list constructor不要使用list作为变量名,即 python 列表构造函数
  • shoot is already a string, no need of str() shoot已经是字符串了,不需要str()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我不知道如何在 Python 中获得正确的 output - I can't figure out how to get the right output in Python 参数作为列表输入(即 [4,5,6])。 但是如何将输出作为列表而不仅仅是整数 - The argument is entered as a list (ie. [4,5,6]). But how can I get the output as a list and not just integers 如何从 python 中的文件文件夹中获取列表列表? - How can I get a list of lists out a folder of files in python? 如何在python中获取列表的列表的String的第n个元素? - How can I get the nth element of String for list of list in python? 我不知道如何获取此脚本以将受感染文件的列表打印到输出文件 - I can't figure out how to get this script to print a list of infected files to an output file 如何挑选出列表中的某些元素并通过用户输入获得 output? - How can I single out certain elements in a list and get an output with user input? 如何将 output 变成 python 中的列表? - How can I turn the output into a list in python? 如何使输出打印出数字列表? - How can I make the output print out a list of numbers? 如何使用正则表达式找出Python列表中是否有任何一项在字符串中? - How can I find out if any one item in a Python list is in a string using regex? 如何 zip 只是一个带有 python 值列表的字符串? - How to zip just a string with a list of values in python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM