Re:Python Cheat Sheet
by mercuryrising on Tuesday September 04, 2012 @07:59PM
(#4477151)
> This cheat sheet sucks. Some examples:
Make a better one and get it to the front page of HN.
Whether or not I make one has no bearing on this one's quality.
Re:Python Cheat Sheet
by TazeTSchnitzel on Tuesday September 04, 2012 @02:44PM
(#4475914)
Looks like it exists, and works (at least for strings):
>>> 'test'.count('e')
1
>>> 'test'.count('')
5
>>> 'test'.count('tes')
1
>>> 'test'.count('t')
2
>>> 'test'.count('apple')
0
Seems to be the equivalent of `in` on a string but with a count.It does exist for strings, but the list contains both `.count('string2')` and `.count(object1)`.
Re:Python Cheat Sheet
by TazeTSchnitzel on Tuesday September 04, 2012 @03:13PM
(#4476061)
It also works on lists, I think he got confused.
Aren't all iterables objects? You can turn any object into an iterable by adding through __iter__ method IIRC
Yes, but all objects are not iterables. If you list1.extend(object1) as the cheat sheet describes and object1 has no __iter__, it won't work. Likewise, you can iterate strings, so understanding the target of the iterable will be frustrating for list_of_strings.extend('newstring').
This is better described in python's native help documentation:
$ python
>>> help()
>>> list
searching for extend via '/extend': | extend(...)
| L.extend(iterable) -- extend list by appending elements from the iterable
Concise and correct.
> list1.extend(object1): Extends list1 with object1 creating a whole list instead of append which jams a an object inside the list.
Extend takes an iterable and not an object. Not sure I can even comprehend that sentence.
> dictionary1.update(dictionary2): updates dictionary1 with values from identical keys in dictionary2
Identical to what? Update adds all key-value pairs from dictionary2. I guess the text might refer to the fact that update overwrites existing values.
> ‘string1’.strip(): Returns one string with whitespace removed.
Only at the start and end of the string.
> ’string1’.count(object1): Returns the number of times that object1 appears in string1.
This method doesn't exist as far as I can tell and I'm not sure how an object "appears" in a string.