我试图找到两个不同集的笛卡尔积.我在网上找不到关于笛卡尔产品的任何内容,无论是列表还是字典.
功率设置也很混乱.
这些都不在我一直在使用的书中.
有没有一个人能指出我正确的方向.
我试图设置一个'help'的全局变量来显示我在代码中的函数.目标是用户在"帮助"一词中输入的任何时候我都可以显示我创建的表格.该函数称为table(),我想显示它.我的代码如下:
def table():
header=(" Operation Command-line Format")
underline=("_________ ___________________")
meat=("Union set1&set2 \n Intersection set1|set2 \n Difference set1-set2 \n Symmetric difference set1 ^ set2 \n Cartesian product set1 X set2 \n Subset set1<=set2 \n Proper subset set1<set2 \n Super set set1=>set2 \n Proper super set set1>set2 \n Cardinality card set1 \n Membership-in xE set1 where x is single element\n Membership-not-in x!Eset1 where x is single element\n Power set power set1\n Display this table help")
print(header, "\n",underline,"\n",meat)
def intro():
firstChoice=input("Would you like …Run Code Online (Sandbox Code Playgroud) 我正在尝试将我的字典写入文件,并且已经知道我必须将其更改为字符串.但是有没有办法在末尾添加'\n'以保持我的文件有条理?
代码如下:
def dictionary(grocerystock):
with open('grocery_stock.txt','r+') as f:
lines = f.readlines()
# filter out empty lines
lines = [line for line in lines if line.strip() != '']
# split all lines
lines = [line.split() for line in lines]
# convert to a dictionary
grocerystock = dict((a, (b, c)) for a, b, c in lines)
# print
for k, v in grocerystock.items():
print (k, v)
grocerystock=str(grocerystock)
grocerystock=grocerystock.replace("{",'')
grocerystock=grocerystock.replace("}",'')
grocerystock=grocerystock.replace("(",'')
grocerystock=grocerystock.replace(")",'')
grocerystock=grocerystock.lstrip()
grocerystock=grocerystock.rstrip()
grocerystock=grocerystock.strip()
grocerystock=grocerystock.replace(":",'')
c=(grocerystock+("\n"))
e=open('grocery_stock.txt', 'w+')
e.write(c)
e.close()
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激.
我目前正在尝试编写一个程序,它是一个零重力的连接四游戏,这意味着你可以从棋盘的一个区域放置棋子,我正在努力选择一个柱子并从顶部放置我的棋子.此外,我的程序会询问用户他们希望在棋盘上放置多少块,这样会让游戏变得更难.在我输入例如C8时,它会查看每个列表中第8个元素的列表中的第8行.有什么想法吗?
我编辑的代码如下:
#this imports random for my blocks
import random
#this makes my board
table = [[ " ","C1", "C2", "C3", "C4", "C5", "C6", "C7", "C8", "C9", "C10"],
[ " R1|", " |", " |", " |", " |", " |", " |", " |", " |", " |", " |"],
[ " R2|", " |", " |", " |", " |", " |", " |", " |", " |", " |", " |"],
[ " R3|", " |", " …Run Code Online (Sandbox Code Playgroud)