在Python中对print()文本进行排序

时间:2018-01-05 15:23:58

标签: python sorting

我有一些python代码可以让电话号码在一个国家/地区投票。他们不能自己投票等等......真的不重要

import random

Countries = ["Belgium", "Netherlands", "Italy", "France", "Germany", "Spain"]

for num in range(5):
    country = random.choice(Countries)

    if(country is "Belgium"):
        phone = f'+32 4{random.randint(1000, 9999)}-{random.randint(1000,9999)}'
        countryVote = random.choices(Countries, weights=[0, 25, 7, 25, 25, 7], k=1)

    elif(country is "Netherlands"):
        phone = f'+31 6{random.randint(1000, 9999)}-{random.randint(1000,9999)}'
        countryVote = random.choices(Countries, weights=[25, 0, 5, 15, 20, 8], k=1)

    elif(country is "Italy"):
        phone = f'+39 3{random.randint(1000, 9999)}-{random.randint(1000,9999)}'
        countryVote = random.choices(Countries, weights=[5, 5, 0, 20, 5, 13], k=1)

    elif(country is "France"):
        phone = f'+33 1{random.randint(1000, 9999)}-{random.randint(1000,9999)}'
        countryVote = random.choices(Countries, weights=[15, 8, 10, 0, 12, 8], k=1)

    elif(country is "Germany"):
        phone = f'+49 1{random.randint(1000, 9999)}-{random.randint(1000,9999)}'
        countryVote = random.choices(Countries, weights=[8, 8, 9, 5, 0, 12, 3], k=1)

    elif(country is "Spain"):
        phone = f'+34 6{random.randint(1000, 9999)}-{random.randint(1000,9999)}'
        countryVote = random.choices(Countries, weights=[5, 5, 15, 15, 10, 0], k=1)

    else:
        phone = f'Country does not exist'
        countryVote = random.choice(Countries)

    print(f'{phone} voted for {countryVote}')

我的输出看起来像这样

Output image

我想要实现的是对数字进行排序,因此所有+31数字都被组合在一起。 我只找到了如何排序对象列表而不是打印文本..

lines = print.readlines() 
lines.sort()

提前谢谢!

1 个答案:

答案 0 :(得分:1)

例如,您可以在打印前将电话号码收集到列表中,然后使用排序功能打印结果列表:

import random
from pprint import pprint

Countries = ["Belgium", "Netherlands", "Italy", "France", "Germany", "Spain"]
result_numbers = []

for num in range(5):
    country = random.choice(Countries)

    if(country is "Belgium"):
        phone = f'+32 4{random.randint(1000, 9999)}-{random.randint(1000,9999)}'
        countryVote = random.choices(Countries, weights=[0, 25, 7, 25, 25, 7], k=1)

    elif(country is "Netherlands"):
        phone = f'+31 6{random.randint(1000, 9999)}-{random.randint(1000,9999)}'
        countryVote = random.choices(Countries, weights=[25, 0, 5, 15, 20, 8], k=1)

    elif(country is "Italy"):
        phone = f'+39 3{random.randint(1000, 9999)}-{random.randint(1000,9999)}'
        countryVote = random.choices(Countries, weights=[5, 5, 0, 20, 5, 13], k=1)

    elif(country is "France"):
        phone = f'+33 1{random.randint(1000, 9999)}-{random.randint(1000,9999)}'
        countryVote = random.choices(Countries, weights=[15, 8, 10, 0, 12, 8], k=1)

    elif(country is "Germany"):
        phone = f'+49 1{random.randint(1000, 9999)}-{random.randint(1000,9999)}'
        countryVote = random.choices(Countries, weights=[8, 8, 9, 5, 0, 12, 3], k=1)

    elif(country is "Spain"):
        phone = f'+34 6{random.randint(1000, 9999)}-{random.randint(1000,9999)}'
        countryVote = random.choices(Countries, weights=[5, 5, 15, 15, 10, 0], k=1)

    else:
        phone = f'Country does not exist'
        countryVote = random.choice(Countries)

    result_numbers.append(f'{phone} voted for {countryVote}')

pprint(sorted(result_numbers))

btw不要使用is来比较字符串和另外一个: 国家不是一个类,所以你需要使用小写或大写,如果你想让它保持不变)