Do you think that it is OK to use map for an applying function to arguments list and ignore the results?
map(foo, bar)
It may appears as bug to person who is reading code.
...
I want to check if any of the items in one list are present in another list. I can do it simply with the code below, but I suspect there might be a library function to do this. If not, is there a more pythonic method of achieving the same result.
In [78]: a = [1, 2, 3, 4, 5]
In [79]: b = [8, 7, 6]
In [80]: c = [8, 7, 6, 5]
In [81...
False is equivalent to 0 and True is equivalent 1 so it's possible to do something like this:
def bool_to_str(value):
"""value should be a bool"""
return ['No', 'Yes'][value]
bool_to_str(True)
Notice how value is bool but is used as an int.
Is this this kind of use Pythonic or should it be avoided?
...
Is there a more Pythonic way of doing this?:
if self.name2info[name]['prereqs'] is None:
self.name2info[name]['prereqs'] = []
if self.name2info[name]['optionals'] is None:
self.name2info[name]['optionals'] = []
The reason I do this is because I need to iterate over those later. T...
More and more features of Python move to be "lazy executable", like generator
expressions and other kind of iterators.
Sometimes, however, I see myself wanting to roll a one liner "for" loop, just to perform some action.
What would be the most pythonic thing to get the loop actually executed?
For example:
a = open("numbers.txt", "w")
...
For my program I have a lot of places where an object can be either a string or a list containing strings and other similar lists. These are generally read from a JSON file. They both need to be treated differently. Right now, I am just using isinstance, but that does not feel like the most pythonic way of doing it, so does anyone have a...
Like csv.reader() are there any other functions which can read .rtf, .txt, .doc files in Python?
...
I was just trying to convert a PPT using the following URL http://code.google.com/p/qifei/wiki/PDFConverter python code
I could see the same thing happening with the command line option too
python documentconverter.py /home/rajeev/Desktop/Downloads/Industry2.ppt /home/rajeev/Desktop/test.pdf
It appears that the image overlaps on some ...
I have a list of strings parsed from somewhere, in the following format:
[key1, value1, key2, value2, key3, value3, ...]
I'd like to create a dictionary based on this list, like so:
{key1:value1, key2:value2, key3:value3, ...}
An ordinary for loop with index offsets would probably do the trick, but I wonder if there's a Pythonic wa...
JavaScript has object literals, e.g.
var p = {
name: "John Smith",
age: 23
}
and .NET has anonymous types, e.g.
var p = new { Name = "John Smith", Age = 23}; // C#
Something similar can be emulated in Python by (ab)using named arguments:
class literal(object):
def __init__(self, **kwargs):
for (k,v) in kwargs.iter...
i have implemented a function:
def postback(i,user,tval):
"""functie ce posteaza raspunsul bazei de date;stringul din mesaj tb sa fie mai mic de 140 de caractere"""
result = {
1:api.PostDirectMessage(user,'Trebuie sa-mi spui si marca pe care o cauti'),
2:postmarket(user,tval),
3:api.PostDirectMessage(use...
I populate a python dictionary based on few conditions,
My question is can we retrieve in the same order as it is populated.
questions_dict={}
data = str(header_arr[opt]) + str(row)
questions_dict.update({data : xl_data})
valid_xl_format = 7
if (type.lower() == "ma" o...
If a string contains *SUBJECT123 how to determine that the string has subject in it in python
...
How do I find a string between two substrings ('123STRINGabc' -> 'STRING')?
My current method is like this:
>>> start = 'asdf=5;'
>>> end = '123jasd'
>>> s = 'asdf=5;iwantthis123jasd'
>>> print((s.split(start))[1].split(end)[0])
iwantthis
However, this seems very inefficient and un-pythonic. What is a better way to do something like ...
I am a PHP developer exploring the outside world. I have decided to start learning Python.
The below script is my first attempt at porting a PHP script to Python. Its job is to take tweets from a Redis store. The tweets are coming from Twitter's Streaming API and stored as JSON objects. Then the information needed is extracted and dumpe...
In the below code d_arr is an array of dictionaries
def process_data(d_arr):
flag2 = 0
for dictionaries in d_arr:
for k in dictionaries:
if ( k == "*TYPE" ):
""" Here we determine the type """
if (dictionaries[k].lower() == "name"):
dictionaries.update({"type" : 0})
fu...
What is an efficient way to repeat a string to a certain length? Eg: repeat('abc', 7) -> 'abcabca'
Here is my current code:
def repeat(string, length):
cur, old = 1, string
while len(string) < length:
string += old[cur-1]
cur = (cur+1)%len(old)
return string
Is there a better (more pythonic) way to do this...
I wrote a simple script to solve a "logic puzzle", the type of puzzle from school where you are given a number of rules and then must be able to find the solution for problems like "There are five musicians named A, B, C, D, and E playing in a concert, each plays one after the other... if A goes before B, and D is not last ... what is th...
In the code below how match the pattern after "answer" and "nonanswer" in the dictionary
opt_dict=(
{'answer1':1,
'answer14':1,
'answer13':12,
'answer11':6,
'answer5':5,
'nonanswer12':1,
'nonanswer11':1,
'nonanswer4':1,
'nonanswer5':1,})
And
if opt_dict:
for ii in opt_dict:
lo...
In the following dictionary,can the elements be sorted according the last prefix in the key
opt_dict=(
{'option1':1,
'nonoption2':1,
'nonoption3':12,
'option4':6,
'nonoption5':5,
'option6':1,
'option7':1,
})
for key,val in opt_dict.items():
if "answer" in key: //match keys last prefix and print output
...