如果元组中的原点为[0](python)则打印
我想格式化我的代码,以便在调用如果元组中的原点为[0](python)则打印,python,python-3.x,Python,Python 3.x,我想格式化我的代码,以便在调用value[0]时打印单词river和sand datafile.txt 我的代码 上面的代码打印的内容类似于“河”(0,4),(-5,6),(-6,8),(6,9),“沙”(0,10),(6,7),(-6,76),(-75,75)。我希望它在调用值[0]时打印河流和沙子。我将如何更改代码来实现这一点?数据来自文本文件 调用值[0]时的预期输出应仅打印“river”和“sand”,而忽略所有其他输出;调用值[1://code>时,除“sand”和“river”的值外
value[0]
时打印单词river
和sand
datafile.txt
我的代码
上面的代码打印的内容类似于“河”(0,4),(-5,6),(-6,8),(6,9),“沙”(0,10),(6,7),(-6,76),(-75,75)
。我希望它在调用值[0]时打印河流和沙子。我将如何更改代码来实现这一点?数据来自文本文件
调用值[0]
时的预期输出应仅打印“river”
和“sand”
,而忽略所有其他输出;调用值[1://code>时,除“sand”
和“river”
的值外,其他所有输出均打印
还应检查值[0]
的值:
label = value[0]
if (label=='river'):
print("river")
elif (label=='sand')
print("sand")
我不确定我是否理解你的问题,但我认为这可能会满足你的要求:
textFile =open("datafile.txt", "r")
dataString = textFile.read()
res = dataString.replace(',',' ').split()
value = [[i for i in res if i.isalpha()] , [i for i in res if not i.isalpha()]]
#!/usr/bin/env python
textFile =open("datafile.txt", "r")
# construct list of non-empty lines
# strip trailing newline and split on ','
lines = [l for l in (m.rstrip('\n').split(',')
for m in textFile.readlines()) if len(l) > 1]
# make tuple from names
names = tuple(a for a,b in lines if a.isalpha())
# prepend names to list of tuples built from each line
# alphabetical entries are substituted for 0s
value = [names] + [(0,b) if a.isalpha() else (a,b) for a,b in lines]
print value
我认为这正是你想要的:
textFile =open("datafile.txt", "r")
dataString = textFile.read()
res = dataString.replace(',',' ').split()
value = [[i for i in res if i.isalpha()] , [i for i in res if not i.isalpha()]]
#!/usr/bin/env python
textFile =open("datafile.txt", "r")
# construct list of non-empty lines
# strip trailing newline and split on ','
lines = [l for l in (m.rstrip('\n').split(',')
for m in textFile.readlines()) if len(l) > 1]
# make tuple from names
names = tuple(a for a,b in lines if a.isalpha())
# prepend names to list of tuples built from each line
# alphabetical entries are substituted for 0s
value = [names] + [(0,b) if a.isalpha() else (a,b) for a,b in lines]
print value
编辑:我认为这种方法更简洁:
#!/usr/bin/env python
# use "with" to avoid having to close the file
with open("datafile.txt") as textFile:
# get array containing all values in the file
v = textFile.read().replace(',','').split()
# extract the names into a separate tuple
names = [tuple(filter(str.isalpha, v))]
# make tuples out of each pair of values, replacing names with zeroes
values = [(0,b) if a.isalpha() else (a,b) for a,b in zip(v[::2], v[1::2])]
# concatenate the two
values = names + values
print values
输出:
[('river', 'sand'), (0, ' 4'), ('-5', ' 6'), ('-6', ' 8'), ('6', ' 9'), (0, ' 10'), ('6', ' 7'), ('-6', ' 76'), ('-75', ' 75')]
和其他人一样,我并不完全理解这个请求,但我的看法是:
value = zip(*[stanza.split(',') for stanza in dataString.split('\n\n')])
这会导致value[0]
打印“字符串”river”和“sand”,而value[1:][/code>打印“其他所有内容”。“上面的代码打印的内容类似于…”是这样的吗?我在任何地方都看不到打印功能。这是你的全部密码吗?另外,“调用值[0]时”是什么意思?比如,value[0]()
?我假设顶部的文本应该是datafile.txt
的内容。请不要创建名为split
的变量。尤其是当它正在存储join()的结果时。。。更智能地命名变量。自我记录是成功编码的关键。@TomFenech,是的,没错。您可以编辑您的问题,明确说明预期的输出吗?这工作正常,但它打印的是river4
,而不是0,4
,我在代码中有一个输入错误,请重试。它对我来说很好。你的代码正在工作,但有一点很小;当我尝试检查label=value[0]if(label==“sand”):print(“sand”)elif(label==“river”):print(“river”)
它不是working@nyabuto这是因为值[0]
不是一个字符串
,而是一个包含两个字符串的元组。我以为这就是你想要的。很难判断预期输出的结构是什么。我建议你编辑你的问题,使它更清楚,确切地说,你想要什么。我已经编辑的问题,因为我认为这是可以理解的,我正在寻找什么输出