当我们使用正则表达式匹配字符串中的中文中文时会发现字符串明明有目标串却不能匹配的情况
如
<span style="font-size:18px;">re.match(r'今天', str):</span>字符串确实存在‘今天‘,解决办法只需要将中文转换为Unicode就行了:
<span style="font-size:18px;">re.match(ur'今天', post_time):</span>需要注意使用sorted函数是,要得到排序的序列需要使用其返回值,即虽然经过sorted函数,但是原来的序列不保证有序:
dict = {'a': 1, 'c':3,'b': 2}
sorted_key_list = sorted(dict.items())
sorted_value_list = sorted(dict.items(),key=lambda item:item[1],reverse=True)
print('dict:',dict)
print('sorted_key_list:',sorted_key_list)
print('sorted_list:',sorted_value_list)输出:
dict: {'a': 1, 'c': 3, 'b': 2}
sorted_key_list: [('a', 1), ('b', 2), ('c', 3)]
sorted_list: [('c', 3), ('b', 2), ('a', 1)]试试下面的语句把!
set_words = list(set([word for line in data.split('\n') for word in line]))int_to_vocab = {idx: word for idx, word in enumerate(set_words)}
vocab_to_int = {word: idx for idx, word in int_to_vocab.items()}删除元素,不能使用切片操作,必须使用list的pop()函数(默认删除最后一个,也可以指定index)或者del函数(指定index)。如我们要在二维list中每一行最多只能有两个元素,可以使用如下代码操作:
list = [['1','2','3','4'],['4','5','6','7'],['3']]
for tmp in list:
if len(tmp)>2:
for i in range(2,len(tmp)):
tmp.pop()
print(list)[['1', '2'], ['4', '5'], ['3']]
反之,若使用切片:
list = [['1','2','3','4'],['4','5','6','7'],['3']]
for tmp in list:
if len(tmp)>2:
tmp = tmp[:2]
print(list)输出的还是原来的值。
6. ubuntu修改python为python2
rm /usr/bin/python
ln -s /usr/bin/python2 /usr/bin/python
7. No module named 'tensorflow.python'
错误:
from tensorflow.python.platform import gfile
ImportError: No module named 'tensorflow.python'原因:想要更改tensorflow代码,python3环境改成了python2
解决方法: 改成python3,然后卸载使用:
pip3 install --upgrade tensorflow重新安装tensorflow
如:
print(lambda x: x*x)
就相当于:def square(x):
return x*x
print(square(x))在tensorflow中的Seq2Seq的tutorial中,
model_with_buckets()函数中有一个lamda表达式:
lambda x, y: seq2seq_f(x, y, True)x,y传入作为seq2seq的函数的参数,然后调用执行seq2seq函数。
在使用pandas写入文件的时候,列表数据默认是以列表的的形式写入的
如:['a','b','c']但是当我们读出来的时候却是以字符串读取的,可以用split函数再次生成新的list,但是操作未免过于繁琐,设个时候假设我们的字符串的内容为:list_str = '['a','b','c']'为了避免再次生成list我们可以使用python里面提供的eval函数,即:list = eval(list_str)这个时候返回的就是一个list