how to get a list of integers? - list

The situation is:
a1= ("5.6,13.16,19,23,24,25,26,29,30,31,32,34,35,36,49,50,51,54,60,65,67,74,75,76,77,81,86,87").replace (".",",")
#replace . with comma
print (a1)
output is :
5,6,13,16,19,23,24,25,26,29,30,31,32,34,35,36,49,50,51,54,60,65,67,74,75,76,77,81,86,87
Now, i would like insert a1 in the list of integer, like this:
[5,6,13,16,19,23,24,25,26,29,30,31,32,34,35,36,49,50,51,54,60,65,67,74,75,76,77,81,86,87]
Any suggestion?
Thanks

You can just use the split like follows:
list = a1.split (",")
# convert elements to ints
li = []
for i in list:
li.append(int(i))
# print list as integers
print "li : ", li

You could split up the string like and then convert each element in the list to integer if you need it as an integer string.
[int(a) for a in a1.split(',')]

Related

How to call characters from first list with second list

I want to input two comma separated strings: the first a set of strings, the second a set of ranges and return substrings based on ranges, for example:
x=input("Input string to search: ")
search=x.split(',')
y=input("Input numbers to locate: ")
numbers=y.split(',')
I would then like to use the second list of ranges to print out specified characters from the first list.
An example:
Input string to search: abcdefffg,aabcdefghi,bbcccdefghi
Input numbers to locate: 1:2,2:3,5:9
I would like the output to look like this:
bc
bcd
defghi
Any suggestions? Thanks in advance!
split(':') splits a "range" into its two components. map(int, ...) converts them to integers. string[a:b] takes characters at indices a through b.
zip is an easy way to read from two different lists combined.
Let me know if you have any other questions:
x = "abcdefffg,aabcdefghi,bbcccdefghi"
search = x.split(',')
y = "1:2,2:3,5:9"
numbers = y.split(',')
results = []
for string, rng in zip(search, numbers):
start, how_many = map(int, rng.split(':'))
results.append(string[start:start+how_many])
print(" ".join(results))
# Output:
# bc bcd defghi

Finding a substring within a string using an array?

Trying to get my head around programming, I cannot work out why this doesn't work? Am I using "not" and "in" incorrectly?
I am trying to get the program to only print the characters that appear in both the strings. It correctly identifies them, but I can't get it to only print one set of the characters if there is more than one occurrence.
a = input("string1 :")
b = input("string2: ")
list1 = []
for i in a:
for j in b:
if i == j and i not in list1:
list1.append([i])
break
print(list1)
For example if you print the strings "alexander" and "alex" it will print the characters a, l, e, x, a, e
I know this current method only works if string1 is inputted as the main string, but I am just interested as to why this doesn't work.
The problem is that instead of adding the common letter to the list1, you are adding a new list containing only the common letter to list1. At the end you have a list of lists. At the same time you are checking if a single letter is in the list of lists, which will be always false.
You should just add the common letter "i" to the list using append:
a = input("string1 :")
b = input("string2: ")
list1 = []
for i in a:
for j in b:
if i == j and i not in list1:
list1.append(i)
break
print(list1)
You could also just convert string a and b to sets and intersect them:
print set(a) & set(b)

Haskell output a list of ascii value

For example I have a haskell list [72,73,74,75], how can i output this list as a string?, all elements in the list are ascii value.
You can combine map, that applies a function to each element of a list, and the chr function, that convert an Int value to its Char equivalent:
> map chr [72,73,74,75]
"HIJK"
You can convert an Int code point to a Char using chr :: Int -> Char; a String is just a list of Chars. Note that this'll work for any Unicode code point, not just ASCII, which is something you should be doing anyway.
You can find functions like this using Hoogle; just type something like Int -> Char, and it'll give you functions that match that type.
You can use 'chr' from the module Char to convert the integer values to characters:
import Char
intListToString l = [ chr x | x <- l ]
main = do
putStrLn $ "the string: " ++ (intListToString [72,73,74,75])
Running the above with 'runghci' gives:
the string: HIJK
Do you want this list as a straight string or a list with commas? Unless you want to convert ASCII char values to their character counterparts (which was already covered), you can do the following:
concatMap show [72,73,74,75]
will give you a "72737475" string and
init $ tail $ show [72,73,74,75]
will give you a "72,73,74,75" string

Convert list of one number to int

I have a regular expression that parses a line# string from a log. That line# is then subjected to another regular expression to just extract the line#.
For example:
Part of this regex:
m = re.match(r"^(\d{4}-\d{2}-\d{2}\s*\d{2}:\d{2}:\d{2}),?(\d{3}),?(?:\s+\[(?:[^\]]+)\])+(?<=])(\s+?[A-Z]+\s+?)+(\s?[a-zA-Z0-9\.])+\s?(\((?:\s?\w)+\))\s?(\s?.)+", line)
Will match this:
(line 206)
Then this regex:
re.findall(r'\b\d+\b', linestr)
Gives me
['206']
In order to further process my information I need to have the line number as an integer and am lost for a solution as to how to do that.
You may try:
line_int = int(re.findall(r'\b\d+\b', linestr)[0])
or if you have more than one element in the list:
lines_int = [int(i) for i in re.findall(r'\b\d+\b', linestr)]
or even
lines_int = map(int, re.findall(r'(\b\d+\b)+', linestr))
I hope it helps -^.^-
Use int() to convert your list of one "string number" to an int:
myl = ['206']
int(myl[0])
206
if you have a list of these, you can conver them all to ints using list comprehension:
[int(i) for i in myl]
resulting in a list of ints.
You can hook this into your code as best fits, e.g.,
int(re.findall(r'\b\d+\b', linestr)[0])
int(re.findall(r'\b\d+\b', linestr)[0])
?

ViM: how to put string from input dialog in a list

VIM: Does anyone know how to put a string from an input dialog in a list?
p.e.:
the string "3,5,12,15"
to:
list item[1] = 3
list item[2] = 5
list item[3] = 12
etc.
and how can I know how many list items there are?
From :h E714
:let l = len(list) " number of items in list
:let list = split("a b c") " create list from items in a string
In your case,
let string = "3,5,7,19"
let list = split(string, ",")
echo len(list)
Use split, len and empty functions:
let list=split(string, ',')
let list_length=len(list)
" If all you want is to check whether list is empty:
if empty(list)
throw "You must provide at least one value"
endif
Note that if you want to get a list of numbers out of the string, you will have to use map to transform list elements into numbers:
let list=map(split(string, ','), '+v:val')
Most of time you can expect strings be transformed into numbers, but sometimes such transformation is not done.