473,472 Members | 2,039 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

change a list of integer into a list of strings and for loop & string

90 New Member
I have a list of integers s=[1,2,3,4] to be converted to s=['1','2','3','4']
I tried
Expand|Select|Wrap|Line Numbers
  1. s=[1,2,3,4]
  2.  
  3. for i in s:
  4.     str(a[i])
  5.  
  6.  
  7. '2'
  8. '3'
  9. '4'
  10.  
but I lost "1" , can someone explain why I lost "1"? I knew that I could use
Expand|Select|Wrap|Line Numbers
  1.    str(a[i-1])
  2.  

My second question is about create a new string

Expand|Select|Wrap|Line Numbers
  1. # I have a string
  2. s='hello! world'
  3.  
  4. # now I want to have a new string printed as
  5. #h
  6. #he
  7. #hel
  8. #...
  9.  
  10. # I tried
  11.  
  12. for i in len(s):
  13.  stringA=a[i]
  14.  stringA+=a[i+1]
  15.  print stringA
  16. #but it did not work
  17.  
  18.  
Oct 5 '07 #1
6 1872
rhitam30111985
112 New Member
I have a list of integers s=[1,2,3,4] to be converted to s=['1','2','3','4']
I tried
Expand|Select|Wrap|Line Numbers
  1. s=[1,2,3,4]
  2.  
  3. for i in s:
  4.     str(a[i])
  5.  
  6.  
  7. '2'
  8. '3'
  9. '4'
  10.  
but I lost "1" , can someone explain why I lost "1"? I knew that I could use
Expand|Select|Wrap|Line Numbers
  1.    str(a[i-1])
  2.  
replace a[i] with i... i means the the elements in the list... not their index.. :
Expand|Select|Wrap|Line Numbers
  1. for i in s:
  2.           str(i)
  3.  
Oct 5 '07 #2
ghostdog74
511 Recognized Expert Contributor
Expand|Select|Wrap|Line Numbers
  1. >>> s=[1,2,3,4]
  2. >>> [str(i) for i in s]
  3. ['1', '2', '3', '4']
  4. >>> s=[str(i) for i in s]
  5.  
Oct 5 '07 #3
python101
90 New Member
thank you very much.

I just added second question.
Oct 5 '07 #4
rhitam30111985
112 New Member
for the hello world program u can try this :

Expand|Select|Wrap|Line Numbers
  1.  for  i in range(len(s)):
  2.      print s[0:i+1]
  3.  
Oct 5 '07 #5
python101
90 New Member
i just read the book some days ago that I could use [:::] to get this.

Thank you very much for helping me out.
Oct 5 '07 #6
rhitam30111985
112 New Member
for i in len(s):

stringA=a[i]

stringA+=a[i+1]

print stringA
your code did not work because u havent defined the variable 'a' anywhere.... so the interpreetr doesnt know what to assign to the variable 'stringA'

regards
rhitam
Oct 5 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

17
by: Gordon Airport | last post by:
Has anyone suggested introducing a mutable string type (yes, of course) and distinguishing them from standard strings by the quote type - single or double? As far as I know ' and " are currently...
1
by: NickB | last post by:
Please could someone tell me what is wrong. Ther error is: An unhandled exception of type 'System.NullReferenceException' occurred in microsoft.visualbasic.dll Additional information: Object...
1
by: John Young | last post by:
Hi I have just started messing about with generics. I have done this.. List<string> myList = new List<string>(); I've added strings to the list... But now I want to clear the list...
2
by: Roger Lord | last post by:
I am constructing a string in my program and then trying to use it to get the value of an integer variable whose label looks like that string. I can't figure out how to do it. For example: Dim...
3
by: Jeff | last post by:
for your info - I'm a newbe. Code is at the bottom. Access dbase (each form ca. 10 KB) if my explanations are not enough (probably they aren't). -- access 97 -...
27
by: Tripper | last post by:
Which is the better way to go and why? //trivial example List<string> strings = GetStrings(); foreach (string s in strings) { // some operation; } strings.ForEach(
2
by: simon | last post by:
hello, new to vb.net, have a few questions about DataGrid. I have a dataGrid that is working pulling a dataset back from a stored proc and binding to the datagrid for display the datagrid's...
23
by: arnuld | last post by:
i was doing exercise 4.3.1 - 4.29 of "C++ Primer 4/e" where authors, with "run-time shown", claim that C++ Library strings are faster than C-style character strings. i wrote the same programme in...
5
by: gazgump | last post by:
I'm doing some practice questions and cant seem to work out these collection probelms think i need to use iterator or a while loop but cant seem to get them to work - dont really know how i'd go...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.