【问题标题】:Python, take value of a list from an inputPython,从输入中获取列表的值
【发布时间】:2016-12-28 21:14:13
【问题描述】:

大家好,我正在学习 python,我有这个问题,因为没有创建 1000 个“if”函数,我会这样做:

#Vari flag
print "choose flag"
print "(1) Syn"
print "(2) Ack"
print "(3) Push"
print "(4) Fin"
print "(5) Urg"
print "(6) Rst"

flag-list = ["--syn","--ack","--push","--fin","--urg","--rst"]
flag = raw_input(write number separated by comma: )

现在我将把写入的数字转换为“标志列表”的文本。

【问题讨论】:

标签: python


【解决方案1】:
# Note that variable names cannot contain hyphens
flag_list = ["--syn","--ack","--push","--fin","--urg","--rst"]

# This clearly has to be in quotes
user_input = raw_input("Enter numbers separated by comma:" )

# Split the user input string at the commas, and turn each element into an integer.
flag_nums = map(int, flag_num.split(','))

# List indexes start at 0, so subtract 1.
# Use brackets to access the Nth item in the list.
# This is a list comprehension.
flags = [flag_list[n - 1] for n in flag_nums]

【讨论】:

  • 我认为你应该使用 for-in 并拆分 flag_num,因为在 flag_num = 它是用逗号分隔的。 @乔纳森·莱因哈特
  • 显然你比我更擅长破译 OP 的破烂英语。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多