【问题标题】:How to get the value from a list + input如何从列表+输入中获取值
【发布时间】:2016-09-08 20:29:35
【问题描述】:

很抱歉标题模糊,但这是我的问题。 (我刚开始这项研究)

例如

list_1 = [a, b, c, d, e, f]

input_1 = input('question1')
input_2 = input('question2')

让我们说他们选择了

input_1  = b
input_2 =  f

我需要一些可以打印两个所选字母之间的字母的东西。 所以你会得到

c
d
e

我尝试将 for 语句与 if 语句一起使用,但效果不佳。 我还需要检查 input2 在列表中是否比 input1 更远的东西。我试过了

if input_2 > input_1:
     print('yey')
else:
     print('not recognized, automatically f')
     input_2 = list_1[-1]

抱歉初学者的问题=(

【问题讨论】:

    标签: python list for-loop input


    【解决方案1】:

    您可能希望遍历列表并像比较数字一样比较字符。

    for i in range(len(list_1)):
        if list_1[i] > input_1 and list_1[i] < input_2:
            print list_1[i]
    

    【讨论】:

    • 很高兴我能帮上忙。
    【解决方案2】:
    if input_1 in list_1 and input_2 in list_1:
        print(list_1[list_1.index(input_1)+1:list_1.index(input_2)])
    else:
        print('didn't find input_1 or input_2) 
    

    这个?

    如果您需要从 input_2 之前或 input_1 之后自动打印列表的其余部分,您可以添加 if 语句,如

    if input_1 in list_1 and input_2 in list_1:
        print(list_1[list_1.index(input_1)+1:list_1.index(input_2)])
    elif input_1 in list_1 and not input_2 in list_1:
        print(list_1[list_1.index(input_1)+1:])
    elif not input_1 in list_1 and input_2 in list_1:
        print(list_1[0:list_1.index(input_2)])
    else:
        print('didn't find input_1 or input_2) 
    

    【讨论】:

      【解决方案3】:

      input_1input_2 是列表中的元素。

      您可以在 python 中使用 slice 来获取输入元素之间存在的元素。

       index_1 = list_1.index(input_1)
       index_2 = list_l.index(input_2)
       new_list = list_1[index_1:index_2]
       print new_list
      

      【讨论】:

      • 这对像我这样的初学者来说很简单也很好。非常感谢!
      【解决方案4】:

      你可能想使用 list.index() 方法,你可以找到文档here

      在你的情况下,这看起来像

      i1 = list_1.index(input_1)
      i2 = list_1.index(input_2)
      
      if i2 > i1:
          print('yey')
      

      然后你可以使用切片和循环来打印子列表

      sublist = list_1[i1:i2]
      for c in sublist:
          print(c)
      

      【讨论】:

        【解决方案5】:

        您可以遍历列表并取出每个输入的索引,然后测试您想要的条件。像这样的:

        list1 = ['a', 'b', 'c', 'd', 'f']
        input_1  = 'b'
        input_2 =  'f'
        input_1_index = -1
        input_2_index = -1
        
        for i in range(len(list1)):
            if list1[i] == input_1:
                input_1_index = i
            elif list1[i] == input_2:
                input_2_index = i
        
        if input_1_index == -1 or input_2_index == -1 or input_1_index > input_2_index:
            print('error')
        
        else:
            print(list1[(input_1_index+1):input_2_index])
        

        【讨论】:

          【解决方案6】:

          这是我想要做的事情的版本。确保字母表是整个字母列表,并且您可以检查用户输入是否是字母表中的字符串。

          alphabet = ['a', 'b', 'c', 'd']
          
          input_1 = str(input(""))
          input_2 = str(input(""))
          
          Input1Pos = alphabet.index(input_1)
          
          Input2Pos = alphabet.index(input_2)
          
          if (Input2Pos > Input1Pos):
              Input1Pos = Input1Pos + 1
              print alphabet[Input1Pos:Input2Pos]
          
          elif (Input2Pos == Input1Pos):
          print "There are no characters between these two inputs"
          else:
              print "The second input was before the first input"
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2016-12-28
            • 1970-01-01
            • 2020-06-01
            • 2017-02-21
            • 2021-01-16
            • 2020-11-14
            • 2020-03-08
            • 1970-01-01
            相关资源
            最近更新 更多