有没有更简单的方法将列表中的字符串项连接成一个字符串?
我可以使用该str.join()功能加入列表中的项目吗?
例如,这是输入['this','is','a','sentence'],这是所需的输出this-is-a-sentence
sentence = ['this','is','a','sentence']
sent_str = ""
for i in sentence:
sent_str += str(i) + "-"
sent_str = sent_str[:-1]
print sent_str
Run Code Online (Sandbox Code Playgroud) 字典在Python 3.6中排序(至少在CPython实现下),与之前的版本不同.这似乎是一个重大变化,但它只是文档中的一小段.它被描述为CPython实现细节而不是语言特性,但也暗示这可能成为未来的标准.
在保留元素顺序的同时,新字典实现如何比旧字典实现更好?
以下是文档中的文字:
dict()现在使用PyPy开创的"紧凑"表示.与Python 3.5相比,新dict()的内存使用量减少了20%到25%.PEP 468(在函数中保留**kwargs的顺序.)由此实现.这个新实现的顺序保留方面被认为是一个实现细节,不应该依赖(这可能会在未来发生变化,但是在更改语言规范之前,希望在几种版本的语言中使用这个新的dict实现为所有当前和未来的Python实现强制命令保留语义;这也有助于保持与随机迭代顺序仍然有效的语言的旧版本的向后兼容性,例如Python 3.5).(由INADA Naoki在issue 27350中提供.最初由Raymond Hettinger提出的想法.)
2017年12月更新:Python 3.7 保证了dict保留插入顺序
我需要更换一些字符如下:&- > \&,#- > \#,...
我编码如下,但我想应该有更好的方法.任何提示?
strs = strs.replace('&', '\&')
strs = strs.replace('#', '\#')
...
Run Code Online (Sandbox Code Playgroud) 我尝试做一个简单的字符串替换,但我不知道为什么它似乎不起作用:
X = "hello world"
X.replace("hello", "goodbye")
Run Code Online (Sandbox Code Playgroud)
我想将单词更改hello为goodbye,因此它应该将字符串更改"hello world"为"goodbye world".但X仍然存在"hello world".为什么我的代码不起作用?
我有字符串,其中包含一个数字,我试图用他们的单词符号替换这个数字(即3 - > 3).我有一个功能,这样做.现在的问题是找到字符串中的数字,同时保持字符串的其余部分完好无损.为此,我选择使用该re.sub函数,它可以接受"可调用".但是,传递给它的对象是内部的_sre.SRE_Match,我不知道如何处理它.我的函数接受一个数字或其字符串表示.
我应该如何编写一些辅助函数,可以用来桥接re.sub调用我的函数进行所需的处理?或者,有没有更好的方法来做我想要的?
我必须在地址字段中用NS替换北,南等.
如果我有
list = {'NORTH':'N','SOUTH':'S','EAST':'E','WEST':'W'}
address = "123 north anywhere street"
Run Code Online (Sandbox Code Playgroud)
我可以迭代我的字典值来替换我的地址字段吗?
for dir in list[]:
address.upper().replace(key,value)
Run Code Online (Sandbox Code Playgroud)
我知道我甚至不是很亲密!但是如果您可以使用这样的字典值,那么任何输入都会受到赞赏.
我有这个django模板,我用它来生成LaTeX文件
\documentclass[11pt]{report}
\begin{document}
\begin{table}
\centering
\begin{tabular}{lcr}
\hline
{% for col in head %}
\textbf{ {{col}} }
{% if not forloop.last %}
&
{% endif %}
{% endfor %}
\\
\hline
{% for row in table %}
{% for cell in row %}
{% if not forloop.last %}
&
{% endif %}
{% endfor %}
\\
{% endfor %}
\hline
\end{tabular}
\caption{Simple Phonebook}
\label{tab:phonebook}
\end{table}
\end{document}
Run Code Online (Sandbox Code Playgroud)
但我的列数非常大,因此它们可以包含任何特殊字符.生成pdf文件时出错.
如何逃避所有列中的所有文本?
我有这个字符串
message = '10100010011'
Run Code Online (Sandbox Code Playgroud)
和这本字典
codes = {97: '1', 98: '01', 107: '001', 114: '000'}
Run Code Online (Sandbox Code Playgroud)
我需要用字典替换原始消息到这样的东西
[97, 98, 114, 97, 107, 97]
Run Code Online (Sandbox Code Playgroud)
我尝试了自己的方式,但是当我使用一些非常大的字符串时,它真的很慢.有没有比这更快的方法呢?
codes = dict(zip(codes.values(), codes.keys()))
decoded_mess = []
pom = ""
for i in message:
pom += i
if pom in codes:
decoded_mess.append(codes[pom])
pom = ""
Run Code Online (Sandbox Code Playgroud)
我在这里看到了答案使用替换字典替换字符串的最简单方法?我试过了,但那对我不起作用.也许是因为他们正在处理整个单词,但我有一长串的1和0.
我创建了一个字典,用来将各种单词带入其基本形式。
dictionary = {'sunny': 'sun', 'banking': 'bank'}
def stemmingWords(sentence, dictionary):
for word in sentence.split():
temp = []
if word in dictionary:
word = dictionary[word]
temp.append(word)
sentence = ' '.join(temp)
return(sentence)
Run Code Online (Sandbox Code Playgroud)
现在,如果打印单独的单词,它似乎可以工作。但是,当我插入一个完整的句子并想要此句子的更新版本时,似乎出现了问题。例如,如果我这样做:
sentence = "the sun us shining"
new_sentence = stemmingWords(sentence, dictionary)
print(new_sentence)
Run Code Online (Sandbox Code Playgroud)
给我“闪闪发光”。当我在寻找“阳光照耀的阳光”时。
对这里出什么问题有任何想法吗?
我有以下句子
a = "you don't need a dog"
Run Code Online (Sandbox Code Playgroud)
和一本字典
dict = {"don't": "do not" }
Run Code Online (Sandbox Code Playgroud)
但我不能使用字典来使用下面的代码映射句子中的单词:
''.join(str(dict.get(word, word)) for word in a)
Run Code Online (Sandbox Code Playgroud)
输出:
"you don't need a dog"
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
我有个字符的几个文本文件,其中有区别标记,例如è,á,ô等等。我想将这些字符替换e,a,o,等
我怎样才能在 Python 中实现这一点?感谢帮助!
python ×12
string ×5
dictionary ×4
python-3.x ×3
replace ×2
diacritics ×1
django ×1
join ×1
list ×1
pdflatex ×1
python-3.6 ×1
regex ×1
str-replace ×1
substitution ×1
text ×1