python3 for loop dictionary [duplicate] - python

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
whats another way to write python3 zip
whats a better way to write these things I was introduced to these codes but I am not used to seeing them these ways:
(alla,allc,) = (set(s) for s in zip(*animaldictionary.keys()))
how else can you write this
print('\n'.join(['\t'.join((c,str(sum(animaldictionary.get(ac,0)
for a in alla
for ac in ((a,c,),))//12)))
for c in sorted(allc)]))

I'll update my answer with a more comprehensive (no pun intended) result once I get home and have more time to wrap my head around this interesting set of comprehensions. For now you will want to check out the following:
List Comprehensions
Nested List Comprehensions
Sets And Set Comprehensions

Related

Regular Expression ".{}." in Python List Comprehensions [duplicate]

This question already has answers here:
What does {0} mean in this Python string?
(6 answers)
Closed 3 years ago.
I'm reviewing code in the popular Deep learning with Python book and came across. The latter part of the code eventually copies 1000 cat images to a directory, and this line stores the file names to fnames.
fnames = ['cat.{}.jpg'.format(i) for i in range(1000)]
Can someone explain how the syntax works, particularly .{}. in this statement? I have used list comprehension in the past, but I'm not following how this line works.
There is no regular expression here. str.format will replace {} with its argument, that's all. There are other ways to use str.format, but that's what it does here. So for each of the thousand numbers generated in range, the comprehension produces one string that is the result of formatting the number via the filename pattern.

How to remove "sub-lists" from a list? [duplicate]

This question already has answers here:
How do I make a flat list out of a list of lists?
(34 answers)
Closed 3 years ago.
I am having trouble creating a function since I want to be able to refer to the tuple and not the list which contains the tuples. Hence I have come to the conclusion that I want to get rid of the inner square brackets.
I have a list similar to this:
List=[[(1,2),(3,4),(5,6)],[(1,2),(5,7),(3,8)],[...],[...]]
So the question I am asking is how can I remove the inner [ ] so that I can just produce a single list of tuples.
Also, I am not sure if I am allowed to ask another question, but how would i also delete duplicates (x,y) entries in my new list?
I have not provided code for this since I know the problem for the code I have and I believe I would confuse people by including it. If however, you wish to see the code, or want me to clarify anything please let me know.
I think this has been asked and answered on here multiple times. The solution to the flattening problem would be as follows:
new_list = [tupl for l in List for tupl in l]

Shortening for loop for sending commands through pipe - python [duplicate]

This question already has answers here:
cleanest way to call one function on a list of items
(1 answer)
Is it Pythonic to use list comprehensions for just side effects?
(7 answers)
Closed 5 years ago.
I know that for creating lists you can shorten a few lines down to something like (in python):
a = [k*2 for k in range(10)]
Can you do this for when sending data through a pipe. (using multiprocessing module in this case). eg:
k = 'hello'
[channel.send(k) for channel in channels]
instead of:
k = 'hello'
for channel in channels:
channel.send(k)
Any suggestions would be great! Thanks in advance.
EDIT: Has been answered. List comprehensions bad idea. Just keep it neat to one line:
k = 'hello'
for channel in channels: channel.send(k)
No. List comprehensions are for creating lists. If you don't want the list, don't use a list comprehension. There is nothing wrong with using a for loop when it is the appropriate thing to use.

C command similar to "if x in list" of Python [duplicate]

This question already has answers here:
Check if a value exists in an array in Cython
(2 answers)
Closed 6 years ago.
In Python we could use if x in list: so I was wondering if there's a similar command in C, so that we don't have to go through the whole thing using a for.
How can you know whether a value is contained in an array without cycling through it? This is exactly what Python does under the hood. No, there's no magical way to instantly know this.

What is a better way to remove key from dict in Python 3.4? [duplicate]

This question already has answers here:
Difference between del, remove, and pop on lists
(14 answers)
Closed 7 years ago.
I've found 2 ways of doing it:
del dict[key]
vs
dict.pop(key)
Which one is better and why, or maybe there's more?
del is generally faster than pop().
Have a look at this discussion Best way to remove an item from a Python dictionary?

Categories

Resources