Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在python中打印字符串中包含字母的值的索引?_Python_String_List - Fatal编程技术网

如何在python中打印字符串中包含字母的值的索引?

如何在python中打印字符串中包含字母的值的索引?,python,string,list,Python,String,List,我有: *月份=(“一月”、“二月”、“三月”、“十二月”)(包括所有月份) 我应该输入一个月的3个字母缩写,然后得到这个月的索引值。到目前为止,我已经: for M in MONTHS: shortMonths = M[0:3] print shortMonths 一月二月三月四月五月六月七月八月九月十月十一月十二月 我注意到shortMonths中的输出月份没有引号,这使得无法测试缩写是否在shortMonths中: MMM=“二月” 打印列表(shortMonths)。索

我有:

*月份=(“一月”、“二月”、“三月”、“十二月”)(包括所有月份)

我应该输入一个月的3个字母缩写,然后得到这个月的索引值。到目前为止,我已经:

for M in MONTHS:
    shortMonths = M[0:3]
    print shortMonths
一月二月三月四月五月六月七月八月九月十月十一月十二月

我注意到shortMonths中的输出月份没有引号,这使得无法测试缩写是否在shortMonths中:

MMM=“二月”

打印列表(shortMonths)。索引(MMM)+1#考虑到列表的第一个月,即1月,是所有月份的0+1=1,依此类推

ValueError:“Feb”不在列表中

如何在不创建函数的情况下修复此问题?
另外,这是一个作业问题。而且,我们不允许使用字典、地图或日期时间。听起来你想把
shortMonths
作为一个列表,但你只是给它分配了一个字符串

我想你想要这样的东西:

shortMonths = [] # create an empty list
for M in MONTHS:
    shortMonths.append(M[0:3]) # add new entry to the list
print shortMonths # print out the list we just created
或使用列表理解:


听起来您希望
shortMonths
成为一个列表,但您只是给它分配了一个字符串

我想你想要这样的东西:

shortMonths = [] # create an empty list
for M in MONTHS:
    shortMonths.append(M[0:3]) # add new entry to the list
print shortMonths # print out the list we just created
或使用列表理解:


shortMonths不是一个列表。按照下面的步骤做

shortMonths = []
  for M in MONTHS:
    shortmonths.append(M[0:3])

shortMonths不是一个列表。按照下面的步骤做

shortMonths = []
  for M in MONTHS:
    shortmonths.append(M[0:3])
这很简单:

>>> months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
>>> search = "Jan"
>>> months.index([i for i in months if i[:3].lower() == search.lower()][0])+1
1
>>> # Or
... def getmonth(search):
...    for i in months:
...        if i[:3].lower() == search.lower():
...            return x.index(i)+1
>>> getmonth("Feb")
2
要捕获错误,请执行以下操作:

months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
while True:
    search = ""
    while len(search) != 3:
        search = raw_input("Enter first 3 letters of a month: ")[:3]
    month = [i for i in months if i[:3].lower() == search.lower()]
    if not month: # Not in the list, empty list: []
        print "%s is not a valid abbreviation!"%search
        continue
    print months.index(month[0])+1
这很简单:

>>> months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
>>> search = "Jan"
>>> months.index([i for i in months if i[:3].lower() == search.lower()][0])+1
1
>>> # Or
... def getmonth(search):
...    for i in months:
...        if i[:3].lower() == search.lower():
...            return x.index(i)+1
>>> getmonth("Feb")
2
要捕获错误,请执行以下操作:

months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
while True:
    search = ""
    while len(search) != 3:
        search = raw_input("Enter first 3 letters of a month: ")[:3]
    month = [i for i in months if i[:3].lower() == search.lower()]
    if not month: # Not in the list, empty list: []
        print "%s is not a valid abbreviation!"%search
        continue
    print months.index(month[0])+1

尝试在解释器中打印“hi”,丢失引号的神秘问题可能会得到解决。这不是一个真正的Python问题。。。更多关于一般编程(即“打印”的含义!)。我建议你开始学习如何调试你的程序,这样你就能够发现错误并从中吸取教训。@roippi,
缺少引号的神秘性很酷:)试试
在你的解释器中打印“hi”
,缺少引号的神秘性可能会得到解决。这不是真正的Python问题。。。更多关于一般编程(即“打印”的含义!)。我建议你开始学习如何调试你的程序,这样你就能够发现错误并从中吸取教训。@roippi,
缺少引号的神秘之处很酷:)指出了OP的误解。提供指向列表comp的指针+1从我这里。指出OP的误解。提供指向列表comp的指针+从我这里得到1。