Re: One-liner to merge lists?
On 22/02/2022 10:44, Frank Millman wrote:
On 2022-02-22 11:30 AM, Chris Angelico wrote:
On Tue, 22 Feb 2022 at 20:24, Frank Millman wrote:
Hi all
I think this should be a simple one-liner, but I cannot figure it out.
I have a dictionary with a number of keys, where each value is a single
list -
>>> d = {1: ['aaa', 'bbb', 'ccc'], 2: ['fff', 'ggg']}
I want to combine all values into a single list -
>>> ans = ['aaa', 'bbb', 'ccc', 'fff', 'ggg']
I can do this -
>>> a = []
>>> for v in d.values():
... a.extend(v)
...
>>> a
['aaa', 'bbb', 'ccc', 'fff', 'ggg']
I can also do this -
>>> from itertools import chain
>>> a = list(chain(*d.values()))
>>> a
['aaa', 'bbb', 'ccc', 'fff', 'ggg']
>>>
Is there a simpler way?
itertools.chain is a good option, as it scales well to arbitrary
numbers of lists (and you're guaranteed to iterate over them all just
once as you construct the list). But if you know that the lists aren't
too large or too numerous, here's another method that works:
sum(d.values(), [])
['aaa', 'bbb', 'ccc', 'fff', 'ggg']
It's simply adding all the lists together, though you have to tell it
that you don't want a numeric summation.
Thanks, that is neat.
However, I did see this -
>>> help(sum)
Help on built-in function sum in module builtins:
sum(iterable, /, start=0)
Return the sum of a 'start' value (default: 0) plus an iterable of
numbers
When the iterable is empty, return the start value.
This function is intended specifically for use with numeric values
and may reject non-numeric types.
>>>
So it seems that it is not recommended.
If you don't like the idea of 'adding' strings you can 'concat'enate:
>>> items = [[1,2,3], [4,5], [6]]
>>> functools.reduce(operator.concat, items)
[1, 2, 3, 4, 5, 6]
>>> functools.reduce(operator.iconcat, items, [])
[1, 2, 3, 4, 5, 6]
The latter is the functional way to spell your for... extend() loop.
Don't forget to provide the initial value in that case lest you modify
the input:
>> functools.reduce(operator.iconcat, items) # wrong
[1, 2, 3, 4, 5, 6]
>>> items
[[1, 2, 3, 4, 5, 6], [4, 5], [6]] # oops
--
https://mail.python.org/mailman/listinfo/python-list
Re: Best way to check if there is internet?
[email protected] writes: > I think someone said it way upthread: don't check, just do whatever you > came to do, and it will work or it will fail (presumably, your program > can tell the difference, regardless of a past snapshot of being able to > retrieve data from an arbitrary URL). > > EAFP, anyone? Yes, but the code being discussed is still helpful, if only for error handling: yes, the network isn't fully up, but *why" isn't it? while True: try: do_whatever_I_came_to_do() except NetworkError: net_config_with_good_error_detection() Aside from error handling, it's useful in a network-up script: when you've just enabled a network, it's good to check right then if there's a captive portal and deal with it. If you just assume that the first thing the user wants is to go to an http: page in a browser, and so don't bother to check for a captive portal, that'll be annoying for people who want to fetch IMAP mail, or run ssh, or load an https: page. ...Akkana -- https://mail.python.org/mailman/listinfo/python-list
Re: Best way to check if there is internet?
On Thu, 24 Feb 2022 at 03:39, Akkana Peck wrote: > > [email protected] writes: > > I think someone said it way upthread: don't check, just do whatever you > > came to do, and it will work or it will fail (presumably, your program > > can tell the difference, regardless of a past snapshot of being able to > > retrieve data from an arbitrary URL). > > > > EAFP, anyone? > > Yes, but the code being discussed is still helpful, if only for > error handling: yes, the network isn't fully up, but *why" isn't it? > > while True: > try: > do_whatever_I_came_to_do() > except NetworkError: > net_config_with_good_error_detection() That's fair, although the NetworkError should at least be able to distinguish between "no DNS server", "DNS server returned error", "timeout connecting", "host not found", "network not found", "connection refused", etc. > Aside from error handling, it's useful in a network-up script: > when you've just enabled a network, it's good to check right then if > there's a captive portal and deal with it. That's a little harder to recognize, since there are so many ways that it can be done. Do all HTTP requests return 302 redirects? Do all requests return a static HTML page? What do non-HTTP requests return? I don't know that there's a generic way to recognize those kinds of login screens. > If you just assume that > the first thing the user wants is to go to an http: page in a > browser, and so don't bother to check for a captive portal, > that'll be annoying for people who want to fetch IMAP mail, > or run ssh, or load an https: page. Agreed, it is annoying, especially when those logins time out after fifteen minutes and force you to watch another ad before they let you use the connection. But it's still extremely hard to recognize reliably, so you'd probably end up coding something for the specific portal that you are experienced with. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: Best way to check if there is internet?
On 2022-02-23 at 09:28:40 -0700, Akkana Peck wrote: > [email protected] writes: > > I think someone said it way upthread: don't check, just do whatever you > > came to do, and it will work or it will fail (presumably, your program > > can tell the difference, regardless of a past snapshot of being able to > > retrieve data from an arbitrary URL). > > > > EAFP, anyone? > > Yes, but the code being discussed is still helpful, if only for > error handling: yes, the network isn't fully up, but *why" isn't it? > > while True: > try: > do_whatever_I_came_to_do() > except NetworkError: > net_config_with_good_error_detection() Sure, after the fact, once things fail, diagnostics are useful. The questions that remain are when should you run them, how complex do they have to be, and how reliable can they be? > Aside from error handling, it's useful in a network-up script: > when you've just enabled a network, it's good to check right then if > there's a captive portal and deal with it. If you just assume that > the first thing the user wants is to go to an http: page in a > browser, and so don't bother to check for a captive portal, > that'll be annoying for people who want to fetch IMAP mail, > or run ssh, or load an https: page. Honest questions: If I'm captured by a portal, do I "have internet"? If I don't "have internet," who and/or what should detect, report, and/or fix it? When? (As a matter of fact, I'm one of those people who annoyingly has to deal with captive portals when my only use for the internet is IMAP or SMTP.) -- https://mail.python.org/mailman/listinfo/python-list
Re: Long running process - how to speed up?
Shaozhong SHI wrote: > Can it be divided into several processes? I'd do it like this: from time import sleep from threading import Thread t = Thread(target=lambda: sleep(1)) t.run() # do your work here t.wait() -- https://mail.python.org/mailman/listinfo/python-list
Coding help
I know next to nothing about computer coding nor Python. However, I am working on a mathematical challenge in which coding is required to calculate and generate different potential solutions. Can anyone help? If so, please private message me and we can discuss in more detail. Many thanks, Richard -- https://mail.python.org/mailman/listinfo/python-list
Re: Best way to check if there is internet?
Abdur-Rahmaan Janhangeer wrote: > Well, nice perspective. > > It's a valid consideration, sound theory > but poor practicality according to me. On the contrary: It is absolutely the right and only way to do it. > It you view it like this then between the moment > we press run or enter key on the terminal, > our Python interpreter might get deleted. Yeah, but by your logic you'd have to check the existence of /usr/bin/python each time before you run it. > Though it's nice to go in and catch exceptions, > if you have a long operation upcoming it might be nice > to see if your key element is present. That is true, but you still must make sure that your long operation fails in a controlled manner if the necessary ressource becomes unavailable in the meantime. > Checking by exceptions is what the snippet > i shared does. But we check for the ability to do it > before we start. Nothing wrong with checking before. > Of course, it can change in the seconds that follow. But it's too much pure > logic at work. No. It's the correct way of doing it. -- https://mail.python.org/mailman/listinfo/python-list
Re: Best way to check if there is internet?
Abdur-Rahmaan Janhangeer wrote: > I've got my answers but the 'wandering' a bit > on this thread is at least connected to the topic ^^. > > Last question: If we check for wifi or ethernet cable connection? > > Wifi sometimes tell of connected but no internet connection. > So it detected that there is or there is no internet ... Your application obviously needs to make a certain network connection. Why don't you just check if the connection can be made? Why would you care if it's via cable or Wifi? -- https://mail.python.org/mailman/listinfo/python-list
