EDIT1:
i have problem about converting a string in a list. I collect only numbers from a file. Then convert them into integer. Using,
For line in range (0,len(file_name):
file_name[line] = int (file_name[line])
It worked, every number converted string to integer but only one number remain string. [2,4,'3']. Now, how can i convert that string into integer.
Thanks
You can do this in a list comprehension instead of a manual for loop.
file_name = [int(i) for i in file_name]
Related
I am working on an IPV4 breakdown where I have the necessary values in a string variable to represent the binary
(example: 00000000.00000000.00001111.11111111) This is a string
I need a way to turn this string into binary to then properly convert it to it's proper integer value
(in this case 0.0.15.255)
I've seen posts asking about something similar but attempting to apply it to what I'm working on has been unsuccessful
Apologies if this made no sense this is my first time posing a question here
You can achieve this using int() with base argument.
You can know more about int(x,base) - here
Split the string at '.' and store it in a list lst
For every item in lst, convert the item (binary string) to decimal using int(item, base=2) and then convert it into string type.
Join the contents of lst using .
s = '00000000.00000000.00001111.11111111'
lst = s.split('.')
lst = [str(int(i,2)) for i in lst]
print('.'.join(lst))
# Output
0.0.15.255
First split the string on . then convert each to integer equivalent of the binary representation using int builtin passing base=2, then convert to string, finally join them all on .
>>> text = '00000000.00000000.00001111.11111111'
>>> '.'.join(str(int(i, base=2)) for i in text.split('.'))
# output
'0.0.15.255'
You should split the data, convert and combine.
data = "00000000.00000000.00001111.11111111"
data_int = ".".join([str(int(i, 2)) for i in data.split(".")])
print(data_int) # 0.0.15.255
Welcome! Once you have a string like this
s = '00000000.00000000.00101111.11111111'
you may get your integers in one single line:
int_list = list(map(lambda n_str: int(n_str, 2), s.split('.')))
I have the following array:
a =['1','2']
I want to convert this array into the below format :
a=[1,2]
How can I do that?
You can do it like that. You change each element of a (which are strings) in an integer.
a=[int(x) for x in a]
This single inverted comma you are talking about is the difference between str and int. This is pretty basic python stuff.
A string is a characters, displayed with the inverted comma's around it. 'Hello' is a string, but '1' can be a string too.
In you case ['1','2'] is a list of strings, and [1,2] is a list of numbers.
To convert a string to an int, you can do what is called casting. This is converting one type to another (They have to be compatible though.) Casting 'hello' to a number doesn't make sense and won't work.
Casting '1' to a number is possible by calling int('1') which will result in 1
In your case you can cast all elements in you list by calling a = [int(x) for x in a].
For more info on types see this article.
For information on list comprehensions (What I used to change your list) see this article.
I'm trying to have the user input a vector so that it can be added, subtracted, etc. The first line is the list input, but it is storing all of the characters as strings including the brackets and commas. The third and fourth line gets rid of the brackets and commas, leaving the three user-inputted numbers as strings.
v = input('Input integers for a vector "v" ex. [1,2,-7]: ')
aux = v[1:-1]
list = aux.split(',')
for x in list:
int(x)
print(list[0] + list[1])
The for loop is my attempt to iterate the list and make all of the numbers into integers but it is still returning them as strings. For example, if I input the list [3,6,5], the program will print 36 at the end instead of the intended 9. I tried using the map function to change them, but that was returning the same values as strings.
How can I make all of the list items into integers after removing the brackets and commas?
First of all the int() caster does not cast the variable in place, it returns the casted value so you need to assign it back.
Secondly, when using the loop of the kind
for x in list:
x is not a pointer reference to the list element, instead it is variable with the value of the list element copied into it, so even if you did:
for x in list:
x = int(x)
the elements in the list will not be affected.
Your loop to convert string chars to int should be (one approach):
for i in range(len(list)):
list[i] = int(list[i])
I have a code as follows
with open(f1) as f:
userid=f.read().replace('0', str(instance.id))
The above works well. Now, the variable userid as a string has character In will like to replace. I tried this code and is given errors shown below. Please note: the variable the_user.phonelist is a python LIST. I will like to replace the character [] with the list.
ans = userid.replace('[]', the_user.phonelist)
Error: Can't convert 'list' object to str implicitly
As the error suggests, you have to convert the list to a string
One way to do this is with a join:
phonelist_str = ''.join(the_user.phonelist)
This only will work though if all the items in the list are strings. If not, you'll have to pair it with a list comprehension in order to cast all of the list's items to strings:
phonelist_str = ''.join([str(x) for x in the_user.phonelist])
Once you have a string, you can do your replace:
ans = userid.replace('[]', phonelist_str)
I am trying to find the sum of all numbers in a list but every time I try I get an error that it cannot convert the string to float. Here is what I have so far.
loop = True
float('elec_used')
while (loop):
totalelec = sum('elec_used')
print (totalelec)
loop = False
You need none of your code above. The while loop is unnecessary and it looks like its just exiting the loop in one iteration i.e. its not used correctly. If you're simply summing all the values in the list:
sum([float(i) for i in elec_used])
If this produces errors, please post your elec_used list. It probably contains string values or blank spaces.
'elec_used' is of type string of characters. You can not convert characters to the float. I am not sure why you thought you could do it. However you can convert the numeric string to float by typecasting it. For example:
>>> number_string = '123.5'
>>> float(number_string)
123.5
Now coming to your second part, for calculating the sum of number. Let say your are having the string of multiple numbers. Firstly .split() the list, type-cast each item to float and then calculate the sum(). For example:
>>> number_string = '123.5 345.7 789.4'
>>> splitted_num_string = number_string.split()
>>> number_list = [float(num) for num in splitted_num_string]
>>> sum(number_list)
1258.6
Which could be written in one line using list comprehension as:
>>> sum(float(item) for item in number_string.split())
1258.6
OR, using map() as:
>>> sum(map(float, number_string.split()))
1258.6