Create a List of given Numbers in Python - python
I would like to create an array of Integers like this (15,0,15,47,0,15,15,0,0,15,0,0,17,0,14,0,0,15,0,0,22,29,0,0,29,22,15,15,0,15,15,0,16,0,0,16,0,0,0,0,17,0,0,19,21,0,17,16,15,0,16,0,0,15,0,16,0,0,0,15,0,16,16,0,0,0,14,21,14,21,14,0,14,29,0,14,15,15,16,0,0,0,29,22,0,0,0,0,14,0,0,0,15,0,15,16,0,0,31,14,0,0,0,0,13,13,0,0,0,14,20,27,0,0,0,0,0,15,29,15,0,0,0,0,21,28,0,15,15,16,0,0,0,0,0,15,0,0,0,15,0,15,0,0,0,17,0,0,0,0,18,0,0,15,0,0,0,15,15,0,0,15,15,0,0,0,30,16,0,0,14,27,14,0,0,14,14,14,14,21,14,29,0,0,0,14,14,0,0,45,16,0,0,29,15,0,0,0,0,15,0,17,0,0,13,13,0,13,27,28,0,13,0,13,13,40,0,0,13,0,0,0,26,13,0,19,25,13,12,25,31,0,13,13,0,0,13,14,13,0,13,0,0,0,12,19,13,26,0,13,13,0,27,15,14,0,13,0,50,13,100) in Python....
I tried this:
data = int(x,x,x,x,x,...etc)
i have this error:
TypeError: int() takes at most 2 arguments (254 given)
How is it possible? I'm working on Jupyter with Python 3.7
thank you in advance!
The syntax you have in your question it is already a tuple, you can get a list using the list class for that effect.
>>> tuple_numbers = (2,3,4,5,6)
>>> list_numbers = list(tuple_numbers)
>>> list_numbers
[2,3,4,5,6]
I see you're new to Python, at least seems to be that way. So I'll recommend reading about list and tuples.
If you want to apply some transformation to list items, try map.
Note
Take into account that it is not the same: list(2,3,4,5) that list((2,3,4,5)). The first is wrong since you are passing more than one argument
Happy coding!
If you enter help(int) on the Python interpreter:
>>> help(int)
you'll see it's documentation and a portion of it says:
Convert a number or string to an integer ...
Your question is not clear about where you are getting the numbers from. If you are getting them from a file, you'll have to:
open the file
read individual numbers. They will be strings.
apply int() to each number string
append the number to a list
Related
is there a way to use numbers in a python array with strings
I get this error from python when I try to run my program does anyone know how to fix it. ops.append(i+".)"+names[i]+"'s Living Quarters\n") TypeError: unsupported operand type(s) for +: 'int' and 'str' ops is a array for choices. names is a array with names to be made in to the ops array with a number for printing. i is a increasing number for the choice number. sorry if there have been other questions like this, I couldn't find a solution
You'll need to convert your integer to a string before you can concatenate it. You can do this with str(i). Or you can accomplish your append line with f-strings, like so: ops.append(f"{i}.) {names[i]}'s Living Quarters\n")
ops.append(str(i)+".)"+str(names[i])+"'s Living Quarters\n") Should work! str(VARIABLE) converts the VARIABLE into STR(String)
You can use an integer in a string by either converting the integer to a string using str(variable), or by formatting it in the string using F-strings. String formatting example: stringName = f"Number: {integer_variable}" Which can also be used for other variable types and is a bit more readable than concatenating a ton of variables to strings using +
There's lots of fun ways to format strings in Python. I tend to prefer string.format just because of the flexibility. ops = "{}{}.) {}'s Living Quarters\n".format(ops, i, names[i]) Ideally, you'd include the formatting for ops in there as well, but since I didn't have the code you used to generate it , I just showed you the closest I could.
How can we understand if the value inside a String is an int or not in Python?
I am writing a code where I am facing the problem and need a solution if it exists. Suppose we have a following String type variable in Python which contains an integer value. Eg:x='123' I know that we can easily convert this by type conversion to int. However, suppose we have the following list. x=['123','Spain'] Is there any method in Python by which I can know which element of the list x is Integer contained inside a string and which is purely an Object?
I would recommend this method: x = "123" if x.isdigit(): # int elif x.replace(".","",1).isdigit(): # float else: # str
I assume you have similar question with this post. But, from my perspective, for more general solution (language agnostic), you should learn more about Regular Expression, here also the same question
Iterable error with int when using for / argument ()error while using map. Where am i going worng
I tried bith ways using map nad using for loop but its not working i know for for loop it has to list,tuples or string. So how do i make this work 1 def narcissistic(value): x = ((value)== sum((c)**len(value) for c in list(value))) return x 2 def narcissistic(value): x=(value== (map(lambda c :sum(c**len(value)),value))) return x
Your issue comes down to confusion about the type of your different objects. Python is a strongly typed language, so each object has a clear type at any given moment and the language generally won't convert anything to another type automatically for you. Based on the error you're getting, you're calling your function with an int argument. This causes you trouble when you try to call len or iterate on your value. Python ints don't have a length, nor are they iterable, so it's quite understandable that these fail under the circumstances. What you want to do is create a string representation of your value number. Then you can loop over the characters of the string, and take its len freely. There's another issue though. You're also trying to do an exponential operation on the c variable in the generator expression. That won't work because c is a string, not a number. It's a one-digit string, but still a str instance! To do math with it, you need to convert it back to a number with int. Here's a fixed version of your function: def narcissistic(number): num_str = str(number) return sum(int(c)**len(num_str) for c in num_str) == number I've renamed the very generic value name with number, which should hopefully make it more clear what type each thing is.
How to parse string with comma in Python?
I am having problems parsing a string to a function in python. def logAppend(self, data): print(data) When I parse a string with a comma in the above code it returns the following. TypeError: logAppend() takes exactly 2 arguments (3 given) I am kinda new to Python, so please take it easy on me if I'm missing something simple here..
It's unclear what your string is, but if you're assuming it's (self, data), those two things are actually the variables for your function. Either could be calling a string, or more likely a list of strings, that are would be ['string1', 'string2'], for example.
Python json.dumps with string interpolation?
Let's say I want to create a json object following the structure: {"favorite_food":["icecream","hamburguers"]} to do so in python, if i know the whole string in advance, I can just do: json.dumps({"favorite_food":["icecream","hamburguers"]}) which works fine. my question though is, how would i do the same thing if i wanted to get the object as a result of a string interpolation? For example: favorite food = 'pizza' json.dumps({"favorite_food":[%s]}) %favorite_food the issue i found is, if I do the interpolation prior to calling the json.dumps: dict= '{"favorite_food":[%s]}' % favorite_food if i then do json.dumps(dict) , because of the string quotation, the json_dumps returns: {"favorite_food":[pizza]} that is, is not a dict anymore (but a string with the structure of a dict) How can i solve this simple issue?
Why not just: >>> food = "pizza" >>> json.dumps({"favorite_food":[food]}) '{"favorite_food": ["pizza"]}' json,dumps takes actual values as input --- that is, real dicts, lists, ints, and strings. If you want to put your string value in the dict, just put it in. You don't want to put in a string representation of it, you want to put in the actual value and let json.dumps make the string representation.
How about below: favorite_food = 'pizza' my_dict = {"favorite_food":[favorite_food]} print json.dumps(my_dict) I found this is very simple.