如何在 python 中将输入字符串拆分为单独的可用整数
How do I split an input String into seperate usable integers in python
我正在尝试将输入字符串 xyz 拆分为 3 个标记,然后拆分为 3 个整数,分别称为 x、y 和 z。
我希望它这样做,这样我就可以减少输入,然后能够将它们用于 mc.setblocks(x1, y1, z1, x, y, z, BlockId)
的坐标。我如何将它分开,以便它变成 3 个不同的整数 and\or 将它们分成令牌来这样做?我知道如何在 java 中执行此操作,但我不知道如何在 python 中执行此操作。它应该看起来像这样:
xyz1 = input("enter first coordinates example: 102 36 74")
st = StringTokenizer(xyz1)
x = st.nextToken
y = st.nextToken
z = st.nextToken
你可以将 xyz1
中的字符串分割成这样的整数
xyz_list = [int(x) for x in xyz1.split(' ')]
如果您不想将这些整数放在列表中,而是希望将它们存储到单独的变量中,只需这样做
x = xyz_list[0]
y = xyz_list[1]
z = xyz_list[2]
您可以使用 string
对象的 split()
方法,该方法默认按空白字符拆分。这将为您提供一个单独的字符串列表。要将每个字符串转换为 integer
,您可以使用理解。假设输入的形式正确,下面的一行就可以做到:
x, y, z = ( int(coord) for coord in xyz1.split() )
我试着少写一些 "pythonically" 这样你就可以看到发生了什么:
xyz1 = input("Enter first 3 coordinates (example: 102 36 74): ")
tokens = xyz1.split() # returns a list (ArrayList) of tokenized values
try:
x = int(tokens[0]) # sets x,y,z to the first three tokens
y = int(tokens[1])
z = int(tokens[2])
except IndexError: # if 0,1,2 are out of bounds
print("You must enter 3 values")
except ValueError: # if the entered number was of invalid int format
print("Invalid integer format")
如果您输入的坐标不止三个,您可以对输入进行分词并对其进行循环,将每个分词转换为 int 并将其附加到列表中:
xyz1 = input("Enter first 3 coordinates (example: 102 36 74): ")
tokens = xyz1.split() # returns a list (ArrayList) of tokenized values
coords = [] # initialise empty list
for tkn in tokens:
try:
coords.append(int(tkn)) # convert token to int
except ValueError: # if the entered number was of invalid int format
print("Invalid integer format: {}".format(tkn))
raise
print(coords) # coords is now a list of integers that were entered
有趣的是,您几乎可以在一行中完成上述操作。这是一种更 pythonic 的方式,因此您可以将其与上面的方式进行对比以了解其含义:
try:
xyz1 = input("Enter first 3 coordinates (example: 102 36 74): ")
coords = [int(tkn) for tkn in xyz1.split()]
except ValueError: # if the entered number was of invalid int format
print("Invalid integer format: {}".format(tkn))
raise
我正在尝试将输入字符串 xyz 拆分为 3 个标记,然后拆分为 3 个整数,分别称为 x、y 和 z。
我希望它这样做,这样我就可以减少输入,然后能够将它们用于 mc.setblocks(x1, y1, z1, x, y, z, BlockId)
的坐标。我如何将它分开,以便它变成 3 个不同的整数 and\or 将它们分成令牌来这样做?我知道如何在 java 中执行此操作,但我不知道如何在 python 中执行此操作。它应该看起来像这样:
xyz1 = input("enter first coordinates example: 102 36 74")
st = StringTokenizer(xyz1)
x = st.nextToken
y = st.nextToken
z = st.nextToken
你可以将 xyz1
中的字符串分割成这样的整数
xyz_list = [int(x) for x in xyz1.split(' ')]
如果您不想将这些整数放在列表中,而是希望将它们存储到单独的变量中,只需这样做
x = xyz_list[0]
y = xyz_list[1]
z = xyz_list[2]
您可以使用 string
对象的 split()
方法,该方法默认按空白字符拆分。这将为您提供一个单独的字符串列表。要将每个字符串转换为 integer
,您可以使用理解。假设输入的形式正确,下面的一行就可以做到:
x, y, z = ( int(coord) for coord in xyz1.split() )
我试着少写一些 "pythonically" 这样你就可以看到发生了什么:
xyz1 = input("Enter first 3 coordinates (example: 102 36 74): ")
tokens = xyz1.split() # returns a list (ArrayList) of tokenized values
try:
x = int(tokens[0]) # sets x,y,z to the first three tokens
y = int(tokens[1])
z = int(tokens[2])
except IndexError: # if 0,1,2 are out of bounds
print("You must enter 3 values")
except ValueError: # if the entered number was of invalid int format
print("Invalid integer format")
如果您输入的坐标不止三个,您可以对输入进行分词并对其进行循环,将每个分词转换为 int 并将其附加到列表中:
xyz1 = input("Enter first 3 coordinates (example: 102 36 74): ")
tokens = xyz1.split() # returns a list (ArrayList) of tokenized values
coords = [] # initialise empty list
for tkn in tokens:
try:
coords.append(int(tkn)) # convert token to int
except ValueError: # if the entered number was of invalid int format
print("Invalid integer format: {}".format(tkn))
raise
print(coords) # coords is now a list of integers that were entered
有趣的是,您几乎可以在一行中完成上述操作。这是一种更 pythonic 的方式,因此您可以将其与上面的方式进行对比以了解其含义:
try:
xyz1 = input("Enter first 3 coordinates (example: 102 36 74): ")
coords = [int(tkn) for tkn in xyz1.split()]
except ValueError: # if the entered number was of invalid int format
print("Invalid integer format: {}".format(tkn))
raise