- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
为什么str(list)
返回我们如何在控制台上看到列表?怎么样str(list)
工作? (对 str(list)
的 CPython 代码的任何引用)?
>>> x = ['abc', 'def', 'ghi']
>>> str(x)
"['abc', 'def', 'ghi']"
str(list)
取回原始列表我必须:
>>> from ast import literal_eval
>>> x = ['abc', 'def', 'ghi']
>>> str(x)
"['abc', 'def', 'ghi']"
>>> list(str(x))
['[', "'", 'a', 'b', 'c', "'", ',', ' ', "'", 'd', 'e', 'f', "'", ',', ' ', "'", 'g', 'h', 'i', "'", ']']
>>> literal_eval(str(x))
['abc', 'def', 'ghi']
list(str(list))
转str(list)
回到原来的名单?
>>> eval(str(x))
['abc', 'def', 'ghi']
literal_eval
同 eval
?是 eval
使用安全吗?
str(list(str(list))))
? 例如。
>>> x = 'abc'
>>> list(x)
['a', 'b', 'c']
>>> str(list(x))
"['a', 'b', 'c']"
>>> list(str(list(x)))
['[', "'", 'a', "'", ',', ' ', "'", 'b', "'", ',', ' ', "'", 'c', "'", ']']
>>> str(list(str(list(x))))
'[\'[\', "\'", \'a\', "\'", \',\', \' \', "\'", \'b\', "\'", \',\', \' \', "\'", \'c\', "\'", \']\']'
>>> list(str(list(str(list(x)))))
['[', "'", '[', "'", ',', ' ', '"', "'", '"', ',', ' ', "'", 'a', "'", ',', ' ', '"', "'", '"', ',', ' ', "'", ',', "'", ',', ' ', "'", ' ', "'", ',', ' ', '"', "'", '"', ',', ' ', "'", 'b', "'", ',', ' ', '"', "'", '"', ',', ' ', "'", ',', "'", ',', ' ', "'", ' ', "'", ',', ' ', '"', "'", '"', ',', ' ', "'", 'c', "'", ',', ' ', '"', "'", '"', ',', ' ', "'", ']', "'", ']']
>>> str(list(str(list(str(list(x))))))
'[\'[\', "\'", \'[\', "\'", \',\', \' \', \'"\', "\'", \'"\', \',\', \' \', "\'", \'a\', "\'", \',\', \' \', \'"\', "\'", \'"\', \',\', \' \', "\'", \',\', "\'", \',\', \' \', "\'", \' \', "\'", \',\', \' \', \'"\', "\'", \'"\', \',\', \' \', "\'", \'b\', "\'", \',\', \' \', \'"\', "\'", \'"\', \',\', \' \', "\'", \',\', "\'", \',\', \' \', "\'", \' \', "\'", \',\', \' \', \'"\', "\'", \'"\', \',\', \' \', "\'", \'c\', "\'", \',\', \' \', \'"\', "\'", \'"\', \',\', \' \', "\'", \']\', "\'", \']\']'
>>> list(str(list(str(list(str(list(x)))))))
['[', "'", '[', "'", ',', ' ', '"', "'", '"', ',', ' ', "'", '[', "'", ',', ' ', '"', "'", '"', ',', ' ', "'", ',', "'", ',', ' ', "'", ' ', "'", ',', ' ', "'", '"', "'", ',', ' ', '"', "'", '"', ',', ' ', "'", '"', "'", ',', ' ', "'", ',', "'", ',', ' ', "'", ' ', "'", ',', ' ', '"', "'", '"', ',', ' ', "'", 'a', "'", ',', ' ', '"', "'", '"', ',', ' ', "'", ',', "'", ',', ' ', "'", ' ', "'", ',', ' ', "'", '"', "'", ',', ' ', '"', "'", '"', ',', ' ', "'", '"', "'", ',', ' ', "'", ',', "'", ',', ' ', "'", ' ', "'", ',', ' ', '"', "'", '"', ',', ' ', "'", ',', "'", ',', ' ', '"', "'", '"', ',', ' ', "'", ',', "'", ',', ' ', "'", ' ', "'", ',', ' ', '"', "'", '"', ',', ' ', "'", ' ', "'", ',', ' ', '"', "'", '"', ',', ' ', "'", ',', "'", ',', ' ', "'", ' ', "'", ',', ' ', "'", '"', "'", ',', ' ', '"', "'", '"', ',', ' ', "'", '"', "'", ',', ' ', "'", ',', "'", ',', ' ', "'", ' ', "'", ',', ' ', '"', "'", '"', ',', ' ', "'", 'b', "'", ',', ' ', '"', "'", '"', ',', ' ', "'", ',', "'", ',', ' ', "'", ' ', "'", ',', ' ', "'", '"', "'", ',', ' ', '"', "'", '"', ',', ' ', "'", '"', "'", ',', ' ', "'", ',', "'", ',', ' ', "'", ' ', "'", ',', ' ', '"', "'", '"', ',', ' ', "'", ',', "'", ',', ' ', '"', "'", '"', ',', ' ', "'", ',', "'", ',', ' ', "'", ' ', "'", ',', ' ', '"', "'", '"', ',', ' ', "'", ' ', "'", ',', ' ', '"', "'", '"', ',', ' ', "'", ',', "'", ',', ' ', "'", ' ', "'", ',', ' ', "'", '"', "'", ',', ' ', '"', "'", '"', ',', ' ', "'", '"', "'", ',', ' ', "'", ',', "'", ',', ' ', "'", ' ', "'", ',', ' ', '"', "'", '"', ',', ' ', "'", 'c', "'", ',', ' ', '"', "'", '"', ',', ' ', "'", ',', "'", ',', ' ', "'", ' ', "'", ',', ' ', "'", '"', "'", ',', ' ', '"', "'", '"', ',', ' ', "'", '"', "'", ',', ' ', "'", ',', "'", ',', ' ', "'", ' ', "'", ',', ' ', '"', "'", '"', ',', ' ', "'", ']', "'", ',', ' ', '"', "'", '"', ',', ' ', "'", ']', "'", ']']
最佳答案
那么你总共有4个问题,让我们一一回答。
1. Why does
str(list)
returns how we seelist
on the console? How doesstr(list)
work?
str()
和
__str__()
?
str()
callable 只是返回对象的可打印形式!来自
docs
str(object)
does not always attempt to return a string that is acceptable toeval()
; its goal is to return a printable string.
__str__()
每当您调用
str()
时都会调用类中的函数在一个物体上。再次来自
documentation
object.__str__(self)
Called by the
str()
built-in function and by the
list
可调用?
list()
callable 是从作为参数传递的可迭代对象创建一个列表。再次来自
docs
Return a
list
whose items are the same and in the same order as iterable‘s items
str(list)
为您提供可打印的表格和
list(str(list))
将遍历字符串。即
list(str(list))
将为您提供所传递参数的可打印形式的单个字符的列表。
l = ['a','b']
(对于采用比您的问题中的示例更小的示例表示歉意)。
str(l)
,它返回列表
l
的可打印形式,即
"['a','b']"
.
"['a','b']"
是一个字符串,确实是一个可迭代的。现在当您拨打
list
在这方面,即
list("['a','b']")
你会得到一个奇怪的列表,比如
['[', "'", 'a', "'", ',', "'", 'b', "'", ']']
.为什么会发生这种情况?发生这种情况是因为字符串迭代其字符,您可以使用虚拟字符串进行测试,
>>> 'dummy'
'dummy'
>>> list('dummy')
['d', 'u', 'm', 'm', 'y']
list
时在一个字符串上,你会得到一个字符列表。再次注意,当您拨打
str()
时在
list('dummy')
,您将无法取回原始字符串
'dummy'
,所以你将不得不再次使用
join
!因此,调用相同的函数将
不是 让你找回你的原始对象!
str()
通过列表调用内置 __str__()
列表的方法?
str()
时内部会发生什么在名单上?
str()
在列表对象上,遵循的步骤是
repr()
每个列表元素。 [
在前面和另一个]
在列表的末尾。 /* Do repr() on each element. Note that this may mutate the list,
so must refetch the list size on each iteration. */ line (382)
/* Add "[]" decorations to the first and last items. */ line (398)
/* Paste them all together with ", " between. */ line (418)
repr()
?
repr()
打印所有对象的字符串表示。再次来自
documentation
Return a string containing a printable representation of an object.
For many types, this function makes an attempt to return a string that would yield an object with the same value when passed to
eval()
, otherwise the representation is a string enclosed in angle brackets that contains the name of the type of the object together with additional information often including the name and address of the object.
2. Why doesn't
list(str(list))
turns thestr(list)
back to the original list?
str(list)
实际上创建了
repr()
列表对象的表示。所以要在拨打
str
后取回名单在列表中,您实际上需要做
eval
在它上面而不是
list
称呼。
eval
is evil ,那么解决方法是什么?
literal_eval
ast.literal_eval
.这就引出了你的第三个问题,
3. Is
literal_eval()
the same aseval()
? Iseval()
safe to use?
ast.literal_eval()
安全
unlike
eval()
功能。文档本身提到它是安全的——
Safely evaluate an expression node or a string containing a Python literal or container display
str.split()
来完成
>>> x = ['abc', 'def', 'ghi']
>>> a = str(x)
>>> a[2:-2].split("', '")
['abc', 'def', 'ghi']
map
.
>>> x = [1,2,3]
>>> a =str(x)
>>> list(map(int,a[1:-1].split(', '))) # No need for list call in Py2
[1, 2, 3]
literal_eval
鉴于您知道列表的元素,这些是简单的技巧。如果它们本质上是异质的,例如
[1, "a", True]
那么您将不得不遍历拆分列表并发现元素类型,然后对其进行转换并将转换后的元素附加到最终列表中。
The
str.split
solution is very fragile and will break if the input contains e.g. strings that contain", "
, or tuples, or other lists, ... It is much better to useast.literal_eval
because that will deal with all the subtleties of the syntax.
4. Does the code break if you do
str(list(str(list))))
again and again?
list
时,输出会越来越长。的
str
然后再次获得它的可打印版本。限制仅是您的物理机的限制。 (很快就会达到字符串长度乘以 5 的每一步。)
关于python - str(list) 如何工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30109030/
你信吗?我有一个这样的循环(请原谅任何错误,我不得不大量编辑大量信息和变量名称,相信我它有效)。 ...旧示例已删除,请参见下面的代码... 如果我将那些中间的 str = "Blah\(odat.c
我正在做一个本地测试来比较 C# 中 String 和 StringBuilder 的 Replace 操作性能,但是对于 String 我使用了以下代码: String str = "String
我想知道为什么str += "A"和 str = str + "A"有不同的表现。 在实践中, string str = "cool" for(int i = 0; i approximately
我有一个类型列表 [("['106.52.116.101']", 1), ("['45.136.108.85']", 1)] 并想将其转换为 [('106.52.116.101', 1), ('45.
我有一个类型列表 [("['106.52.116.101']", 1), ("['45.136.108.85']", 1)] 并想将其转换为 [('106.52.116.101', 1), ('45.
我正在遍历 HashMap并通过一些本地变量中的模式匹配将值放入其中。 委托(delegate)者 fn lyrics_no_bottles(song_template:&mut String){
如果字符串(短语)中只有元音,它(对我而言)说True;否则说 False。我不明白为什么它总是返回 False,因为 (x >= x) 总是返回 True。我感谢任何人检查此查询的解决方案。 (st
我有代码以某种方式转换字符串引用,例如取第一个字母 trait Tr { fn trim_indent(self) -> Self; } impl Tr for &'a str { f
我正在学习指针,这是我的代码。我定义了一个指向 char(实际上是字符串)的指针 *str 和一个指向 int *a 的指针,它们的定义方式相同。我认为 str 和 a 都应该是一个地址,但是当我试图
为什么我会收到错误消息?我已经正确添加了类型,对吗? Invalid index type "str" for "Union[str, Dict[str, str]]"; expected type
你知道下面两个函数是否等价吗? function validate(str) { return ( ['null','','undefined'].indexOf(str) [v, valida
我正在解决这里的 Dataquest 问题:https://app.dataquest.io/m/293/data-cleaning-basics/5/removing-non-digit-chara
我有一个字符串列表,如下所示: ["A TB", "A-R TB", "B TB", "B-R TB", "C TB", "C-R TB"...] 但字符串的顺序是随机的。我如何编写一个将元素配对的函
我正在尝试将此函数从使用 split 改为使用 str.extract (正则表达式)。 def bull_lev(x): spl = x.rsplit(None, 2)[-2].strip(
给定这样的数据结构: [{'a':1, 'b': 2}, {'c':3 }, {'a':4, 'c':9}, {'d':0}, {'d': 0, 'b':6}] 目标是解析数据以产生: {'a': 2
给定这样的数据结构: [{'a':1, 'b': 2}, {'c':3 }, {'a':4, 'c':9}, {'d':0}, {'d': 0, 'b':6}] 目标是解析数据以产生: {'a': 2
s = 'someString' s = QTreeWidgetItem(s) print(s.text(0)) # 0 being 'column' 输出: 's' 如果我对另一
黑白有什么区别: function(char* str ) function(char* str[] ) function(char str[] ) 它们是如何被调用的(通过什么类型的string/c
我试过谷歌搜索但找不到准确的答案,所以请允许我尝试在这里提问。如果问题看起来不合适,请告诉我,我会删除它。 在 JS 中,您可以通过三种不同的方式编写特定的内置功能: 字符串长度 str.toStri
我有这段代码(我的 strlen 函数) size_t slen(const char *str) { size_t len = 0; while (*str) {
我是一名优秀的程序员,十分优秀!