473,804 Members | 3,185 Online
Bytes | Software Development & Data Engineering Community
+ 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 1884
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
7405
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 interchangeable in all circumstances (as long as they're paired) so there's no overloading to muddy the language. Of course there could be some interesting problems with current code that doesn't make a distinction, but it would be dead easy to fix...
1
2094
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 variable or With block variable not set. I am trying to print the contents of a list box on another form. This is the sub, and the highlighted line is where it is falling over.
1
15980
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 completely... myList.RemoveAll( ??? );
2
2562
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 aXYZ As Integer = 5 Dim myString As String Dim myResults As Integer myString = "aXYZ"
3
1525
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 - http://www.lokalny.net/Source97.zip -- access 200 - http://www.lokalny.net/Source2000.zip ------ Im trying to change code - just trying to learn something. But nothing seems
27
79933
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
12604
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 first column is a textbox(TemplateColumn), the other 3 columns are just display(BoundColumn). (1) if the value of the textbox is 0, then i'd like to change it null, so the box is empty
23
2977
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 C & hence found that claim of the authors is *partial*. If we use C-style strings in C++ instead of Library String class, then they are slow but if write the same programme in C then C strings are "faster" than both C++ Library strings & C-style...
5
1819
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 about getting the highest value in the collection any help would be appreciated, even just learning from the answers would be a lot of help.. cheers - see below Given a list of integers and an integer variable declared like this: List<Integer>...
0
9704
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9572
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10303
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9132
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6845
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5508
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5639
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4282
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 we have to send another system
3
2978
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.