首页 \ 问答 \ python如何无限遍历字典中的value,在不知道字典里面有几层字典的时候

python如何无限遍历字典中的value,在不知道字典里面有几层字典的时候

python如何无限遍历字典中的value,在不知道字典里面有几层字典或者列表的时候,如何最快的遍历
更新时间:2021-10-04 09:10

最满意答案

os 包括os.path
import os 之后要 os.path 来调用
from os import path 后 直接用path来调用就可以了

其他回答

请参考请文档:
os doc:
This exports:
  - all functions from posix, nt, os2, or ce, e.g. unlink, stat, etc.
  - os.path is one of the modules posixpath, or ntpath
  - os.name is 'posix', 'nt', 'os2', 'ce' or 'riscos'
  - os.curdir is a string representing the current directory ('.' or ':')
  - os.pardir is a string representing the parent directory ('..' or '::')
  - os.sep is the (or a most common) pathname separator ('/' or ':' or '\\')
  - os.extsep is the extension separator ('.' or '/')
  - os.altsep is the alternate pathname separator (None or '/')
  - os.pathsep is the component separator used in $PATH etc
  - os.linesep is the line separator in text files ('\r' or '\n' or '\r\n')
  - os.defpath is the default search path for executables
  - os.devnull is the file path of the null device ('/dev/null', etc.)

Programs that import and use 'os' stand a better chance of being
portable between different platforms.  Of course, they must then
only use functions that are defined by all platforms (e.g., unlink
and opendir), and leave all pathname manipulation to os.path
(e.g., split and join).

path 的文档:

Common pathname manipulations, WindowsNT/95 version.

Instead of importing this module directly, import os and refer to this
module as os.path.

如果想有差别你可以像下面这样导入,就不行使用os引用path,直接使用path即可
from os import path
首先,我对 os.exit 这个用法表示惊讶,于是亲自尝试了一下:
>>> print sys.version
2.7.3 (default, jul 24 2013, 00:50:10) 
[gcc 4.2.1 compatible apple llvm 4.2 (clang-425.0.28)]
>>> import os
>>> os.exit
traceback (most recent call last):
  file "

 ", line 1, in 
 
  
attributeerror: 'module' object has no attribute 'exit'
>>> 
wtf?os.exit 根本就不存在呀!如果这个用法真的存在,请在评论区里指正。谢谢
比如说,os.path.split、os.system等,这些都是在和os交互。值得注意的,os.fork只在*nix上可用,这也说明了「os模块负责与操作系统的交互」。
至于sys呢,sys.exit让解释器停止你的程序、sys.version获得python解释器(而非操作系统)的版本号、sys.argv获得你的程序的参数……所以说sys是与解释器高度相关的。
综上,题主你“os连接操作系统、sys连接解释器”的说法很对。我不知道题主哪里不能理解它们的区别,你可以在评论区说出来。

  【本段存在重大技术错误已经宣布召回】至于题主说的path,我个人理解是这样的:
你说path和os肯定有关系吧,path和python解释器也有关系呀:你的程序能import哪些模块就取决于path。guido想了半天,实在不知道应该把path放在os里还是sys里,于是干脆在os和sys里都放了一个path。
刚意识到自己脑残了,os.path和sys.path明明是完全不同的两个东西啊!sys.path是人民群众喜闻乐见的path环境变量,os.path是一个module,提供split、join、basename等“处理目录、文件名”的工具。
 

相关问答

更多
  • os 包括os.path import os 之后要 os.path 来调用 from os import path 后 直接用path来调用就可以了
  • 用os.extsep分割。 >>> import os >>> 'filename.ext1.ext2'.split(os.extsep) ['filename', 'ext1', 'ext2'] 如果你想要在第一个点之后的一切: >>> 'filename.ext1.ext2'.split(os.extsep, 1) ['filename', 'ext1.ext2'] 如果您使用的路径包含可能包含点的目录: >>> def my_splitext(path): ... """splitext ...
  • 从技术上讲,os.path将指向“项目/项目”,因为这是settings.py所在的位置。 尝试移动“模板”目录。 它对我有用! 只需确保将templateDir更改为以下内容: templateDir = os.path.dirname(__file__) TEMPLATE_DIRS = ( os.path.join(templateDir, "templates"), One of the small difference between Windows & *inx sys ...
  • 你并没有逃避你的反斜杠。 请使用\\而不是\ ,或使用原始字符串,例如: a = r"D:\temp\temp.txt" 在你的非转义字符串中, \t被解释为制表符。 Using a.encode('string-escape') seems preferable to other solutions because i) it can be done inline and ii) it doesn't add extra single/double-quotes.
  • os.path以有趣的方式工作。 看起来os应该是一个带有子模块path的包,但实际上os是一个正常的模块,用sys.modules来注入os.path 。 发生这种情况: 当Python启动时,它将一堆模块加载到sys.modules 。 它们没有绑定到脚本中的任何名称,但是您可以以某种方式导入已经创建的模块。 sys.modules是缓存模块的dict。 导入模块时,如果已经在某个地方导入了模块,它会将实例存储在sys.modules 。 os是在Python启动时加载的模块之一。 它将其path属性分 ...
  • os.path.abspath(os.path.join(os.path.dirname( __file__ ), '..', 'templates')) 至于模板文件夹应该去哪里,我不知道,因为Django 1.4刚刚出来,我还没有看过它。 你应该在SE方面提出另一个问题来解决这个问题。 您也可以使用normpath来清理路径,而不是abspath 。 然而,在这种情况下,Django希望绝对路径而不是相对路径。 对于跨平台兼容性,请使用os.pardir而不是'..' 。 os.path.abspat ...
  • 因为你是从cygwin运行的,所以这些路径不是原生的,但是为了能够正常工作而改变了cygwin(MSYS也是如此)。 所以作为一个副作用,当python请求当前文件时,它会返回斜杠。 但是anaconda仍然是一个原生的Windows发行版,它解释了你在加入字符串时得到\ (native os.sep )。 要使用本机分隔符( \ here)获取__file__路径,只需执行以下操作: os.path.normpath(__file__) Since you're running from cygwin, ...
  • 几个笔记: #!/usr/bin/env python # import mks_function .. you won't need this ... from mks_function import mks_create_sandbox import os, datetime # import time, sys .. these aren't used in this snippet # import os.path .. just refer to os.path, since o ...
  • listdir仅返回文件名列表,没有基本路径。 将这些文件名与x合并以获取完整路径。 #! /usr/bin/python import os os.system('clear') x=raw_input('enter a path ') y=os.listdir(x) k=0 m=0 for a in y: p = os.path.join(x, a) # <-- here if os.path.isfile(p): k=k+1 elif os.path.isdi ...
  • 根据您使用的平台, os.path实际上是: ntpath.py于Windows的ntpath.py 用于Linux的posixpath.py macpath.py于Mac OSX的macpath.py os模块在文件顶部说明: r"""OS routines for NT or Posix depending on what system we're on. This exports: - all functions from posix, nt, os2, or ce, e.g. unlink, ...

相关文章

更多

最新问答

更多
  • h2元素推动其他h2和div。(h2 element pushing other h2 and div down. two divs, two headers, and they're wrapped within a parent div)
  • 创建一个功能(Create a function)
  • 我投了份简历,是电脑编程方面的学徒,面试时说要培训三个月,前面
  • PDO语句不显示获取的结果(PDOstatement not displaying fetched results)
  • Qt冻结循环的原因?(Qt freezing cause of the loop?)
  • TableView重复youtube-api结果(TableView Repeating youtube-api result)
  • 如何使用自由职业者帐户登录我的php网站?(How can I login into my php website using freelancer account? [closed])
  • SQL Server 2014版本支持的最大数据库数(Maximum number of databases supported by SQL Server 2014 editions)
  • 我如何获得DynamicJasper 3.1.2(或更高版本)的Maven仓库?(How do I get the maven repository for DynamicJasper 3.1.2 (or higher)?)
  • 以编程方式创建UITableView(Creating a UITableView Programmatically)
  • 如何打破按钮上的生命周期循环(How to break do-while loop on button)
  • C#使用EF访问MVC上的部分类的自定义属性(C# access custom attributes of a partial class on MVC with EF)
  • 如何获得facebook app的publish_stream权限?(How to get publish_stream permissions for facebook app?)
  • 如何防止调用冗余函数的postgres视图(how to prevent postgres views calling redundant functions)
  • Sql Server在欧洲获取当前日期时间(Sql Server get current date time in Europe)
  • 设置kotlin扩展名(Setting a kotlin extension)
  • 如何并排放置两个元件?(How to position two elements side by side?)
  • 如何在vim中启用python3?(How to enable python3 in vim?)
  • 在MySQL和/或多列中使用多个表用于Rails应用程序(Using multiple tables in MySQL and/or multiple columns for a Rails application)
  • 如何隐藏谷歌地图上的登录按钮?(How to hide the Sign in button from Google maps?)
  • Mysql左连接旋转90°表(Mysql Left join rotate 90° table)
  • dedecms如何安装?
  • 在哪儿学计算机最好?
  • 学php哪个的书 最好,本人菜鸟
  • 触摸时不要突出显示表格视图行(Do not highlight table view row when touched)
  • 如何覆盖错误堆栈getter(How to override Error stack getter)
  • 带有ImageMagick和许多图像的GIF动画(GIF animation with ImageMagick and many images)
  • USSD INTERFACE - > java web应用程序通信(USSD INTERFACE -> java web app communication)
  • 电脑高中毕业学习去哪里培训
  • 正则表达式验证SMTP响应(Regex to validate SMTP Responses)