有没有办法了解字符串所包含的数据类型...这个问题没有什么逻辑,但请参阅下面的案例
varname = '444'
somefunc(varname) => int
varname = 'somestring'
somefunc(varname) => String
varname = '1.2323'
somefunc(varname) => float
Run Code Online (Sandbox Code Playgroud)
我的案例:我在列表中得到一个混合数据,但它们是字符串格式.
myList = ['1', '2', '1.2', 'string']
Run Code Online (Sandbox Code Playgroud)
我正在寻找一种通用的方法来了解他们的数据是什么,以便我可以添加各自的比较.由于它们已经转换为字符串格式,我真的不能将列表(myList)称为混合数据......但仍然有办法吗?
我意识到这对你们来说可能是简单而基本的,这是我孩子的作业,对于使用Python我没有任何经验或想法。她需要获取代码来请求一个数字,将其乘以9并显示结果。她正在使用下面的代码,但是它是重复数字而不是相乘。(即显示3 * 9为999而不是27)。从我阅读的内容来看,这似乎是将整数乘以字符串的结果(尽管我可能错了)。任何帮助将不胜感激。
number=input("Enter a number to multiply by 9 ")
number=number*9
print('the answer is '+number)
Run Code Online (Sandbox Code Playgroud) 我有一个文本文件,其中每行都有数字(只有数字)。我的color.txt样子:
3
3
5
1
5
1
5
Run Code Online (Sandbox Code Playgroud)
当我读到这个列表时使用
f=open('D:\\Emmanu\\project-data\\color.txt',"r")
for line in f:
g_colour_list.append(line.strip('\n'))
print g_colour_list
Run Code Online (Sandbox Code Playgroud)
输出就像
['3', '3', '5', '1', '5', '1', '5']
Run Code Online (Sandbox Code Playgroud)
但我希望它是:
[3,3,5,1,5,1,5]
Run Code Online (Sandbox Code Playgroud)
我怎样才能在单行中做到这一点
g_colour_list.append(line.strip('\n'))?
为了通过实践学习python,我正在尝试使用python实现和测试快速排序算法。
实现本身并不难,但是排序的结果有点令人费解:
当我对列表进行排序时
['35', '-1', '-2', '-7', '-8', '-3', '-4', '20', '-6', '53']
结果给了我
['-1', '-2', '-3', '-4', '-6', '-7', '-8', '20', '35', '53']
因此列表已排序,但负整数以相反的顺序排序。
我怀疑这可能是我正在对从文件中读取的整数列表进行排序这一事实的问题,并且 int 的类型实际上不是 int 而是其他的东西(也许是字符串。)我能做些什么来解决这个问题?
这是快速排序实现的代码
#quicksort -> the conquer part of the algorithm
def quicksort(input_list, start_index, end_index):
if start_index < end_index:
#partition the list of integers.
pivot = partition(input_list, start_index, end_index)
#recursive call on both sides of the pivot, that is excluding the pivot itself
quicksort(input_list, start_index, pivot-1)
quicksort(input_list, pivot+1, end_index)
return input_list
#divide …Run Code Online (Sandbox Code Playgroud) 我正在尝试获取一个字符串a = "99.99",然后将其转换为float类型。最重要的是,我也希望能够转换a为int。我怎样才能做到这一点?内置int()和float()函数似乎不接受字符串。
julia> a = "99.99"
"99.99"
julia> float(a)
ERROR: MethodError: no method matching AbstractFloat(::String)
Closest candidates are:
AbstractFloat(::Bool) at float.jl:252
AbstractFloat(::Int8) at float.jl:253
AbstractFloat(::Int16) at float.jl:254
...
Stacktrace:
[1] float(::String) at ./float.jl:271
[2] top-level scope at REPL[2]:1
julia> Int(a)
ERROR: MethodError: no method matching Int64(::String)
Closest candidates are:
Int64(::Union{Bool, Int32, Int64, UInt32, UInt64, UInt8, Int128, Int16, Int8, UInt128, UInt16}) at boot.jl:710
Int64(::Ptr) at boot.jl:720
Int64(::Float32) at float.jl:700
...
Stacktrace:
[1] …Run Code Online (Sandbox Code Playgroud) 我试图通过Python将.csv文件转换为netCDF4,但我无法弄清楚如何将.csv表格格式的信息存储到netCDF中.我主要担心的是我们如何将列中的变量声明为可行的netCDF4格式?我发现的一切通常都是从netCDF4中提取信息到.csv或ASCII.我提供了样本数据,示例代码和我的错误,用于声明适当的数组.任何帮助将非常感激.
样本表如下:
Station Name Country Code Lat Lon mn.yr temp1 temp2 temp3 hpa
Somewhere US 12340 35.52 23.358 1.19 -8.3 -13.1 -5 69.5
Somewhere US 12340 2.1971 -10.7 -13.9 -7.9 27.9
Somewhere US 12340 3.1971 -8.4 -13 -4.3 90.8
Run Code Online (Sandbox Code Playgroud)
我的示例代码是:
#!/ usr/bin/env python
import scipy
import numpy
import netCDF4
import csv
from numpy import arange, dtype
Run Code Online (Sandbox Code Playgroud)
#Declare空数组
v1 = []
v2 = []
v3 = []
v4 = []
Run Code Online (Sandbox Code Playgroud)
#打开csv文件并为每个标题声明数组的变量
f = open('station_data.csv', 'r').readlines()
for line in f[1:]:
fields = …Run Code Online (Sandbox Code Playgroud) 如何从 python 中的一个输入行获取 int 和 string 输入示例:对于给定的行
10 I love coding
Run Code Online (Sandbox Code Playgroud)
我想得到 10,我喜欢将编码作为单独的变量。我试过了,input().split()但因为我和爱之间有空间,所以出现了混乱
常见问题解答:在 Raku 中,如何解析String并获得Number?例如:
xxx("42"); # 42 (Int)
xxx("0x42"); # 66 (Int)
xxx("42.123456789123456789"); # 42.123456789123456789 (Rat)
xxx("42.4e2"); # 4240 (Rat)
xxx("42.4e-2"); # 0.424 (Rat)
Run Code Online (Sandbox Code Playgroud)