从2个键和2个元组的字典创建排序列表

时间:2017-10-16 07:13:34

标签: python tuples

尝试创建一个函数,我可以用它来创建一个排序列表,从两个键和两个元组作为值。我在“for”循环的某个地方遇到问题,因为它出于某种原因只打印键和第一个元组。不知何故,第二个元组永远不会通过分拣机。

<?php
$connection = mysqli_connect('localhost', 'root', 'root', 'crud');
$edit = $_REQUEST['edit'];
$run = mysqli_query($connection, "select * from user where id = '$edit'");

while ($row = mysqli_fetch_array($run)) {

    $name = $row[1];
    $email = $row[2];
    $pass = $row[3];
    $id = $row[5];
}
?>



  <html>
    <head>
        <title>EDIT USER</title>
    </head>
    <body>
        <center>
            <h1> EDIT USER</h1>
            <form action="" method="post">
                <?php echo '<input type="hidden" name="id" value="' . $id . '">'; ?>
                name=<input type ="text" name="uname" value="<?php echo $name ?>"/>
                password=<input type ="text" name="upass" value="<?php echo $pass ?>" />
                email=<input type ="text" name="uemail" value="<?php echo $email ?>"/>
                <input type ="submit" name="uedit" VALUE ="Edit"/>
            </form>
        </center>
    </body>
</html>

<?php
$connection = mysqli_connect('localhost', 'root', 'root', 'crud');
if (isset($_POST['uedit'])) {
    $uid = $_POST['id'];
    $uname = $_POST['uname'];
    $upass = $_POST['upass'];
    $uemail = $_POST['uemail'];

    if (mysqli_query($connection, "update user set name='$uname',email='$uemail',pass='$upass' where  id='$uid' ")) {
        header("location:index.php");
    }
}
?>

我的猜测是“for y”声明存在某个问题,但在几个版本之后,包含了中断和诸如此类的东西,我仍然无法正确输出输出。

理想情况下,输出应该是这样的:

def printDictionary(dictionaryParm):
    for x in dictionaryParm:
    header = []
    header.append(x)

    for y in dictionaryParm.values():
       value = list(y)
       value.sort()

    output = header + value
    for item in output:
        print item

dictionaryTest = dict()
dictionaryTest["Key 1"] = ("234","123","345")
dictionaryTest["Key 2"] = ("456","678","567")

printCourseDictionary(dictionaryTest)

思想?

3 个答案:

答案 0 :(得分:0)

您的代码存在一些缩进问题,但如果我理解正确您需要以下代码

def printDictionary(dictionaryParm):
    for x in dictionaryParm:
        header = [x]

        value = list(dictionaryParm[x])
        value.sort()

        output = header + value
        for item in output:
            print(item)

dictionaryTest = dict()
dictionaryTest["Key 1"] = ("234","123","345")
dictionaryTest["Key 2"] = ("456","678","567")

printDictionary(dictionaryTest)

代码输出

Key 2
456
567
678
Key 1
123
234
345

答案 1 :(得分:0)

问题是循环中的@ indentation,其中要对值进行排序和添加。这是你的问题吗? def printDictionary(dictionaryParm): for k, y in dictionaryParm.items(): value = list(y) value.sort() output = ([k]+ value) for it in output: print (it) dictionaryTest = dict() dictionaryTest["Key 1"] = ("234","123","345") dictionaryTest["Key 2"] = ("456","678","567") printDictionary(dictionaryTest) Output code Key 1 123 234 345 Key 2 456 567 678

答案 2 :(得分:0)

试试这个

def printDictionary(dictionaryParm):

    for x in dictionaryParm:
        header = []
        header.append(x)

        #print header

        value=list(dictionaryParm.get(x))
        print value
        output = header + value
        for item in output:
            print item

dictionaryTest = dict()
dictionaryTest["Key 1"] = ("123","234","345")
dictionaryTest["Key 2"] = ("456","567","678")

printDictionary(dictionaryTest)