Python list .extend method breaks string into characters

This question already has an answer here: Difference between append vs. extend list methods in Python 22 answers list.extend extends the list with each of the items in the iterable you pass. A string is an iterable of characters, so it appends the characters. Easier to use list.append here.

Python列表.extend方法将字符串分解为字符

这个问题在这里已经有了答案: Python中的append和extend列表方法之间的区别22回答 list.extend用你传递的迭代中的每个项目扩展列表。 一个字符串是一个可迭代的字符,所以它附加了字符。 在这里更容易使用list.append 。

Combine the list returned by `re.findall()`

This question already has an answer here: Difference between append vs. extend list methods in Python 22 answers 尝试reduce(sum, line) : def extractDollar(line): global mainList temp=[] #lowercasing all the string line=line.lower() #storing all word starting with $ in a line in temp #then adding that to existing list mainList #to for

合并由`re.findall()`返回的列表

这个问题在这里已经有了答案: Python中的append和extend列表方法之间的区别22回答 尝试reduce(sum, line) : def extractDollar(line): global mainList temp=[] #lowercasing all the string line=line.lower() #storing all word starting with $ in a line in temp #then adding that to existing list mainList #to form a single list and removing empty

Add a string to a Python list

This question already has an answer here: Difference between append vs. extend list methods in Python 22 answers Use append : >>> movie_list = [] >>> movie_list.append('Espas en Hollywood') >>> movie_list ['Espas en Hollywood'] If you want to use extend : movie_list.append(['Espas en Hollywood'])

将一个字符串添加到Python列表中

这个问题在这里已经有了答案: Python中的append和extend列表方法之间的区别22回答 使用append : >>> movie_list = [] >>> movie_list.append('Espas en Hollywood') >>> movie_list ['Espas en Hollywood'] 如果你想使用extend : movie_list.append(['Espas en Hollywood'])

python store string to array not characters

This question already has an answer here: Difference between append vs. extend list methods in Python 22 answers a.extend(b) is for extending list a by concatenating another sequence b onto it. When b is a string, and you force it to be interpreted as a sequence, it is interpreted as a sequence of individual characters. A simple example of this is: >>> b = 'Hello' >>> lis

python存储字符串数组不是字符

这个问题在这里已经有了答案: Python中的append和extend列表方法之间的区别22回答 a.extend(b)用于通过连接另一个序列 b来扩展列表a 。 当b是一个字符串,并强制它被解释为一个序列时,它被解释为一个单独的字符序列。 一个简单的例子是: >>> b = 'Hello' >>> list(b) ['H', 'e', 'l', 'l', 'o'] 相反,你显然希望做a.append(b)即插入整个字符串b截至年底单个新项目a 。

Compare dictionaries ignoring specific keys

How can I test if two dictionaries are equal while taking some keys out of consideration. For example, equal_dicts( {'foo':1, 'bar':2, 'x':55, 'y': 77 }, {'foo':1, 'bar':2, 'x':66, 'z': 88 }, ignore_keys=('x', 'y', 'z') ) should return True. UPD: I'm looking for an efficient, fast solution. UPD2. I ended up with this code, which appears to be the fastest: def equal_dicts_

比较字典忽略特定的键

如何测试两个字典是否相同,同时考虑一些密钥。 例如, equal_dicts( {'foo':1, 'bar':2, 'x':55, 'y': 77 }, {'foo':1, 'bar':2, 'x':66, 'z': 88 }, ignore_keys=('x', 'y', 'z') ) 应该返回True。 UPD:我正在寻找一种高效,快速的解决方案。 UPD2。 我结束了这个代码,这看起来是最快的: def equal_dicts_1(a, b, ignore_keys): ka = set(a).difference(ignore_keys) kb = set(b).difference(i

How can I sort a dictionary by key?

What would be a nice way to go from {2:3, 1:89, 4:5, 3:0} to {1:89, 2:3, 3:0, 4:5} ? I checked some posts but they all use the "sorted" operator that returns tuples. Standard Python dictionaries are unordered. Even if you sorted the (key,value) pairs, you wouldn't be able to store them in a dict in a way that would preserve the ordering. The easiest way is to use OrderedDict

我怎样才能按键排序字典?

从{2:3, 1:89, 4:5, 3:0}到{1:89, 2:3, 3:0, 4:5}什么? 我检查了一些帖子,但他们都使用返回元组的“排序”运算符。 标准的Python词典是无序的。 即使您对(键,值)对进行了排序,您也无法以保留排序的方式将它们存储在dict中。 最简单的方法是使用OrderedDict ,它记住元素的插入顺序: In [1]: import collections In [2]: d = {2:3, 1:89, 4:5, 3:0} In [3]: od = collections.OrderedDict(sorted(d.items())) In [

Iterating over a stack (reverse list), is there an isempty() method?

What's the best way to iterate over a stack in Python? a = [1,2,3,4] while (len(a) > 0) print a.pop() # prints 4, 3, 2, 1 in sequence I couldn't find an isempty method, and checking the length each time seems wrong somehow. 通常的容器惯例是,它们是真的,而不是空的,空的时候是假的,所以你可以这样做: while a: print a.pop() Use the list as a boolean condition which evaluates t

迭代堆栈(反向列表),是否有一个isempty()方法?

在Python中迭代堆栈的最佳方式是什么? a = [1,2,3,4] while (len(a) > 0) print a.pop() # prints 4, 3, 2, 1 in sequence 我找不到一个isempty方法,每次查看长度似乎都是错误的。 通常的容器惯例是,它们是真的,而不是空的,空的时候是假的,所以你可以这样做: while a: print a.pop() 使用该列表作为布尔条件,仅当列表为空时才计算为False : >>> while a: ... print a.pop() ... 4 3 2 1

Why is there no explicit emptyness check (for example `is Empty`) in Python

The Zen of Python says "Explicit is better than implicit". Yet the "pythonic" way to check for emptiness is using implicit booleaness: if not some_sequence: some_sequence.fill_sequence() This will be true if some_sequence is an empty sequence, but also if it is None or 0 . Compare with a theoretical explicit emptiness check: if some_sequence is Empty: some_sequen

为什么在Python中没有明确的空白检查(例如`Empty')

Python的禅说:“显式比隐式更好”。 然而,检查空虚的“pythonic”方法是使用隐式布尔型: if not some_sequence: some_sequence.fill_sequence() 如果some_sequence是一个空序列,但如果它是None或0 ,则这将成立。 与理论显式空白检查相比较: if some_sequence is Empty: some_sequence.fill_sequence() 用一些不愉快的选择变量名称来检查空虚的隐含的boolea变得更加混乱: if saved: mess_up() 与之比较:

Check if list is empty without using the `not` command

How can I find out if a list is empty without using the not command? Here is what I tried: if list3[0] == []: print "No matches found" else: print list3 I am very much a beginner so excuse me if I do dumb mistakes. In order of preference: # Good if not list3: # Okay if len(list3) == 0: # Ugly if list3 == []: # Silly try: next(iter(list3)) # list has elements except

检查列表是否为空而不使用`not`命令

如何在不使用not命令的情况下查看列表是否为空? 这是我试过的: if list3[0] == []: print "No matches found" else: print list3 如果我犯了愚蠢的错误,我是一个初学者,所以很抱歉。 按照优先顺序: # Good if not list3: # Okay if len(list3) == 0: # Ugly if list3 == []: # Silly try: next(iter(list3)) # list has elements except StopIteration: # list is empty 如果你同时拥

Calculating arithmetic mean (average) in Python

Python中是否有内置或标准库方法来计算数字列表的算术平均值(平均值)? I am not aware of anything in the standard library. However, you could use something like: def mean(numbers): return float(sum(numbers)) / max(len(numbers), 1) >>> mean([1,2,3,4]) 2.5 >>> mean([]) 0.0 In numpy, there's numpy.mean() . NumPy has a numpy.mean which is an arithmetic mean. Usage is a

在Python中计算算术平均值(平均值)

Python中是否有内置或标准库方法来计算数字列表的算术平均值(平均值)? 我不知道标准库中的任何内容。 不过,你可以使用如下的东西: def mean(numbers): return float(sum(numbers)) / max(len(numbers), 1) >>> mean([1,2,3,4]) 2.5 >>> mean([]) 0.0 在numpy中,有numpy.mean() 。 NumPy有一个numpy.mean这是一个算术平均值。 用法如此简单: >>> import numpy >>> a = [1