LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-30-2016, 05:52 AM   #1
Gyroman
Member
 
Registered: Nov 2014
Location: Australia
Distribution: Mint 19.1 Toshiba Satellite Pro R50-C
Posts: 108
Blog Entries: 5

Rep: Reputation: 1
Extending a Python List


p = 5
values = [150,320,120,550,630]

I want to add the index to the list of values;
values
[[150,0],[320,1],[120,2],[550,3],[630,4]]

Code sample which fails;

for i in range(p):
____values[i] = [values[i],i]

TypeError: 'str' object does not support item assignment

It used to work then stopped.. can't see any change I made to values list ?

Last edited by Gyroman; 06-30-2016 at 05:55 AM. Reason: indent was removed
 
Old 06-30-2016, 07:36 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,850

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
this works for me:
Code:
p = 5
values = [150,320,120,550,630]
valuesxx = {}

for i in range(p):
    valuesxx[i] = [values[i],i]
check the type of ___values
 
1 members found this post helpful.
Old 06-30-2016, 07:18 PM   #3
Gyroman
Member
 
Registered: Nov 2014
Location: Australia
Distribution: Mint 19.1 Toshiba Satellite Pro R50-C
Posts: 108

Original Poster
Blog Entries: 5

Rep: Reputation: 1
Thanks.. Yes type of 'values' had changed..

I have a couple of simple questions about Python 3.3

After
>>>import prog
returns
>>>

(1) How do I re-run prog without quit() and re-import..?

(2) Inside prog what command do I use to exit on a condition before prog ends normally..?
 
Old 07-01-2016, 12:14 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,850

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
1) usually you have a function and you only need to invoke it again. (but I have no idea about your environment at all)
2) I do not understand
 
1 members found this post helpful.
Old 07-01-2016, 02:13 AM   #5
Gyroman
Member
 
Registered: Nov 2014
Location: Australia
Distribution: Mint 19.1 Toshiba Satellite Pro R50-C
Posts: 108

Original Poster
Blog Entries: 5

Rep: Reputation: 1
Linux on HP Laptop.. Python 3.3

(1) I create a module in gedit and import to run. I want to know if there is a command I can use to re-run the same module without having to quit() and restart Python and re-import it again to run it a second time.

(2) Just mean how to exit a Python module based on a condition.. ie prematurely..

thanks
 
Old 07-01-2016, 03:26 AM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,850

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
1. reimport is quite simple, just type import module again. But it won't rerun anything else just the initialisation of that module
2. I do not think you can exit a module - by the way what does it mean at all?
When you import a module it will be available. And when you execute del module, it won't be available any more, but it won't work in case of from module import something.
 
1 members found this post helpful.
Old 07-03-2016, 12:18 AM   #7
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,225

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
You don't need p if it's just to store the length of the list. And the Pythonic way to do this would be with a list comprehension. Note the use of enumerate to iterate over the list and get both the index and the value at that index.

Code:
>>> values = [150, 320, 120, 550, 630]
>>> values = [[value, index] for index, value in enumerate(values)]
>>> values
[[150, 0], [320, 1], [120, 2], [550, 3], [630, 4]]
I'm also having trouble understanding what you mean by "re-import". Python 2 had a "reload" keyword, which I think may be what you're thinking of. It's changed in Python 3. Here's a link about that:

http://stackoverflow.com/q/961162/240515

Last edited by dugan; 07-03-2016 at 12:30 AM.
 
1 members found this post helpful.
  


Reply

Tags
python based



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Python List Question nitrohuffer2001 Programming 12 11-10-2015 02:17 AM
LXer: Extending GlusterFS with Python LXer Syndicated Linux News 0 01-19-2013 01:51 AM
[SOLVED] list sorting in python aihaike Programming 4 05-12-2011 12:20 AM
Science/Engineering programming - Which is easier in extending C? Python/Ruby? chutsu Programming 8 06-05-2010 01:33 PM
Extending Python with C (reference counting questions) spacepigeon Programming 0 07-19-2008 09:27 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:54 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration