输入n个数字的通常方法是先询问n个数字,然后在不同的行中键入n个数字。
n = int(input())
for i in range(n):
x = int(input())
我怎么要求n并仅在一行中输入n个数字。
类似这样的东西:
>> 4 1 2 3 4
答案 0 :(得分:2)
我怎么要求n并仅在一行中输入n个数字。
如果从空格分隔的输入中可以明显看出您拥有多少个整数,则不需要不需要询问 n 。
但是,如果输入字符串格式是不可协商的,则可以通过序列解包来拆分:
n, *num_list = map(int, input().split())
例如,使用输入'4 1 2 3 4'
,您将得到以下结果:
print(n, num_list)
4 [1, 2, 3, 4]
要了解以上逻辑:
input().split()
将由空格输入的字符串分成一个列表。map(int, X)
返回int
的可迭代,应用于X
中的每个元素。n, *num_list = map(...)
迭代map
对象,并分为第一个对象和其余对象。更多习惯用法是自己计算n
:
num_list = list(map(int, input().split()))
n = len(num_list)
例如,使用输入'1 2 3 4'
,您将得到以下结果:
print(n, num_list)
4 [1, 2, 3, 4]
明确输入数字的唯一目的是进行检查。这可以通过assert
语句来实现:
n, *num_list = map(int, input().split())
assert n == len(num_list), f'Check failed: {n} vs {len(num_list)} provided does not match'
答案 1 :(得分:0)
space_separated_numbers = input()
num_list = [int(x) for x in space_separated_numbers.split()]
诀窍是立即将整个输入作为字符串,然后自己分割。
编辑:如果仅考虑获取第一个数字,则只需获取第一个值即可。
space_separated_numbers = input()
num = space_separated_numbers.split()[0]
答案 2 :(得分:0)
也许您可以尝试将整个输入作为字符串处理。然后将它们转换为整数。在这种情况下,您也无需指定n的值。
stack-root: /Users/evanzamir/.stack
project-root: /Users/evanzamir/Code/Haskell/bartosz
config-location: /Users/evanzamir/Code/Haskell/bartosz/stack.yaml
bin-path: /Users/evanzamir/.stack/snapshots/x86_64-osx/lts-12.17/8.4.4/bin:/Users/evanzamir/.stack/compiler-tools/x86_64-osx/ghc-8.4.4/bin:/Users/evanzamir/.stack/programs/x86_64-osx/ghc-8.4.4/bin:/Library/Frameworks/Python.framework/Versions/3.6/bin:/usr/local/bin/stack:/usr/local/smlnj-110.77/bin:/opt/local/bin:/opt/local/sbin:/Library/Frameworks/Python.framework/Versions/3.6/bin:/Users/evanzamir/.rvm/gems/ruby-1.9.3-p327/bin:/Users/evanzamir/.rvm/gems/ruby-1.9.3-p327@global/bin:/Users/evanzamir/.rvm/rubies/ruby-1.9.3-p327/bin:/Users/evanzamir/.rvm/bin:/usr/local/smlnj-110.77/bin:/opt/local/bin:/opt/local/sbin:/usr/local/share/npm/bin/jitsu :/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/usr/local/sbin:/usr/local/share/npm/bin:/usr/local/sbin:/usr/local/share/npm/bin
programs: /Users/evanzamir/.stack/programs/x86_64-osx
compiler-exe: /Users/evanzamir/.stack/programs/x86_64-osx/ghc-8.4.4/bin/ghc
compiler-bin: /Users/evanzamir/.stack/programs/x86_64-osx/ghc-8.4.4/bin
compiler-tools-bin: /Users/evanzamir/.stack/compiler-tools/x86_64-osx/ghc-8.4.4/bin
local-bin: /Users/evanzamir/.local/bin
extra-include-dirs:
extra-library-dirs:
snapshot-pkg-db: /Users/evanzamir/.stack/snapshots/x86_64-osx/lts-12.17/8.4.4/pkgdb
local-pkg-db: /Users/evanzamir/Code/Haskell/bartosz/.stack-work/install/x86_64-osx/lts-12.17/8.4.4/pkgdb
global-pkg-db: /Users/evanzamir/.stack/programs/x86_64-osx/ghc-8.4.4/lib/ghc-8.4.4/package.conf.d
ghc-package-path: /Users/evanzamir/Code/Haskell/bartosz/.stack-work/install/x86_64-osx/lts-12.17/8.4.4/pkgdb:/Users/evanzamir/.stack/snapshots/x86_64-osx/lts-12.17/8.4.4/pkgdb:/Users/evanzamir/.stack/programs/x86_64-osx/ghc-8.4.4/lib/ghc-8.4.4/package.conf.d
snapshot-install-root: /Users/evanzamir/.stack/snapshots/x86_64-osx/lts-12.17/8.4.4
local-install-root: /Users/evanzamir/Code/Haskell/bartosz/.stack-work/install/x86_64-osx/lts-12.17/8.4.4
snapshot-doc-root: /Users/evanzamir/.stack/snapshots/x86_64-osx/lts-12.17/8.4.4/doc
local-doc-root: /Users/evanzamir/Code/Haskell/bartosz/.stack-work/install/x86_64-osx/lts-12.17/8.4.4/doc
dist-dir: .stack-work/dist/x86_64-osx/Cabal-2.2.0.1
local-hpc-root: /Users/evanzamir/Code/Haskell/bartosz/.stack-work/install/x86_64-osx/lts-12.17/8.4.4/hpc
local-bin-path: /Users/evanzamir/.local/bin
ghc-paths: /Users/evanzamir/.stack/programs/x86_64-osx
然后可以遍历列表来使用这些值。如果需要n的值,则只需获取列表的长度即可。
>>> x = [int(y) for y in input().split()]
1 2 3 4
>>> x
[1, 2, 3, 4]