我想知道如何从索引中获取字符串。
例如:
user_input
我输入5 5 4 6 1
number_pick
我输入4
Value: 6
这是我的代码。它没有给我正确的价值。如果有人可以提供帮助那将是有用的。 (我是Python的初学者)
import math
user_input1 = input("Your chosen number: ")
number_pick = input(" Num?")
value_of_pick1 = user_input1.find(number_pick)
value1 = user_input1[value_of_pick1]
print("Value: ", value1)
答案 0 :(得分:1)
您可以将find()
解析为number_pick
int
。
我会使用str.split()
方法,它只需要一个字符串,然后按给定的分隔符拆分。
user_input = input("Your chosen number: ").split(' ')
# I use .split(' ') to make '5 4 4 6 1' into ['5', '4', '4', '6', '1']
# Because now it will be easier to index it
number_pick = int(input("Num: "))
# If we enter 4, this will be the integer 4, not '4'
# And now we just take the element with our index - 1,
# because lists are zero-indexed
value = user_input[number_pick - 1]
print("Value: {}".format(value)) # Value: 6
运行此代码后:
>>> Your chosen number: 5 5 4 6 1
['5', '4', '4', '6', '1']
>>> Num: 4
4
Value: 6
我认为math
对于这段代码来说是多余的。
split()
用法说明:当我们为5 4 4 6 1
变量输入user_input
时,它的值只是字符串"5 4 4 6 1"
。
当我们对其.split(' ')
进行操作时,它会创建一个包含我们值的列表,以" "
分隔。
因此,在进行拆分后,我们有['5', '4', '4', '6', '1']
。有关详细信息,请参阅the docs。
答案 1 :(得分:0)
问题是在列表中索引从0开始而不是1.所以这应该给你想要的结果:
value_of_pick1 = user_input1.find(number_pick - 1)
答案 2 :(得分:0)
试试这个:
user_input1 = input("Your chosen number: ").split()
number_pick = input(" Num?")
print("Value: ", user_input1[int(number_pick)-1]) #index starts at 0
答案 3 :(得分:0)
您误解了find
中string
选项的性质以及它返回索引的想法,list
。
find
确定字符串str是否出现在字符串中并返回index
,str
是string
中>>> a="now is the winter"
>>> a.find("winter")
11
的起点
所以:
>>> a="5 5 4 6 1"
>>> a.find("4")
4
“冬天”这个词出现在a中,从11号位置开始(从零位开始)
在你的情况下:
find
是正确的答案。
扩展这一点,您可以从>>> a="now is the winter of our discontent, made glorious Summer by this Son of York"
>>> f="winter"
>>> x = a.find(f)
>>> a[x:]
'winter of our discontent, made glorious Summer by this Son of York'
>>> a[x:len(f)+x]
'winter'
package com.png.kotlinsample
/**
* Created by admin on 13-08-2017.
*/
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
/**
* Created by prashant on 6/19/2017.
*/
class CustomAdapter(val userList: ArrayList<User>, val listener: (User) -> Unit) : RecyclerView.Adapter<CustomAdapter.ViewHolder>() {
//this method is returning the view for each item in the list
class MyAdapter(val userList: ArrayList<User>)
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomAdapter.ViewHolder {
val v = LayoutInflater.from(parent.context).inflate(R.layout.row_layout, parent, false)
return ViewHolder(v)
}
//this method is binding the data on the list
override fun onBindViewHolder(holder: CustomAdapter.ViewHolder, position: Int) {
holder.bindItems(userList[position],listener)
}
//this method is giving the size of the list
override fun getItemCount(): Int {
return userList.size
}
//the class is hodling the list view
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
fun bindItems(user: User, listener: (User) -> Unit) = with(itemView) {
val textViewName = itemView.findViewById(R.id.textViewUsername) as TextView
val textViewAddress = itemView.findViewById(R.id.textViewAddress) as TextView
textViewName.text = user.name
textViewAddress.text = user.address
setOnClickListener { listener(user) }
}
}
}
最佳运行答案将其转换为列表,因为这似乎是您真正想要做的事情。
答案 4 :(得分:0)
您可以使用此代码而不是使用查找来实现相同的目标。
user_input1 = input("Your chosen number: ").split()
#spliting input into list
number_pick = int(input(" Num?"))
#explicitly converting into integer
user_input1_tolist = [int(i) for i in user_input1]
#using list comprehension to convert into list
value1 = user_input1_tolist[number_pick]
#index starting from zero
print("Value: ", value1)