我们经常需要将字符串的结尾截断一定量.正确的方法是my_string[:-i].
但是,如果你的代码可以i是0,这个tuncate整个字符串.我通常使用的解决方案是做的my_string[:len(my_string)-i],它完全正常.
虽然我总觉得有点难看.是否有更优雅的方式来实现这种行为?
我正在尝试使用 python 从 JSON 文件中删除一个元素。我已将字典转换为 python 字典,但到目前为止我一直失败。我正在使用的 JSON 文件上传到这里
如果键是'access_ip_v4'.
我不能使用 sed/grep 或任何其他正则表达式技术或字符串替换技术。在这方面,我有点被 Python 词典困住了。
这是我迄今为止的工作。
def dict_sweep(input_dic, k):
for key in input_dic.keys():
if key == k:
print("DIRECT DELETE")
del input_dic[key]
elif type(input_dic[key]) is dict:
print('DICT FOUND')
input_dic[key] = dict_sweep(input_dic[key], k)
elif isinstance(type(input_dic[key]), type([])):
print("LIST FOUND")
for i, v in enumerate(input_dic[key]):
if isinstance(input_dic[key][i], dict):
input_dic[key][i] = dict_sweep(v, k)
return input_dic
Run Code Online (Sandbox Code Playgroud)
我认为我的代码在遇到列表时会失败。从某种意义上说,失败,
clean_data = dict_sweep(data, 'access_ip_v4')
print(clean_data)
Run Code Online (Sandbox Code Playgroud)
将再次打印data而不是打印数据的清理版本。
我不太确定。我已阅读像其他一些问题,这却是无益的。有人可以在这里给我指点吗?
我一直在寻找一段时间,但我似乎找不到这个小问题的答案.
我有这个代码,应该在每三个单词之后拆分字符串:
import re
def splitTextToTriplet(Text):
x = re.split('^((?:\S+\s+){2}\S+).*',Text)
return x
print(splitTextToTriplet("Do you know how to sing"))
Run Code Online (Sandbox Code Playgroud)
目前输出如下:
['', 'Do you know', '']
Run Code Online (Sandbox Code Playgroud)
但我实际上期待这个输出:
['Do you know', 'how to sing']
Run Code Online (Sandbox Code Playgroud)
如果我打印(splitTextToTriplet("你知道怎么做")),它还应该输出:
['Do you know', 'how to']
Run Code Online (Sandbox Code Playgroud)
如何更改正则表达式以产生预期的输出?
在我的Python Shell中,删除__name__使其成为'builtins'.虽然,检查globals确认我没有__name__从某个全局变量中引用.
Python 3.6.2 (v3.6.2:5fd33b5, Jul 8 2017, 04:57:36) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> __name__
'__main__'
>>> del __name__
>>> __name__
'builtins'
>>> globals()[__name__]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'builtins'
Run Code Online (Sandbox Code Playgroud)
我的猜测是我们从一些关闭中使用它.这种行为是如何发生的?
我正在研究这个名为“lambert W 函数”的函数。2^x= 5x它通常在出现格式问题以及朗伯函数的属性在哪里xe^x=y时应用。虽然MATLAB可以有效地解决它,但我也想看看Python是否可以得到同样质量的结果。x=w(y)w
在适用于Python 3.7和更低版本的Itertools食谱中,提到了padnone “模拟内置map()函数的行为”:
def padnone(iterable):
"""Returns the sequence elements and then returns None indefinitely.
Useful for emulating the behavior of the built-in map() function.
"""
return chain(iterable, repeat(None))
Run Code Online (Sandbox Code Playgroud)
虽然我了解的有用性padnone,但是我看不到它如何以任何方式模拟 map。这是错误还是我错过了什么?
在python中,我可以将任何对象用作字典键(7,“ hello”,无):
例如
myDict = {}
x = someObject()
myDict[x] = "world"
Run Code Online (Sandbox Code Playgroud)
但是我不能使用未绑定的标识符。给出另一种语言的示例,其中标识符成为对象的属性(JavaScript)
x = { y : "hello" };
Run Code Online (Sandbox Code Playgroud)
为什么不支持将未绑定标识符用作密钥?在字典范围内或封闭范围内定义此绑定不是很简单吗?
我在Python中有以下代码,我不确定这里的for循环是如何工作的.
cur.execute("SELECT CUST_ID, COMPANY, LASTNAME, CITY, STATE FROM
mytable")
colnames = [desc[0] for desc in cur.description]
Run Code Online (Sandbox Code Playgroud)
这是如何工作的?
[desc[0] for desc in cur.description]
Run Code Online (Sandbox Code Playgroud)
什么是desc[0]?
我试图检查列表中的所有元素,看看它们是否符合"小于5"的条件.我想要做的是如果我的列表中没有数字小于5,我想打印一条声明"此列表中没有小于5的元素",否则只打印那些数字,而不是"此列表中没有小于5的元素." 也.
list = [100, 2, 1, 3000]
for x in list:
if int(x) < 5:
print(x)
else:
print("There are no elements in this list less than 5.")
Run Code Online (Sandbox Code Playgroud)
这会产生输出:
2
1
There are no elements in this list less than 5.
Run Code Online (Sandbox Code Playgroud)
如何摆脱输出的最后一行?
我有以下字典,我只需要打印带有奇数(1、3 ...)的字典。我将如何去做?
zen = {
1: 'Beautiful is better than ugly.',
2: 'Explicit is better than implicit.',
3: 'Simple is better than complex.',
4: 'Complex is better than complicated.',
5: 'Flat is better than nested.',
6: 'Sparse is better than dense.',
7: 'Readability counts.',
8: 'Special cases aren't special enough to the rules.',
9: 'Although practicality beats purity.',
10: 'Errors should never pass silently.'
}
Run Code Online (Sandbox Code Playgroud)
到目前为止,我有:
for c in zen:
print (c , zen[c][:])
Run Code Online (Sandbox Code Playgroud) python ×10
python-3.x ×5
coding-style ×1
dictionary ×1
for-else ×1
javascript ×1
json ×1
module ×1
optimization ×1
regex ×1
string ×1