在Python中,假设我有:
f = open("file.txt", "r")
a = f.readlines()
b = f.readline()
print a
print b
Run Code Online (Sandbox Code Playgroud)
print a将显示文件的所有行并且print b不显示任何内容。
同样反之亦然:
f = open("file.txt", "r")
a = f.readline()
b = f.readlines()
print a
print b
Run Code Online (Sandbox Code Playgroud)
print a显示第一行,但print b将显示除第一行之外的所有行。
如果 和a都是breadlines(),a将显示所有行并且b不显示任何内容。
为什么会出现这种情况?为什么两个命令不能独立工作?有解决方法吗?
我知道我实际上可以合并两个列表(在 Python 2.7 中)如下
list1 = ['one', 'two', 'three', 'four', 'five']
list2 = ['A', 'B', 'C', 'D', 'E']
merged = list1 + list2
print merged
# ['one', 'two', 'three', 'four', 'five', 'A', 'B', 'C', 'D', 'E']
Run Code Online (Sandbox Code Playgroud)
问题是,我希望在 list1 的每两个之后插入一个 list2。例子:
list1 = ['one', 'two', 'three', 'four', 'five']
list2 = ['A', 'B', 'C', 'D', 'E']
after 2 of list1:
add 1 of list2
print merged
# ['one', 'two', 'A', 'three', 'four', 'B', 'five', 'six', 'C', 'seven', 'eight', 'D', 'nine', 'ten']
Run Code Online (Sandbox Code Playgroud)
任何帮助将非常感激!
我正在处理大量数据(元组列表),我想组织这些数据.更具体:
# my characters for the items in the strings are 1-9,a-e
# the results of my previous program produce a list of tuples
# e.g. ('string', int), where int is the count of occurrence of that string in my data
# my program currently lists them by count order, starting highest to lowest
>>> print results #results from the previous part of my code
[('7b7', 23522), ('dcd',23501)....('ccc',1)]
>>> for three_grams in results:
print (sorted(three_grams))
[23522, '7b7']
[23501, 'dcd']
.... …Run Code Online (Sandbox Code Playgroud) 我有一个方法返回一个布尔值的三元素元组,我在循环中调用它.我想最终得到一个包含or单个元组结果的三元素元组.如果该方法只返回一个布尔值,它将只是:
result = False
for j in some_list: # there is more processing in the loop, omitted
result |= method(j)
return result
Run Code Online (Sandbox Code Playgroud)
我可以用一些优雅的方式或method()现在返回的元组来概括它吗?我当然可以这样做:
result = False, False, False
for j in some_list:
res1, res2, res3 = method(j)
result = res1 | result[0], res2 | result[1], res3 | result[2]
return result
Run Code Online (Sandbox Code Playgroud)
但似乎有点不雅.
编辑:澄清我想在两种情况下返回结果 - 首先是布尔值,然后是布尔值元组
当我on_message()在我的代码中时,它会停止所有其他@bot.command命令的工作。我试过了await bot.process_commands(message),但这也不起作用。这是我的代码:
@bot.event
@commands.has_role("Owner")
async def on_message(message):
if message.content.startswith('/lockdown'):
await bot.process_commands(message)
embed = discord.Embed(title=":warning: Do you want to activate Lock Down?", description="Type 'confirm' to activate Lock Down mode", color=0xFFFF00)
embed.add_field(name="\u200b", value="Lock Down mode is still in early development, expect some issues")
channel = message.channel
await bot.send_message(message.channel, embed=embed)
msg = await bot.wait_for_message(author=message.author, content='confirm')
embed = discord.Embed(title=":white_check_mark: Lock Down mode successfully activated", description="To deactivate type '/lockdownstop'", color=0x00ff00)
embed.add_field(name="\u200b", value="Lock Down mode is still in early development, …Run Code Online (Sandbox Code Playgroud) 我是 discord.py 的新手,正在尝试制作翻译机器人。当用户对某个标志做出反应时,机器人会对其进行翻译,但该事件永远不会被调用,因此我还没有代码来翻译任何消息。我知道它没有被调用,因为程序没有打印'x'到控制台。
@client.event
async def on_reaction_add(reaction, user):
channel = reaction.message.channel
print('x')
await client.send_message(channel, '{} has added {} to the the message {}'.format(user.name, reaction.emoji, reaction.message.content))
await client.process_commands(reaction.message)
Run Code Online (Sandbox Code Playgroud) 我想遍历dict,spam并以格式打印结果"key: value".我的代码产生了不同的结果.
有没有办法纠正输出?为什么我得到这个输出?
spam = {'color': 'red', 'age': '42', 'planet of origin': 'mars'}
for k in spam.keys():
print(str(k) + ': ' + str(spam.values()))
Run Code Online (Sandbox Code Playgroud)
得到的结果是:
color: dict_values(['red', '42', 'mars'])
age: dict_values(['red', '42', 'mars'])
planet of origin: dict_values(['red', '42', 'mars'])
Run Code Online (Sandbox Code Playgroud)
预期结果:
color: red
age: 42
planet of origin: mars
Run Code Online (Sandbox Code Playgroud) 如何将文件复制到另一个文件?
我正在使用的代码是:
FileX = open("X.txt","r")
FileY = open("Y.txt","w")
X = FileX
FileY.write(FileX)
FileX.close()
FileY.close()
Run Code Online (Sandbox Code Playgroud)
这给出了错误:
TypeError: write() argument must be str, not _io.TextIOWrapper
Run Code Online (Sandbox Code Playgroud)
我该如何解决这个错误?
如果我有一套:
{'NYC', 'Ames', 'LA', 'Houston', '500', '1000', '3000',
'SanFrancisco', '300', '200', 'Detroit', 'Austin'}
Run Code Online (Sandbox Code Playgroud)
如何从集合中删除所有数字字符串?
要明确我想要这个:
{'NYC', 'Ames', 'LA', 'Houston', 'SanFrancisco', 'Detroit', 'Austin'}
Run Code Online (Sandbox Code Playgroud) 我有一个命令:
@bot.command(pass_context=True)
async def hellothere(ctx):
await Bot.say("Hello {}".format(ctx.message.author))
Run Code Online (Sandbox Code Playgroud)
我想复制此命令,使其更短。
我试过了:
@bot.command(pass_context=True)
async def hello(ctx):
hellothere(ctx)
Run Code Online (Sandbox Code Playgroud)
但是我收到一条错误消息,指出该错误Command不可调用。
有谁知道如何做到这一点?
python ×10
discord.py ×3
discord ×2
append ×1
dictionary ×1
file ×1
list ×1
python-2.7 ×1
python-3.x ×1
readline ×1
readlines ×1
set ×1
sorting ×1
string ×1
tuples ×1
typeerror ×1