Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python-遍历多个字典及其键_Python_Dictionary - Fatal编程技术网

Python-遍历多个字典及其键

Python-遍历多个字典及其键,python,dictionary,Python,Dictionary,我正在尝试创建一个程序,用户可以在其中输入多个彩票号码,并将这些号码与一组中奖号码进行比较。我知道我可以通过使用几个if语句来实现,但我认为必须有一种方法在循环中实现这一点。我尝试了一些使用“for key in_u__;”的循环,但我一直收到错误 amount=int(输入('您有多少组数字?')) 票证={} ticketMatch={} WiningNumber={ '1': '1', '2': '2', '3': '3', '4': '4', '5': '5', '6': '6', }

我正在尝试创建一个程序,用户可以在其中输入多个彩票号码,并将这些号码与一组中奖号码进行比较。我知道我可以通过使用几个if语句来实现,但我认为必须有一种方法在循环中实现这一点。我尝试了一些使用“for key in_u__;”的循环,但我一直收到错误

amount=int(输入('您有多少组数字?'))
票证={}
ticketMatch={}
WiningNumber={
'1': '1',
'2': '2',
'3': '3',
'4': '4',
'5': '5',
'6': '6',
}
对于范围内的i(0,金额,1):
票[i]={
“1”:输入(“输入#1”),
“2”:输入(“输入#2”),
“3”:输入(“输入#3”),
“4”:输入(“输入#4”),
“5”:输入(“输入#5”),
“6”:输入(“输入Powerball”),
}
对于范围内的i(0,len(票证),1):
ticketMatch[i]=0
如果winingNumbers.values()中的票证[i]['1']:
ticketMatch[i]+=1
如果winingNumbers.values()中的票证[i]['2']:
ticketMatch[i]+=1

如有任何提示或提示,将不胜感激。谢谢

遍历任何字典都非常简单。假设我们有以下词典:

amount = int(input('How many sets of numbers do you have?'))
winingNumbers = {1, 2, 3, 4, 5, 6}
tickets = [set() for _ in range(amount)]
ticketMatch = []


for i in range(amount):
    for j in range(6):
        if j == 5:
            tickets[i].add(int(input("Input Powerball ")))
        else:
            tickets[i].add(int(input("Input #" + str(j + 1) + " ")))

for i in range(amount):
    ticketMatch.append(len(tickets[i] & winingNumbers))
d={
“a”:1,
“z”:26
}
我们想打印出键及其值

对于d中的键:#注意,键代表“a”和“z”
打印(键,d[键])
然而,对于您试图实现的目标,可能根本不需要使用字典,就像@ggorlen评论的那样

我想出了一个快速的表演来做同样的事情

query=“输入彩票号码或“退出”退出:”
条目=输入(查询)
数字=[]
当输入时!=“退出”:
数字。追加(条目)
条目=输入(查询)
中奖号码=“123456”
对于范围内的i(len(数字)):
如果数字[i]==中奖数字:
打印(f“票{i}是赢家!”)

我不知道你为什么要把每张彩票的1号和2号与中奖总人数进行对比,因为这不是普通彩票的工作方式,这就是为什么我的版本看起来有点不同,但当然,如果它是你想要的,你可以很容易地进行更改。

遍历任何字典都是非常简单的。假设我们有以下词典:

d={
“a”:1,
“z”:26
}
我们想打印出键及其值

对于d中的键:#注意,键代表“a”和“z”
打印(键,d[键])
然而,对于您试图实现的目标,可能根本不需要使用字典,就像@ggorlen评论的那样

我想出了一个快速的表演来做同样的事情

query=“输入彩票号码或“退出”退出:”
条目=输入(查询)
数字=[]
当输入时!=“退出”:
数字。追加(条目)
条目=输入(查询)
中奖号码=“123456”
对于范围内的i(len(数字)):
如果数字[i]==中奖数字:
打印(f“票{i}是赢家!”)
我不知道你为什么要将每张彩票的1号和2号与总中奖号码进行对比,因为这不是普通彩票的工作方式,这就是为什么我的版本看起来有点不同,但当然,如果这是你想要的,你可以很容易地进行更改。

  • 只有当您有一个键并通过它存储一个值时,字典才有意义。访问键的值并检查键是否是字典的一部分是快速的(
    O(1)
    )-迭代
    dict
    的所有值与
    列表
    一样慢-但是创建字典比创建
    列表
    慢,并且占用更多的计算机内存
  • 使用
    set
    操作避免使用
    if的
    级联-示例见下文
使用
split()
和列表分解
*a,b=[1,2,3,4]
可以更快地在循环中输入:

for _ in range(int(input("How many tickets do you want to enter? "))):
    *nums, power = map(int, 
         input( ("Input space seperated numbers of your ticket,"
                 " last one is Powerball: ") ).strip().split())
    print(nums, power)    
输出:

How many tickets do you want to enter? 3
Input space seperated numbers of your ticket, last one is Powerball: 1 2 3 4 99
    [1, 2, 3, 4] 99
Input space seperated numbers of your ticket, last one is Powerball: 2 3 4 7 199
    [2, 3, 4, 7] 199
Input space seperated numbers of your ticket, last one is Powerball: 4 2 4 5 6
    [4, 2, 4, 5] 6
Winner:  14 49 26 27 60    Powerball:  6

You got [49] correct and guessed [21, 41, 59, 66] wrong. Powerball: wrong.
You got [60] correct and guessed [17, 19, 63, 68] wrong. Powerball: wrong.
You got 'nothing' correct and guessed [10, 21, 51, 67, 69] wrong. Powerball: wrong.
You got 'nothing' correct and guessed [18, 30, 40, 45, 52] wrong. Powerball: wrong.
You got [26, 27] correct and guessed [11, 37, 58] wrong. Powerball: wrong.
You got 'nothing' correct and guessed [28, 33, 38, 59, 65] wrong. Powerball: wrong.
You got 'nothing' correct and guessed [11, 18, 35, 61, 64] wrong. Powerball: wrong.
You got 'nothing' correct and guessed [2, 3, 47, 54, 63] wrong. Powerball: wrong.
You got [14] correct and guessed [23, 25, 58, 66] wrong. Powerball: wrong.
You got [27] correct and guessed [47, 52, 56, 58] wrong. Powerball: correct.
(尽管对于根本不输入NUMEBR或输入的NUMEBR太少/超出范围的用户,可能需要进行更多检查:)


对于乐透比较,可以使用
set
-操作快速检查带有一组()数字的“彩票”列表中的正确数字

检查车票列表将是
O(n)
(使用
n
==车票数量)-检查号码是否与中奖号码匹配很快:
O(1)
,并避免
如果..

您可以这样做(完全随机示例):

输出:

How many tickets do you want to enter? 3
Input space seperated numbers of your ticket, last one is Powerball: 1 2 3 4 99
    [1, 2, 3, 4] 99
Input space seperated numbers of your ticket, last one is Powerball: 2 3 4 7 199
    [2, 3, 4, 7] 199
Input space seperated numbers of your ticket, last one is Powerball: 4 2 4 5 6
    [4, 2, 4, 5] 6
Winner:  14 49 26 27 60    Powerball:  6

You got [49] correct and guessed [21, 41, 59, 66] wrong. Powerball: wrong.
You got [60] correct and guessed [17, 19, 63, 68] wrong. Powerball: wrong.
You got 'nothing' correct and guessed [10, 21, 51, 67, 69] wrong. Powerball: wrong.
You got 'nothing' correct and guessed [18, 30, 40, 45, 52] wrong. Powerball: wrong.
You got [26, 27] correct and guessed [11, 37, 58] wrong. Powerball: wrong.
You got 'nothing' correct and guessed [28, 33, 38, 59, 65] wrong. Powerball: wrong.
You got 'nothing' correct and guessed [11, 18, 35, 61, 64] wrong. Powerball: wrong.
You got 'nothing' correct and guessed [2, 3, 47, 54, 63] wrong. Powerball: wrong.
You got [14] correct and guessed [23, 25, 58, 66] wrong. Powerball: wrong.
You got [27] correct and guessed [47, 52, 56, 58] wrong. Powerball: correct.
见:

      • 只有当您有一个键并通过它存储一个值时,字典才有意义。访问键的值并检查键是否是字典的一部分是快速的(
        O(1)
        )-迭代
        dict
        的所有值与
        列表
        一样慢-但是创建字典比创建
        列表
        慢,并且占用更多的计算机内存
      • 使用
        set
        操作避免使用
        if的
        级联-示例见下文
      使用
      split()
      和列表分解
      *a,b=[1,2,3,4]
      可以更快地在循环中输入:

      for _ in range(int(input("How many tickets do you want to enter? "))):
          *nums, power = map(int, 
               input( ("Input space seperated numbers of your ticket,"
                       " last one is Powerball: ") ).strip().split())
          print(nums, power)    
      
      输出:

      How many tickets do you want to enter? 3
      Input space seperated numbers of your ticket, last one is Powerball: 1 2 3 4 99
          [1, 2, 3, 4] 99
      Input space seperated numbers of your ticket, last one is Powerball: 2 3 4 7 199
          [2, 3, 4, 7] 199
      Input space seperated numbers of your ticket, last one is Powerball: 4 2 4 5 6
          [4, 2, 4, 5] 6
      
      Winner:  14 49 26 27 60    Powerball:  6
      
      You got [49] correct and guessed [21, 41, 59, 66] wrong. Powerball: wrong.
      You got [60] correct and guessed [17, 19, 63, 68] wrong. Powerball: wrong.
      You got 'nothing' correct and guessed [10, 21, 51, 67, 69] wrong. Powerball: wrong.
      You got 'nothing' correct and guessed [18, 30, 40, 45, 52] wrong. Powerball: wrong.
      You got [26, 27] correct and guessed [11, 37, 58] wrong. Powerball: wrong.
      You got 'nothing' correct and guessed [28, 33, 38, 59, 65] wrong. Powerball: wrong.
      You got 'nothing' correct and guessed [11, 18, 35, 61, 64] wrong. Powerball: wrong.
      You got 'nothing' correct and guessed [2, 3, 47, 54, 63] wrong. Powerball: wrong.
      You got [14] correct and guessed [23, 25, 58, 66] wrong. Powerball: wrong.
      You got [27] correct and guessed [47, 52, 56, 58] wrong. Powerball: correct.
      
      (尽管对于根本不输入NUMEBR或输入的NUMEBR太少/超出范围的用户,可能需要进行更多检查:)


      对于乐透比较,可以使用
      set
      -操作快速检查带有一组()数字的“彩票”列表中的正确数字

      检查车票列表将是
      O(n)
      (使用
      n
      ==车票数量)-检查号码是否与中奖号码匹配很快:
      O(1)
      ,并避免
      如果..

      您可以这样做(完全随机示例):

      输出:

      How many tickets do you want to enter? 3
      Input space seperated numbers of your ticket, last one is Powerball: 1 2 3 4 99
          [1, 2, 3, 4] 99
      Input space seperated numbers of your ticket, last one is Powerball: 2 3 4 7 199
          [2, 3, 4, 7] 199
      Input space seperated numbers of your ticket, last one is Powerball: 4 2 4 5 6
          [4, 2, 4, 5] 6
      
      Winner:  14 49 26 27 60    Powerball:  6
      
      You got [49] correct and guessed [21, 41, 59, 66] wrong. Powerball: wrong.
      You got [60] correct and guessed [17, 19, 63, 68] wrong. Powerball: wrong.
      You got 'nothing' correct and guessed [10, 21, 51, 67, 69] wrong. Powerball: wrong.
      You got 'nothing' correct and guessed [18, 30, 40, 45, 52] wrong. Powerball: wrong.
      You got [26, 27] correct and guessed [11, 37, 58] wrong. Powerball: wrong.
      You got 'nothing' correct and guessed [28, 33, 38, 59, 65] wrong. Powerball: wrong.
      You got 'nothing' correct and guessed [11, 18, 35, 61, 64] wrong. Powerball: wrong.
      You got 'nothing' correct and guessed [2, 3, 47, 54, 63] wrong. Powerball: wrong.
      You got [14] correct and guessed [23, 25, 58, 66] wrong. Powerball: wrong.
      You got [27] correct and guessed [47, 52, 56, 58] wrong. Powerball: correct.
      
      见:


      欢迎来到SO!我不确定这样使用dicts是否有意义。首先,如果您只是调用
      value