有没有一种简单的方法可以在python列表中输出列表?像这样[[0,1],[2,3]]

时间:2019-06-24 12:36:41

标签: python string slice nested-lists

我解决了以下练习的逻辑部分,但找不到用于以所需格式输出的简单解决方案。对我来说,它看起来像列表中的列表。

问题描述:

  

编写一个程序,该程序将使用数字字符串并为您提供所有   该字符串中长度为n的可能连续切片。

     

如果n大于字符串的长度,则会引发错误。

     

示例例如,字符串“ 01234”具有以下两位数字   片:

     

[[0,1],[1,2],[2,3],[3,4]]

     

同一字符串包含以下4位数字切片:

     

[[0,1,2,3],[1,2,3,4]]

这是我的代码,工作正常,除了返回答案的方式不同,因此无法提交。

digits = "01234"
n = 4
result = [None]*(len(digits)-n+1) #create a list with required size
for x in range(0, len(digits)-n+1):
    result[x] = digits[x:n]
    n += 1
print(result)

此代码显示['0123', '1234'],这是正确的答案,但我希望使用以下格式:[[0, 1, 2, 3], [1, 2, 3, 4]]

(edit)添加此行解决了mkrieger1建议的问题,谢谢 这是最终代码: def series_slices(digits, n): digits = [int(x) for x in digits] result = [] for x in range(0, len(digits)-n+1): result.append(list(digits[x:n])) n += 1 return result

3 个答案:

答案 0 :(得分:1)

只需在您的字符串上使用list

list('0123')

返回

['0', '1', '2', '3']

并将其转换为整数(感谢mkrieger,我没有足够仔细地阅读问题:

您可以使用map,它将迭代字符串的字符并为每个字符返回一个整数,然后从地图对象中列出一个列表:

list(map(int, '0123'))
# [0, 1, 2, 3]

或使用列表理解:

[int(digit) for digit in '0123'] 
# [0, 1, 2, 3]

更一般地,将list应用于任何可迭代项将返回其项列表。

答案 1 :(得分:0)

您可以通过列表理解来做到这一点。第一层通过大小,第二层获取该大小的切片:

C:\vscode>yarn
yarn install v1.16.0
$ node build/npm/preinstall.js
[1/4] �?  Resolving packages...
[2/4] �?  Fetching packages...
info vscode-fsevents@0.3.10: The platform "win32" is incompatible with this module.
info "vscode-fsevents@0.3.10" is an optional dependency and failed compatibility check. Excluding it from installation.
info fsevents@1.2.7: The platform "win32" is incompatible with this module.
info "fsevents@1.2.7" is an optional dependency and failed compatibility check. Excluding it from installation.
info fsevents@1.2.4: The platform "win32" is incompatible with this module.
info "fsevents@1.2.4" is an optional dependency and failed compatibility check. Excluding it from installation.
[3/4] �?  Linking dependencies...
[4/4] �?  Building fresh packages...
[1/17] ⠄ keytar
[2/17] ⠄ native-is-elevated
[3/17] ⠄ native-keymap
[4/17] ⡀ native-watchdog
error C:\vscode\node_modules\native-watchdog: Command failed.
Exit code: 7
Command: node-gyp rebuild
Arguments:
Directory: C:\vscode\node_modules\native-watchdog
Output:
C:\vscode\node_modules\native-watchdog>if not defined npm_config_node_gyp (node "C:\Program Files\nodejs\node_modules\npm\bin\node-gyp-bin\\..\..\node_modules\node-gyp\bin\node-gyp.js" rebuild )  else (node "" rebuild )
gyp info it worked if it ends with ok
gyp info using node-gyp@3.8.0
gyp info using node@10.15.3 | win32 | x64
gyp info spawn C:\Python27\python.EXE
gyp info spawn args [ 'C:\\Program Files\\nodejs\\node_modules\\npm\\node_modules\\node-gyp\\gyp\\gyp_main.py',
gyp info spawn args   'binding.gyp',
gyp info spawn args   '-f',
gyp info spawn args   'msvs',
gyp info spawn args   '-G',
gyp info spawn args   'msvs_version=2015',
gyp info spawn args   '-I',
gyp info spawn args   'C:\\vscode\\node_modules\\native-watchdog\\build\\config.gypi',
gyp info spawn args   '-I',
gyp info spawn args   'C:\\Program Files\\nodejs\\node_modules\\npm\\node_modules\\node-gyp\\addon.gypi',
gyp info spawn args   '-I',
gyp info spawn args   'C:\\Users\\Syler\\.node-gyp\\4.2.5\\include\\node\\common.gypi',
gyp info spawn args   '-Dlibrary=shared_library',
gyp info spawn args   '-Dvisibility=default',
gyp info spawn args   '-Dnode_root_dir=C:\\Users\\Syler\\.node-gyp\\4.2.5',
gyp info spawn args   '-Dnode_gyp_dir=C:\\Program Files\\nodejs\\node_modules\\npm\\node_modules\\node-gyp',
gyp info spawn args   '-Dnode_lib_file=C:\\Users\\Syler\\.node-gyp\\4.2.5\\<(target_arch)\\node.lib',
gyp info spawn args   '-Dmodule_root_dir=C:\\vscode\\node_modules\\native-watchdog',
gyp info spawn args   '-Dnode_engine=v8',
gyp info spawn args   '--depth=.',
gyp info spawn args   '--no-parallel',
gyp info spawn args   '--generator-output',
gyp info spawn args   'C:\\vscode\\node_modules\\native-watchdog\\build',
gyp info spawn args   '-Goutput_dir=.' ]
gyp info spawn C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\15.0\Bin\MSBuild.exe
gyp info spawn args [ 'build/binding.sln',
gyp info spawn args   '/clp:Verbosity=minimal',
gyp info spawn args   '/nologo',
gyp info spawn args   '/p:Configuration=Release;Platform=x64' ]
gyp ERR! UNCAUGHT EXCEPTION
gyp ERR! stack Error: spawn C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\15.0\Bin\MSBuild.exe ENOENT
gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:240:19)
gyp ERR! stack     at onErrorNT (internal/child_process.js:415:16)
gyp ERR! stack     at process._tickCallback (internal/process/next_tick.js:63:19)
gyp ERR! System Windows_NT 10.0.17763
gyp ERR! command "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\node_modules\\node-gyp\\bin\\node-gyp.js" "rebuild" 
gyp ERR! cwd C:\vscode\node_modules\native-watchdog
gyp ERR! node -v v10.15.3
gyp ERR! node-gyp -v v3.8.0

答案 2 :(得分:-1)

将代码更改为 result = [] 和第3行和第5行的result.append(list(digits[x:n]))