0

A list called numbers, includes all prime integers from 1 to 1000. List numbers17 has the same numbers with numbers. I want to check if a number includes "1" or "7", and if so, remove it from the list numbers:

for number in numbers17:
    number = (list(str(number)))
    if any(x in list(str(number)) for x in ("1", "7")):
        number = [int(i) for i in number]
        number = (''.join(str(i) for i in number))
        numbers.remove(int(number))
        print(number)

The problem is that it is semi-functional. Results are:

2, 3, 5, 9, 13, 17, 21, 23, 25, 29, 33, 35, 39, 43, 45, 49, 53, 55, 59, 63,    65, 69, 73, 77, 81, 83, 85, 89, 93, 95, 99, 103, 107, 111, 115, 119, 123, 127, 131, 135, 139, 143, 147, 151, 155, 159, 163, 167, 171, 175, 179, 183, 187, 191, 195, 199, 203, 205, 209, 213, 217, 221, 223, 225, 229, 233, 235, 239, 243, 245, 249, 253, 255, 259, 263, 265, 269, 273, 277, 281, 283, 285, 289, 293, 295, 299, 303, 305, 309, 313, 317, 321, 323, 325, 329, 333, 335, 339, 343, 345, 349, 353, 355, 359, 363, 365, 369, 373, 377, 381, 383, 385, 389, 393, 395, 399, 403, 405, 409, 413, 417, 421, 423, 425, 429, 433, 435, 439, 443, 445, 449, 453, 455, 459, 463, 465, 469, 473, 477, 481, 483, 485, 489, 493, 495, 499, 503, 505, 509, 513, 517, 521, 523, 525, 529, 533, 535, 539, 543, 545, 549, 553, 555, 559, 563, 565, 569, 573, 577, 581, 583, 585, 589, 593, 595, 599, 603, 605, 609, 613, 617, 621, 623, 625, 629, 633, 635, 639, 643, 645, 649, 653, 655, 659, 663, 665, 669, 673, 677, 681, 683, 685, 689, 693, 695, 699, 703, 707, 711, 715, 719, 723, 727, 731, 735, 739, 743, 747, 751, 755, 759, 763, 767, 771, 775, 779, 783, 787, 791, 795, 799, 803, 805, 809, 813, 817, 821, 823, 825, 829, 833, 835, 839, 843, 845, 849, 853, 855, 859, 863, 865, 869, 873, 877, 881, 883, 885, 889, 893, 895, 899, 903, 905, 909, 913, 917, 921, 923, 925, 929, 933, 935, 939, 943, 945, 949, 953, 955, 959, 963, 965, 969, 973, 977, 981, 983, 985, 989, 993, 995, 999

Expected result is all numbers which include "1" or "7" E.G: 17, 477, 917 and so on.

Why is this happening?

  • Followed the answers on the previous question, and they are not fixing the problem. – TheAngryAvocado Jan 31 '15 at 16:13
  • That suggests you didn't do the job properly. You need to create a new list — call it `numbers17` — containing the relevant numbers from `numbers`, then if you want to destroy your original `number` list, assign the new list to the old. – Jonathan Leffler Jan 31 '15 at 18:30
  • @TheAngryAvocado: the additional problem is `("1" or "7")`. Try replacing this with `("1", "7")`. – Luke Woodward Jan 31 '15 at 20:38
  • @JonathanLeffler Thanks. I updated the code as understood, and the results changed a little, but it doesn't improve. – TheAngryAvocado Feb 01 '15 at 07:46
  • I think the problem is that you did `numbers17 = numbers`, and that leaves both variables pointing at the same list, rather than making a copy of the list. If you assign `numbers17 = list(numbers)`, you end up with a copy of the list and the code then works. – Jonathan Leffler Feb 01 '15 at 08:02
  • This solved my problem. Thanks for helping me with this. I am new to Python, and I couldn't imagine this simple clarification fixs the problem. Have a nice day. – TheAngryAvocado Feb 01 '15 at 08:05

0 Answers0